I have been looking for something to make it click in my head on how to get the average of my array and I need help.

#include <iostream>
#include <math.h>
using namespace std;

int grades[10],a,total;
double average;
void main(){
	
	cout << "Enter 10 test grades to average:"  <<endl <<endl;
	


for (a = 0; a < 10; a++){
cout << "Enter a grade: ";
cin >> grades[a];
	
}
while { 
	total = total + grades[a];
	average = total/10;

	cout << average  <<endl;
}


	
	char wait;
	cout << "Press any key followed by enter to exit. ";
	cin >> wait;

	return;
}

Recommended Answers

All 3 Replies

After some thinking I finally got it. Here is what I came up with.

#include <iostream>
#include <math.h>
using namespace std;

int grades[10],a,total;
double average;
void main(){
	
	cout << "Enter 10 test grades to average:"  <<endl <<endl;
	


for (a = 0; a < 10; a++){
	cout << "Enter a grade: " ;
	cin >> grades[a];
	total = total + grades[a];
	average = total/10;
	
}

	cout <<endl;
	cout << "Your average is: " << average <<endl <<endl;

if (average >=90) {
	cout << "That is an 'A':";
}
else if (average >=80){
	cout << "That is a 'B':";
}

else if (average >=70){
	cout << "That is a 'C':";
}
else if (average >=60){
	cout << "That is a 'D':";
}
else {
	cout << "That is a 'F':";
}
	cout <<endl <<endl;

	char wait;
	cout << "Press any key followed by enter to exit. ";
	cin >> wait;

	return;
}

Except you need to move the average calculation outside the for loop so it's not done each time. You should cast total to a double before the division so that your average isn't truncated.

First initialize your variables.

Average = totalSum/totalElement.

Thus totalSum should be computed first and then divide it by the
array size.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.