HELP !!! Store Random Numbers and Sort Function

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

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

void values(int [][p], const int);
void displaytable(int [][p], const int);
void sort(int [][p], const int);
string randstr(const int);
void menu();

int main()
{
	int choice;
	
	values(x, s);
	displaytable(x, s);
	do
	{
		menu();
		cout << "Enter choice: ";
		cin >> choice;
		if(choice == 2)
		{
			sort(x, s);
			displaytable(x, s);
		}
	}
	while(choice != 4);
	return 0;
}

void values(int x0[][p], const int s0)
{
	for (int i = 0; i < s0; i++)
	{
		for (int j = 0; j < p; j++)
		{
			x0[i][j] = 0 + rand() % 101;
		}
	}
	
}
void displaytable(int x1[][p], const int s1)
{
	cout << fixed << setprecision(2);
	string sname;
	double total, total2;
	cout << "Names:\t\t";
	for(int count = 1; count < 7; count++)
	{
		cout << "P" << count << "\t";
	}
	cout << "AVER."
		 << "\n\n";
	for(int i = 0; i < s1; i++)
	{
		sname = randstr(s);
		cout << sname << "           ";
		for(int j = 0; j < p; j++)
		{
			cout << x1[i][j] << "\t";
			
			total += x1[i][j];
			if (j == (p-1))
			{
				total = total/p;
				cout << total;
				cout << endl;
			}
		}
	}
	cout << "AVER.           ";
	for (int j = 0; j < p; j++)
	{
		for(int i = 0; i < s1; i++)
		{
			total2 += x1[i][j];
			if (i == (s1-1))
			{
				total2 = total2/s1;
				cout << total2 << "\t";
			}
		}
	}
	cout << endl;
}
string randstr(const int s)
{
	int L = 3;
	string name = "     ";
	for (int count = 0; count < s; count++)
	{
		name[count] = 65 + rand() % 26;
	}
	return name;
}
void menu()
{
	cout << "\n1: Sort Alphebeltically"
		 << "\n2: Sort Grades Increasing Order (Student)"
		 << "\n3: Sort Grades Increasing Order (Project)"
		 << "\n4: End Program";
}

void sort (int xs[][p], const int sort_s)
{
	int temp;
	for (int i = 0; i < sort_s; i++)
	{
		for (int j = 0; j < p; j++)
		{
			if(xs[i][j] > xs[i][j+1])
			{
				temp = xs[i][j];
				xs[i][j] = xs[i][j+1];
				xs[i][j+1] = temp;
			}
		}
	}
}


Names:          P1      P2      P3      P4      P5      P6      AVER.

EVIKE           41      85      72      38      80      69      64.17
KKASV           65      68      96      22      49      67      71.86
YCXFX           51      61      63      87      66      24      70.64
FADPO           80      83      71      60      64      52      80.11
OEJUV           90      60      49      31      23      99      72.02
POEYL           94      11      25      24      51      15      48.67
VRVIP           13      39      67      97      19      76      59.94
QNQRQ           12      33      99      18      92      35      58.16
OOVAO           74      0       95      71      39      33      61.69
NCBXC           39      32      37      45      57      71      57.12
ATXDK           95      5       71      24      86      8       57.69
IXJSW           51      54      74      24      75      70      67.61
AVER.           58.75   49.15   72.35   51.11   62.68   56.81

1: Sort Alphebeltically
2: Sort Grades Increasing Order (Student)
3: Sort Grades Increasing Order (Project)
4: End ProgramEnter choice: 2
Names:          P1      P2      P3      P4      P5      P6      AVER.

XXZRZ           41      72      38      80      69      65      60.83
OKETL           68      85      22      49      67      51      67.14
GZQRC           61      63      87      66      24      80      74.69
OJWAY           83      71      60      64      52      90      82.45
PSAJL           60      49      31      23      96      94      72.57
AOVLZ           11      25      24      51      15      13      35.26
CPWSR           39      67      97      19      76      12      57.54
IZCOB           33      99      18      92      35      74      68.09
IJTVD           0       95      71      39      33      39      57.52
LDVGY           32      37      45      57      71      95      65.75
MBORX           5       71      24      86      8       51      51.79
XOHGM           54      74      24      75      70      0       58.13
AVER.           40.58   70.72   50.98   62.66   56.56   60.05

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


Help me figure out why my sort is not working. Also, I want to keep the same random numbers for the continuation of the program, I don't want new randomized values when I display the table. Help me !!
You have numerous problems with your program:
1) your sort function accesses out of bounds:
1
2
for (int j = 0; j < p; j++){
    if(xs[i][j] > xs[i][j+1])
Whan j = p - 1, this code will try to access xs[i][p] which is out of bounds access.
2) You are making only a single pass of bubble sort which is not enough to actually sort contents.
What do you mean by single pass? I see the out of bounds error you just mentioned. What can I do to sort the entire 2D array?
What do you mean by single pass?
I posted part of code related to sorting single row. You are passing each row once. That means if some characters are not in place when it finishes. They will remain here. Bubble sort is N2 algorithm so generally you need to make p passes for array of size p.

However if you are not reqired to actually implement your own sorting function, use std::sort:
1
2
3
for (int i = 0; i < sort_s; i++) { //for each row
    std::sort(xs[i], xs[i] + p); //Sort it
}
Okay got it. Thanks !
Topic archived. No new replies allowed.