0

double arr[]={5,2,7,8,11,9}; int n = sizeof(arr) / sizeof(arr[0]); double sum = 0; for(int i=0;i<n;i++) { sum+= arr[i]; } double avg = sum / n; cout<< "avarage ="<< avg<< endl; return 0; ) /*what does int n= sizeof(arr[]) /sizeof(arr[0]); do, can someone explain that line for me*/

15th Oct 2016, 11:31 AM
Leslie Jason
Leslie Jason - avatar
3 ответов
+ 2
int n = sizeof(arr) / sizeof(arr[0]); This line is to calculate the length of the array arr, and store it in n. sizeof(arr) is the size of the whole array (size as in how much space it takes in memory), and sizeof(arr[0]) is the size of a single element. The first divided by the second gives you the number of elements that arr can contain, ie its length.
15th Oct 2016, 11:43 AM
Zen
Zen - avatar
0
thank you, it makes a lot of sense now
15th Oct 2016, 11:47 AM
Leslie Jason
Leslie Jason - avatar
0
how to find average
19th Oct 2016, 9:55 AM
ramsha iqbal