Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get the student average and the class average to function and I cant think of how to do this based on the information that I have
Do I need another array to process this and if so how would I word it to work when I know that for the student average I need to take their total curgrades then divide by the numgrades.and for class average total of avggrade and divide it by numstudents. But I cant figure out how to get the information out of the array when I try i get nothing but errors like char or not defined is there another way to make this easier to figure out? thanks

C#
int numgrade;
int numstudent;
const int max_students = 499;
const int max_grade = 499;
string student [max_students];
int grades [max_students][max_grade] = {0,0};

for (int curstudent =0; curstudent < numstudent; curstudent++){	
	cout << "Enter the students name: ";
	cin >> student[curstudent];
    for (int curgrade =0; curgrade < numgrade; curgrade++){
		cout << "Enter the grade: ";
		cin >> grades[curstudent][curgrade];
	}
}
for (int curstudent = 0; curstudent < numstudent; curstudent++){  
	cout << student [curstudent] << endl;
	for (int curgrade = 0; curgrade < numgrade; curgrade++){
		cout << grades[curstudent][curgrade] << endl;
	}
}

            
system("pause");
return 0;
}
Posted
Updated 24-Oct-10 3:04am
v3

You have two loops like:
for (int curstudent =0; curstudent < numstudent; curstudent++)

However, since numstudent is zero neither will do anything. I see a similar issue with numgrade.

It would probably be better to read all the data into your arrays first and then iterate the arrays to calculate your averages.
 
Share this answer
 
Comments
EDITH GINGRAS 24-Oct-10 9:08am    
ok I think I got what you mean but where in the array would this take place I am confused on how to get the information out of the array and use it for the avg calculation then to get the class average out of the arrays be more specific on that process please it would help me to understand how to work it thanks
Brij2010 24-Oct-10 9:58am    
good call
In your first loop your termination test cannot be based on numstudent as this value is indeterminate until you have read in all the students. You need to repeat this loop until the user responds with some value that indicates no more students. The same rule holds for the inner loop that reads all the grades. However while reading these values you should be incrementing the count of students so you know how many entries to process in the second part of the program. You could also use some special value (NULL if a string, or -1 if numeric) to indicate the end of the information. In your second loop you process each student array and add the grades together as you go and after adding the last value you can divide the total by the number of grades to get the student average. While processing these you should also be adding them to the class total and at the very end divide by the total number of grades in the class to get your class average. Try writing the individual steps on paper first so you understand what values you need to keep at each step in the process and what counters you need. Then it is a simple matter to transform your design into code.
 
Share this answer
 
Comments
EDITH GINGRAS 24-Oct-10 11:32am    
ok i get what you are saying but i forgot to add that part of the program to the list i will post it now it is the first loop information then followed by the rest that is already posted sorry so now that you see that I have this part how will I get the results of student average and class average out of it? thanks


cout << "Enter the number of students: ";
cin >> numstudent;

cout << "Enter the number of grades: ";
cin >> numgrade;
Quote: " how will I get the results of student average and class average out of it?"

Please re-read my previous post, I cannot make it much clearer than this.

Pseudo code
classaverage = 0
classcount = 0
for each student
{
    studentaverage = 0
    gradecount = 0
    for each grade
    {
        studentaverage = studentaverage + grade
        gradecount = gradecount + 1
    }
    classaverage = classaverage + studentaverage
    classcount = classcount + gradecount

    studentaverage = studentaverage / gradecount
    print studentname, studentaverage
}
classaverage = classaverage / classcount
print "Class average", classaverage
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900