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*/
3 odpowiedzi
+ 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.
0
thank you, it makes a lot of sense now
0
how to find average