Computer Science, asked by goms20, 11 months ago

The following code finds out the average of five numbers using arrays. There is some error in it. Can you fix the bug?




=5
1
#include
2
int main()
3
{
4
int arr[] = {10, 20, 30, 40, 50};
5
int size=5;
6
int sum = 0;
7
for(int i = 0; i < size; i++){
8
sum = sum + arr[0];
9
}
10
std::cout< 11
return 0;
12
}

Answers

Answered by prashantrohilla32
1

Answer: sum=sum+arr[i];

Explanation:

i will start from 0;

and it will go from 0 to arr.length, thus adding all elements one by one in sum;

Answered by MynkxB
0

Answer:

#include<iostream>

int main()

{

   int arr[] = {10, 20, 30, 40, 50};

   int size=5;

   int sum = 0;

   for(int i = 0; i < size; i++){  

     sum=sum+arr[i];

   }

   std::cout<<sum/size;

   return 0;

}

Explanation:

in a loop array index should be specified by loop index variable.

Similar questions