Sort Function won't work

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>
#include <algorithm>

using namespace std;

const int s = 12, p = 6;
int x[s][p], size = 3;

string randstr();
void menu ();
void displayTable(int [][p], int);
void sort(int [][p], int);

int main()
{
	int choice;
	
	displayTable(x, s);
	do
	{
		menu();
		cout << "\n\nEnter desired output [1 - 4]: ";
		cin >> choice;
		
		if(choice == 1)
		{
		}
		if(choice == 2)
		{
			sort(x, s);
			displayTable(x, s);
			
		}
		if(choice == 3)
		{
			sort(x, s);
			displayTable(x, s);
		}
		if(choice == 4)
		{
			exit(0);
		}
	}
	while (choice != 4);
	return 0;
	
}

string randstr()
{
	int L = 3;
	string str = "    ";
	for(int c = 0; c < L; c++)
	{
		str[c] = 65 + rand() % 26;
	}
	return str;
	
}

void menu()
{
	int selection;
	cout << "\n1: Sort Alphebeltically"
		 << "\n2: Sort Grades Increasing Order (Student)"
		 << "\n3: Sort Grades Increasing Order (Project)"
		 << "\n4: End Program";
}

void displayTable(int xt[][p], int student)
{
	string sname;
	double total, total2;
	
	unsigned seed = time(0);
	srand(seed);
 	
	cout << fixed << setprecision(2);
	
	cout << "Names:\t\t";
	for (int count = 1; count < 7; count++)
	{
		cout << count << "\t";
	}
	cout << "AVG";
	cout << endl << endl;
	for (int i = 0; i < student; i++)
	{
		for (int j = 0; j < p; j++)
		{
			xt[i][j] = 0 + rand() % 101;
		}
	}
	for (int i = 0; i < student; i++)
	{
		sname = randstr();
		cout << sname << "            ";
		for(int j = 0; j < p; j++)
		{
			cout << xt[i][j] << "\t";
		}
		for (int j = 0; j < p; j++)
		{
			total += xt[i][j];
			if(j == (p-1))
			{
				total = total/p;
			}
		}
		cout << total
			 << endl;
		total = 0;
	}
	
	cout << "AVG:            ";
	for (int j = 0; j < p; j++)
	{
		for (int i = 0; i < student; i++)
		{
			total2 += xt[i][j];
			if(i == (student-1))
			{
				total2 = total2/student;
			}
		}
		cout << total2 << "\t";
		total2 = 0;
	}
	cout << endl;
	
}

void sort (int xs[][p], int sort_s)
{
	bool check = true;
	for (int i = 0; i < sort_s; i++)
	{
		for (int j = 0; j < p; j++)
		{
			int m = i, n = j + 1;
			while(check == true)
			{
				if (n == p)
				{
					n = 0;
					m++;
					if (m == sort_s)
					{
						check = false;
						break;
					}
				}
				if(xs[i][j] > xs[m][n])
				{
					swap(xs[i][j], xs[m][n]);
					n++;
				}
				
			}
		}
	}
}


Names:          1       2       3       4       5       6       AVG

XID             80      33      17      23      19      78      41.67
SVZ             91      15      82      71      41      4       50.67
WEH             20      31      32      92      3       48      37.67
IJM             20      92      76      21      62      68      56.50
WCY             52      30      33      9       32      60      36.00
OCP             88      79      56      66      87      96      78.67
DEF             83      61      90      9       50      37      55.00
ZIZ             69      20      91      74      98      25      62.83
VNB             83      7       84      39      44      1       43.00
KPL             80      70      93      8       27      72      58.33
SNT             41      0       75      39      22      20      32.83
NCI             70      5       42      47      1       60      37.50
AVG:            64.75   36.92   64.25   41.50   40.50   47.42

1: Sort Alphebeltically
2: Sort Grades Increasing Order (Student)
3: Sort Grades Increasing Order (Project)
4: End Program

Enter desired output [1 - 4]: 2


Sort function won't work? What's wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
			while(check == true)
			{
				if (n == p)
				{
					n = 0;
					m++;
					if (m == sort_s)
					{
						check = false;
						break;
					}
				}
				if(xs[i][j] > xs[m][n])
				{
					swap(xs[i][j], xs[m][n]);
					n++;
				}
				
			}


And what happens if neither of those if statements trigger?

Also, switch statements are your friend for menus. Or at least use if/else, not just if.
Topic archived. No new replies allowed.