0

passing array as arguments specx range of array at main func?is the foll snippet wrong

float avg(a,size) float a[]; int size; { int i; float sum=0; for(i=0;i<size;i++) sum+=a[i]; return(sum/size); } //main func #define NUM 25 float a[NUM]; avg(a,NUM);

15th Aug 2018, 10:27 AM
Charlie
Charlie - avatar
2 Réponses
0
your first line should be float avg(float a[],int size) and then you dont need second and third line.
15th Aug 2018, 11:09 AM
Mooaholic
Mooaholic - avatar
+ 2
A minor modification is all that is needed here: #include <iostream> #define NUM 25 float avg(float a[], int size) { float sum = 0; for(int i = 0; i < size; i++) sum += a[i]; return(sum / size); } // main func int main() { float a[NUM]; // Get value for each array element // 25 inputs needed (as defined by NUM) for(int i = 0; i < NUM; i++) std::cin >> a[i]; // Calculate & display avg std::cout << " AVG: " << avg(a, NUM); }
15th Aug 2018, 11:24 AM
Ipang