Hi I need help to do this Thanks a lotSolutionaveragem funct

Hi! I need help to do this:

Thanks a lot!

Solution

average.m

%function to return the average of elements in a given array

function av = average(arr)

%initialising the total of all values and the number of values
total = 0;
n = 0;

%iterating through the array (arr)
for elem=arr
    total = total + elem; %updating total by adding each element
    n = n + 1; %incrementing the number of values
end

%return average of the values which is total/number of values
av = total*1.0/n;

script.m

%program to test the above function

%declaring an array

arr = [2 4 5 78 5 68];

%calling average on the array and displaying the result.
disp(average(arr));

Hi! I need help to do this: Thanks a lot!Solutionaverage.m %function to return the average of elements in a given array function av = average(arr) %initialising

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site