Sorting a 2D array?

I am trying to sort an 2-Dimensional array, but I want to sort it based on the greatest number in a certain column of that array, and depending on what row that number is in, move the WHOLE row to the top(Sorry if this is too vague, I can try to explain better if anyone has trouble).

My problem: I keep getting a segmentation fault, and I know that means its trying to write information where it doesn't have access to it, however I have no clue how to fix that.
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sort(double grade[50][7], int dec)
{
    int  i, j, numberofstudents;
    double temp;

    
    for(i = 0; i < numberofstudents - 1; i++)
    {	    
	for(j = 0; j < 6; j++)
	{ 
		if(grade[i][dec] < grade[i+1][dec])
		{
		    temp = grade[i][j];
		    grade[i][j] = grade[i+1][j];
		    grade[i+1][j] = temp;
		}
	}
    }
}


for example: if the array looks like:

{1100 90 60 70 90 50 70
2200 100 50 60 50 70 50
1000 95 60 90 50 60 69}

and I want to sort it by index 1(2nd column), then it will look like this:

{2200 100 50 60 50 70 50
1000 95 60 90 50 60 69
1100 90 60 70 90 50 70}
Last edited on
Where do you append something to numberofstudents? Nowhere. So you should first append a value to numberofstudents
Last edited on
Yeah, number of students is an unknown. As an uninitialized variable, it will have any value from −2,147,483,648 to 2,147,483,647.
I guess it's his/her second try to get an answer...

Meecrob wrote:
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include<cstdlib>

using namespace std;

void sort(double grade[50][7], int dec);


int main()
{
    double grade[50][7], average, total;
    int numberofstudents, i, j;
    char input;
  
    //Input number of students
    cin >> numberofstudents;

    for(i = 0; i < numberofstudents; i++)
    {
	for(j = 0; j < 6; j++)
	{ 

            //Input Student I.D. first, then grades of each student
	    cin >> grade[i][j];
	 	    
	}
    }
    //Average the scores for an individual student's quiz grades
    for(i = 0; i < numberofstudents; i++)
    {
	for(j = 1; j < 6; j++)
	{
	    total += grade[i][j]; 
	}
	average = total/5.0;
	grade[i][6] = average;
	total = 0;
    }

    cout << "StudentID" << "      " << "Quiz1" << "\t" << "Quiz2" << "\t"
	 << "Quiz 3" << "\t" << "Quiz4" << "\t" << "Quiz5" << "\t"
	 << "Average" << endl;

    //Read out the grade array
    for(i = 0; i < numberofstudents; i++)
    {
	for(j = 0; j < 7; j++)
	{
	    
	    cout << grade[i][j] << "\t";
	}
	cout << endl;
    }

    //Average all the individual quizzes together
    cout << "Quiz Average";
    for(j = 1; j < 6; j++)
    {
	for(i = 0; i < numberofstudents; i++)
	{
	    total += grade[i][j];
	}
	cout << "\t" << total/numberofstudents;
	total = 0;
    }
    cout << endl;
	    
	    
    //Read in an Input, Sorts the files using inputs
    cout << "Enter in a character to sort the array(S, A, B, C, D, E, F, T): " ;
    cin >> input;
    while(input != 'T')
    {
	switch(input)
	{
	    //Sort by ID
	case 'S':
	    sort(grade, 0);
	    break;

	    //Sort by Quiz 1
	case 'A':
	    sort(grade, 1);
	    break;

	    //Sort by Quiz 2
	case 'B':
	    sort(grade, 2);
	    break;

	    //Sort by Quiz 3
	case 'C':
	    sort(grade, 3);
	    break;

	    //Sort by Quiz 4
	case 'D':
	    sort(grade, 4);
	    break;

	    //Sort by Quiz 5
	case 'E':
	    sort(grade, 5);
	    break;

	    //Sort by Averages
	case 'F':
	    sort(grade, 6);
	    break;
	default:
	    cout << "Please Press a Valid Input: ";
	    cin >> input;
	    break;
	}
   
    }
    return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void sort(double grade[50][7], int dec)
{
    int  i, j, numberofstudents;// blah
    double temp;

	for(j = 0; j < 7; j++)
	{
	    for(i = 0; i < numberofstudents - 1; i++)
	    {

		if(grade[i][dec] < grade[i+1][dec])
		{
		    temp = grade[i][j];
		    grade[i][j] = grade[i+1][j];
		    grade[i+1][j] = temp;
		}
		cout << grade << endl;
	}
    }
}



The point of this code is to input the amount of students in a class, then input a individuals students I.D. and grade and so on until every student's information has been placed, then it will be sorted based on the column of the array,
My problem is I keep getting a Segmentation Fault whenever I try to debug it, and I can't figure out what is wrong with my bounds of my sort function.
@meecrob: You have to declare numberofstudents either as a global variable (write it outside the main()-function) or hand it over to sort().
I made your input stuff more clear:

(l. 15 - 66)
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
    cout<<"Input number of students: ";
    cin >> numberofstudents;

    for(i = 0; i < numberofstudents; i++)
    {
		cout<<"Input grades of student "<<i+1<<": "<<endl;
		for(j = 0; j < 6; j++)
		{
		    cin >> grade[i][j];
		}
    }
    //Average the scores for an individual student's quiz grades
    for(i = 0; i < numberofstudents; i++)
    {
		for(j = 1; j < 6; j++)
		{
		    total += grade[i][j]; 
		}
		average = total/5.0;
		grade[i][6] = average;
		total = 0;
    }

    cout << "Student " << "Quiz1" << "\t" << "Quiz2" << "\t"
	 << "Quiz 3" << "\t" << "Quiz4" << "\t" << "Quiz5" << "\t"
	 << "Average" << endl;

    //Read out the grade array
    for(i = 0; i < numberofstudents; i++)
    {
		cout<<i+1<<"\t";
		
		for(j = 0; j < 6; j++)
		{
		    
		    cout << grade[i][j] << "\t";
		}
		cout << endl;
    }

    //Average all the individual quizzes together
    cout << endl << "Average";
    for(j = 1; j < 6; j++)
    {
		for(i = 0; i < numberofstudents; i++)
		{
		    total += grade[i][j];
		}
		cout << "\t" << total/numberofstudents;
		total = 0;
    }
    cout << endl;
Last edited on
Sorry for that, yes i only gave a fragment of my code initially, as I got no replies on my other post which I thought might have been too long for people. I input "numberofstudents" in the beginning of my program, so thats where that comes from in my sort function. But again, is there something wrong with my limits in my sort function?

@FlashDrive, my first input in my grade array is a student's ID number(any random number basically), followed by that students 5 quiz grades, then the next students ID number and there following quiz grades, and so on. Quick question: After linking my code, did you not see where I inputed "numberofstudents"?
Last edited on
Sorry for my delayed response everyone!
Topic archived. No new replies allowed.