Write a program in MATLAB that: i) Creates a two-dimensional array A whose first
ID: 3730585 • Letter: W
Question
Write a program in MATLAB that:
i) Creates a two-dimensional array A whose first column will contain degrees of temperature from 0 to 100 per 5 degrees Celsius (0, 5, 10, 15, ..., 100), while the second
column will contain the corresponding degrees in Fahrenheit , 41, 50, ..., 212).
ii) Calculates the average temperature in degrees Celsius and Fahrenheit of array A and store the averages in a one-dimensional array B.
iii) Displays in the Command Window: a) array A with disp, and b) the values of array B with fprintf to display the following message:
Mean temperature is 50 Celcius or 122 Fahrenheit.
The relationship between temperatures in degrees Celsius and Fahrenheit is as follows: F=9/5C+32 where C is the temperature in degrees Celsius and F in Fahrenheit.
Explanation / Answer
% part (i)
A = zeros( 21 , 2 );
index = 1;
% initialize the first colum
for i = 0 : 5 : 100
A(index, 1) = i;
index = index + 1;
end
index = 1;
% initialize the first colum
for i = 41 : 9 : 221
A(index, 2) = i;
index = index + 1;
end
% part (ii)
B = [ 0 , 0 ];
sum_F = 0;
sum_C = 0;
% get the rows and columns
[ r , c ] = size(A);
for i = 1 : r
sum_C = sum_C + A( i , 1 );
sum_F = sum_F + A( i , 2 );
end
% calculate the averages
average_C = sum_C / r;
% calculate the averages
average_F = sum_F / r;
B = [ average_C , average_F ];
% part (iii)
fprintf('A ... ');
% Displays in the Command Window: a) array A with disp
disp(A);
fprintf(' B ... ');
fprintf('Mean temperature is %d or %d Fahrenheit. ', B(1), B(2));
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.