โ Back to Arrays
Question 102
Calculate the Average
Calculate the Average
An array called nums has already been created for you with the values {10, 20, 30, 40, 50}. A variable called sum has been initialized to 0.
Your task is to:
1. Use the for loop to add up all the elements in the array.
2. After the loop, calculate the average by dividing the sum by the number of elements (5).
3. Print the result in this exact format: Average: 30
The average is the total sum divided by how many numbers there are. Since 10 + 20 + 30 + 40 + 50 = 150, and there are 5 elements, the average is 150 / 5 = 30.Expected Output:
Average: 30
Topics:
Arrays
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.