I will store float data in an array and we will assume a maximum of 10 values Wr
ID: 3629676 • Letter: I
Question
I will store float data in an array and we will assume a maximum of 10 values
Write a function which must accept two arguments, the data array and how many data
values there are in it to be averaged. The function must return a float which is the average of the data provided. Note if the user entered 3 values then we must average over only the first 3 array elements not all 10.
In the main code first create a float array to hold 10 values.
Then repeatedly ask the user how many floats they wish to enter until they enter a
number in the range 1-10.
Then ask them to enter that many data values and fill (part of) the array.
Call the averaging function and print the result to the screen.
Thanks a lot !
Here is my code, is there any problems:
#include <stdio.h>
#include <clib.h>
float average( float[] , number );
(
//I think there must be have an equation//
}
int main()
{
int number , s ;
float avrg,data,arr[10];
char ch ;
printf(" Enter the number of data : ");
scanf("%c",&ch);
for(s = 0 ; s < number ; s++ )
{
printf(" Enter %i nd data : ",s+1);
scanf("%f",&data);
arr[s] = data;
}
avrg = average(arr,number);
printf(" Average is %.2f ",avrg);
}
Explanation / Answer
please rate - thanks
I changed yours as little as possible
#include <stdio.h>
#include <conio.h>
float average( float[] , int );
int main()
{
int number , s ;
float avrg,data,arr[10];
char ch ;
printf(" Enter the number of data : ");
scanf("%d",&number);
while(number<1||number>10)
{printf("must be between 1 and 10 ");
printf(" Enter the number of data : ");
scanf("%d",&number);
}
for(s = 0 ; s < number ; s++ )
{
printf(" Enter %i nd data : ",s+1);
scanf("%f",&arr[s]);
}
avrg = average(arr,number);
printf(" Average is %.2f ",avrg);
getch();
}
float average( float a[] , int n)
{int i;
float sum=0;
for(i=0;i<n;i++)
sum+=a[i];
return sum/n;
}