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); }
3 odpowiedzi
+ 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++] ;
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);
}
}