Program to swap a 2-dimensional array based on input of grades

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.
Topic archived. No new replies allowed.