Inputting Data Into a 2D Array

I'm working on a program, part of which requires you to input grades for several students, which must be displayed as an array. I have it where the grades are entered like you would if there was no array, and then displayed them all as an array. However, whenever I run the program, it just regurgitates the list back. Is it possible to do it this way or do I have to enter the grades directly as an array? My teacher doesn't like to answer too many questions and I've been unable to find anything on the Internet.

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
#include<iostream>
#include<iomanip>
using namespace std;

main(){
    int array[3][4];

    //inputs the grades
    for(int i=0; i<3; i++){
        for(int x=0; x<4; x++){
            cin>>array[i][x]; 
        }
    }

    //displays the input grades as an array
    for(int i=0; i<3; i++){
        for(int x=0; x<4; x++){
            cout<<array[i][x]<<"    ";
            if(x<4){
                cout<<endl;
            }
            else{
                cout<<"";
            }
        }
    }

}
Is it possible to do it this way or do I have to enter the grades directly as an array?

Do you mean something like this:

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
Enter the grades: (3 rows) x (4 columns)

Row 1
     Col 1: 20
     Col 2: 50
     Col 3: 60
     Col 4: 30

Row 2
     Col 1: 50
     Col 2: 80
     Col 3: 60
     Col 4: 70

Row 3
     Col 1: 20
     Col 2: 60
     Col 3: 30
     Col 4: 40

Display as an array: (3 rows) x (4 columns)

   20     50     60     30  
   50     80     60     70  
   20     60     30     40
Yes. Exactly like that.
The code bellow works as in my previous post. Write now the final part to display the entered array. Good luck!

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
#include<iostream>
#include<iomanip>
using namespace std;

int main(){
    int array[3][4];

    //inputs the grades
    std::cout << "\nEnter the grades: (3 rows) x (4 columns)\n\n"; 
	for (int i=0; i < 3; i++)
	{
		std::cout << "Row " << i + 1 << '\n';
		
		for (int j=0; j < 4; j++)
		{
			std::cout << "     Col " << j + 1 << ": ";
			std::cin >> array[i][j]; 
		}
		std::cout <<'\n';
	}
	
	//displays the input grades as an array
         
//    ....here, try to modify your code to display data as an array...
    
	return 0;
}

Last edited on
You can try this program.

Change SizeOfArray to equal the number of students you are inputting.

If there are more than 2 grades for each student then you need to create an array for each grade.

You will also need to add extra cout and cin for each grade.

You will need to add to the final cout if you are entering more tests.

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
#include <iostream>

using namespace std;

int main ()
{
    const int SizeOfArray = 4;//This will be the number of students grades you are entering
    const int rows = SizeOfArray;//this line is not necessary and just used to help follow the program
    
    int Grade1 [SizeOfArray];
    int Grade2 [SizeOfArray];
    
    //This is you assigning the test results for each student.
    for (int i = 0; i < rows; i++)
    {
        cout << "Please enter student " << i+1 << ", 1st grade: ";
        cin >> Grade1 [i];
        cout << "Please enter student " << i+1 << ", 2nd grade: ";
        cin >> Grade2 [i];
    }
    
    //this outputs the results of each student. each row represents a different student's results.
    for (int i = 0; i < rows; i++)
    {
        cout << Grade1 [i] << "\t" << Grade2 [i] << endl;
    }
    
    int x;
    cin >> x;  
    return 0;
}
That helped a ton, condor. Thanks so much!
Topic archived. No new replies allowed.