I earlier made a 2-d array called grades[][] and now I'm trying to find out the average on a specific column of the array (i.e. project 1 that has Num_of_Students grades in it).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
float compute_ave(int grades[][8], int Num_of_Students)
{
int sum=0;
int j; //project number
float average;
cout << "Give the number of the project: ";
cin >> j;
for(int i=0;i<Num_of_Students;i++){
sum+=grades[i][j];
}
average=sum/Num_of_Students;
return average;
}
But this outputs some crazy 1.39e+008 and I dont know why..
That's just the format. If you want the whole thing, you have to write that into your code. I'm not sure, but I think adding cout.setf (ios :: fixed, ios :: floatfield) ; cout.precision (9) ; before you output average should work.
hanst, I build the array from a nested for loop. heres my whole (tentative) code. Does it look like part of my array is messed up which is causing this? (collect_data is the array builder func)
#include<iostream>
#include<cmath>
usingnamespace std;
void collect_data (int grades[][8], int Num_of_Students); //Gets data to store in array
float compute_ave(int grades[][8], int Num_of_Students); //Prototype for computing the average of a specific exam
float class_average(int grades[][8], int Num_of_Students);
int main()
{
cout << "Student Exam Database\nThis program stores the exam scores\n";
cout << "of a number of students and produces\nthe average and the letter grades." << endl << endl;
int choice; //for picking option
int flag=0; //because you must input data first
int Num_of_Students; //Number of students in class
int grade;
int grades[][8]={}; //the empty array
do{
cout << "Please choose an option." << endl;
cout << "1. To store students exams." << endl;
cout << "2. To compute class average on a specific exam." << endl;
cout << "3. To compute overall class average in the course." << endl;
cout << "4. To see the letter grade of a specific student." << endl;
cout << "Please choose: ";
cin >> choice;
if (choice==1 && flag==0){
cout << "Please give number of students: " <<endl;
cin >> Num_of_Students;
collect_data(grades, Num_of_Students);
flag=1;}
elseif(choice==2 && flag==0){
cout << "No scores stored yet." <<endl;
}
elseif(choice==2 && flag==1){
float average;
average=compute_ave(grades, Num_of_Students);
cout << average;
}
elseif(choice==3 && flag==0){
cout << "No scores stored yet." <<endl;
}
elseif(choice==3 && flag==1){
float totalaverage;
totalaverage=class_average(grades, Num_of_Students);
cout << totalaverage << endl;
}
}while(choice!=0);
system("pause");
}
void collect_data (int grades[][8], int Num_of_Students) //a nested for loop that builds a 2-D array
{ int j; //counter for which project it is
int i; //counter for which student
for(j=0;j<8;j++){
for(i=0;i<Num_of_Students;i++){
cout << "Please give grade of student " << i+1 << " in project " <<j+1 << ": ";
cin >> grades[i][j];
}
}
}
float compute_ave(int grades[][8], int Num_of_Students)
{
int sum=0;
int project_number;
int j; //project number adjusted for array
float average;
cout << "Give the number of the project: ";
cin >> project_number;
j=project_number-1; //i.e. project "1" is grades[i][0]
for(int i=0;i<Num_of_Students;i++){
sum+=grades[i][j];
}
average=sum/Num_of_Students;
return average;
}
I`m not absolutely sure but:
L21 I don`t think the array is initialized to 0 for each element when [ ] is empty. If this was a single dimensional array like grades[8]={} all elements would be initialized to 0.
...and why use a 2D array in any case?
L72 why are you incrementing i and j in the body of the i for loop?
Ok I'll see what I can do about initializing a 2-d array to zero...
I'm using a 2-d array because my professor is making us.
As for L72, that's just a cout syntax bc i=0 is student 1 and j=0 is project one. So to get it to read out correctly i had to put i+1, j+1.
My for loop acts in such a way that grades[0][0], grades[1][0], grades[2][0].....etc til it reaches num of students, then goes to the second project.