6 Generate a block of MATLAB code that the average of a grou
6. Generate a block of MATLAB code that the average of a group of numbers. Collect the number of values from the user as well as the values for the numbers to be averaged. Place the values to be averaged in a row array. Display the value for the average as a floating point number, with a maximum field width of 6 characters, and with accuracy to 4 decimal places. (5)
Solution
MATLAB Code
clear
clc
disp(\'In put Numbers to average = \');
for i = 1:1:6
A(i) = input(\'\');
j = i;
i = i+1;
end
S = 0;
for i = 1:1:6
A1(1,i) = A(i);
S = (S + A1(1,i));
end
j
Avg = (S)/(j)
A1
sprintf(\'Avg = %6.4f\',Avg)
OUTPUT :-
In put Numbers to average =
55
99
77
44
88
11
j =
6
Avg =
62.3333
A1 =
55 99 77 44 88 11
ans =
Avg = 62.3333
>>
