I am trying to write a function that calculates the average of a sequence of num
ID: 3798691 • Letter: I
Question
I am trying to write a function that calculates the average of a sequence of numbers in an array. It should take in an input array and prints out the average with with only two place values after decimal.
If I type these into the Command window individually I get the write answer but I want to creat a function to allow input and output answer in the right decimal form.
A = [2, 4, 5, 6, 6, 6];
ANSWER = mean (A);
fprintf('%.2f',ANSWER)
I get the following answer of 4.83. This is write but I am entering each line individually.
Example 1: input array is [2 4 5 6 6 6], should ouptut 4.83
Explanation / Answer
#include<stdio.h>
void calcmean(int array[])
{
float sum=0;
float avg=0;
int i;
for(i=0;i<6;i++)
{
sum=sum+array[i];
}
avg=sum/6;
printf(" the mean is %0.2f ",avg);
}
int main()
{
int i;
int array[]={2,4,5,6,6,6};
printf("input array is [ ");
for(i=0;i<6;i++)
{
printf("%d ",array[i]);
}
printf("]");
calcmean(array);
}
output
input array is [ 2 4 5 6 6 6 ]
the mean is 4.83
--------------------------------
Process exited after 0.02383 seconds with return value 20
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.