Exam Average | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Exam Average

I'm trying to output the average of 4 exam scores. I can input values put the average is not ouput. //program takes 4 exam scores and calculate average #include<iostream> using namespace std; double average(int a[], int size); int main() { int studentScores[4]; cout << "This program calculates your average exam score." << endl; cout << "Please input your test scores. Separate scores using a space." << endl; for (int i = 0; i < 4; i++) { int number; cin >> number; } average(studentScores, 4); system("pause"); return(0); } double average(int a[], int size) { int sum = 0; int i = 0; for (int i = 0; i < size; i++) { sum += a[i++]; } return (sum / size); }

29th Sep 2018, 9:52 PM
Krista Clark
Krista Clark - avatar
3 Answers
29th Sep 2018, 11:38 PM
Aleksander Szczepura
Aleksander Szczepura - avatar
+ 1
inside the for loop in main, remove the variable number, you don't need it. You should, instead use: cin >> studentScore[i]; Also, in the for loop inside the average function put: sum += a[i]; instead of: sum += a[i++] ;
29th Sep 2018, 11:39 PM
Ulisses Cruz
Ulisses Cruz - avatar
0
correct code is function main() { var exam1 = parseInt(readLine(), 10); var exam2 = parseInt(readLine(), 10); var exam3 = parseInt(readLine(), 10); console.log(Exams.average(exam1, exam2, exam3)); // Call the static method } class Exams { static average(...scores) { let sum = 0; for (let score of scores) { sum += score; // Add each score to the sum } // Calculate and return the rounded average return Math.round(sum / scores.length); } }
10th Jan 2024, 3:56 AM
Maysa Jumayeva
Maysa Jumayeva - avatar