I don't know what is wrong with this array builder

closed account (9hv4jE8b)
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.

Thanks guys.

Reverse the assignment in line 10.

grades[i][j] = grade;

Since you are passing the array grades by reference,
you will be updating the array directly.

So no need to return any value (last students project #8 grade).
You could remove line 13 and change the returning int to void in line 1.

void collect_data (int grades[][8], int Num_of_Students)

And you should be all set.






Last edited on
closed account (9hv4jE8b)
Oh ok, I see what you did.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream>
#include<cmath>

using namespace 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;}
                
             else if(choice==2 && flag==0){
                  cout << "No scores stored yet." <<endl;
                  }
             else if(choice==2 && flag==1){
                  cout << grades[0][0];
                  }
             else if(choice==3 && flag==0){
                  cout << "No scores stored yet." <<endl;
                  }
             else if(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;
            }
    } 

}  
  
closed account (9hv4jE8b)
nvm it's working
Topic archived. No new replies allowed.