I'm trying to write a function that will build an array for me. But I know it doesnt work because when I print part of the array it comes out with a large (9 digit?) number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int 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
int grade;
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 >> grade;
grade=grades[i][j];
}
}
return grades[i][j];
}
This is my first time working with 2-d arrays, so I think that is the problem. I'm also not sure if I'm passing it to my main correctly.
I'm still not sure if mine is working properly though. Here's my tentative main where I'm trying to see grades[0][0];
Skip down to line 42 where I try to cout. It doesnt print anything, so I'm a bit confused.
#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){
cout << grades[0][0];
}
elseif(choice==3 && flag==0){
cout << "No scores stored yet." <<endl;
}
elseif(choice==3 && flag==1){
}
}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
int grade;
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 >> grade;
grades[i][j]=grade;
}
}
}