Sorting 2D Arrays

Sorry Im new in this forum. I have an assignment due tonight that I have display student ID's and grades for those ID's.
The problem I am having is that when I sort by grades the ID's dont stay matching the grades(also the same for when I sort by ID's). Any help would be greatly appreciated.


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

const int NUMBER_OF_STUDENTS = 10, NUMBER_OF_QUIZZES = 1;
const int MAX_SCORE = 100, MIN_SCORE = 0;
const int MAX_ID = 200, MIN_ID = 1;

void randomInitArray(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int MIN_SCORE, int MAX_SCORE, int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int MIN_ID, int MAX_ID);

void displayGrade(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]);

void sortingGrade(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]);
void sortingId(int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]);


int main(){
	int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES];
	int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES];

	srand(unsigned(time(NULL)));
	
//INITIALIZE ARRAY
	randomInitArray(grade, MIN_SCORE, MAX_SCORE, id, MIN_ID, MAX_ID);
	cout << "Before sorting: " << endl;

//DISPLAY GRADE THE FIRST TIME
	displayGrade(grade, id);
	cout << endl;

//SORTING BY GRADE
	sortingGrade(grade);
	cout << "After sorting by GRADE: " << endl;

//DISPLAY BY SORTED GRADE
	displayGrade(grade, id);
	cout << endl;

//SORTING BY ID
	sortingId(id);
	cout << "After sorting by ID: " << endl;

//DISPLAY BY SORTED ID
	displayGrade(grade, id);
	cout << endl;

	system("pause");
	return 0;
}

void randomInitArray(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int MIN_SCORE, int MAX_SCORE, int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int MIN_ID, int MAX_ID){
	for (int stNum = 1; stNum <= NUMBER_OF_STUDENTS; stNum++){
	  for (int quizNum = 1; quizNum <= NUMBER_OF_QUIZZES; quizNum++){
	    id[stNum - 1][quizNum - 1] = rand() % (MAX_ID - MIN_ID + 1) - MIN_ID;
	    grade[stNum - 1][quizNum - 1] = rand() % (MAX_SCORE - MIN_SCORE + 1) - MIN_SCORE;
		}
	}
}

void displayGrade(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES], int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]){
	cout.setf(ios::fixed | ios::showpoint);
	cout.precision(4);

	cout << setw(10) << "Student ID"
		<< setw(15) << "Quiz Grade\n";

	for (int stNum = 1; stNum <= NUMBER_OF_STUDENTS; stNum++){
	  for (int quizNum = 1; quizNum <= NUMBER_OF_QUIZZES; quizNum++){
	    cout << setw(10) << id[stNum - 1][quizNum - 1];
	    cout << setw(10) << grade[stNum - 1][quizNum - 1];
		}
	cout << endl;
	}
}

void sortingGrade(int grade[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]){
	int temp;
	for (int x = 0; x < NUMBER_OF_STUDENTS; x++){
	  for (int y = 0; y < NUMBER_OF_QUIZZES; y++){
	    for (int stNum = 0; stNum <= NUMBER_OF_STUDENTS; stNum++){
	      for (int quizNum = 0; quizNum <= NUMBER_OF_QUIZZES; quizNum++){
		if (grade[stNum - 1][quizNum - 1] > grade[x][y]){
			temp = grade[x][y];
			grade[x][y] = grade[stNum - 1][quizNum - 1];
			grade[stNum - 1][quizNum - 1] = temp;
					}
				}
			}
		}
	}
}

void sortingId(int id[NUMBER_OF_STUDENTS][NUMBER_OF_QUIZZES]){
	int temp;
	for (int a = 0; a < NUMBER_OF_STUDENTS; a++){
          for (int b = 0; b < NUMBER_OF_QUIZZES; b++){
	    for (int stNum = 0; stNum <= NUMBER_OF_STUDENTS; stNum++){
	       for (int quizNum = 0; quizNum <= NUMBER_OF_QUIZZES; quizNum++){
		  if (id[stNum - 1][quizNum - 1] > id[a][b]){	
			  temp = id[a][b];
	   		  id[a][b] = id[stNum - 1][quizNum - 1];
			  id[stNum - 1][quizNum - 1] = temp;
					}
				}
			}
		}
	}
}
Last edited on
Don't make unnecessary function arguments.

I would put all the code in main to make it more shorter, and more easier to navigate through, but ok...

If you have the freedom of using the standard library, I would use a vector of student objects, then overload the less-than operator using a sort predicate for both id and grades, so that you can use the sort function in <algorithm>. Because your code looks awfully like C code.

Otherwise, the actual problem is that you are not using references in your functions, and you are modifying a copy.
Topic archived. No new replies allowed.