I cannot sort the arrays. Can someone help me with that?

This program stores student roll-numbers and their marks in two separate arrays. Their roll-numbers are being stored in the array. If I type in the roll-numbers randomly, I want the program to arrange the roll-numbers in an ascending order. How can I do that?

It took me 4 days to make the program below and now my brain has refused to co-operate. Just one last step left and that is if I type the roll-numbers randomly, the program should arrange them in an ascending order and print it out.

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
#include <iostream>
using namespace std;
int main()
{
	int number=0;
	int intRollNumber;
	float fltMarks;
	int arrintStudents[60];
	float arrfltMarks[60];
	int intArrayIndex = 0;
	
	do
	{
		cout<<"1. Insert student data"<<endl;
		cout<<"2. Show the record of the student"<<endl;
		cout<<"3. Show the complete data"<<endl;
		cout<<"4. Delete a student data (Please enter the roll number to be deleted)"<<endl;
		cout<<"5. Exit"<<endl<<endl;
		
		cin >> number;
		
		if (number == 1)
		{
			cout << "Enter Roll Number:";
			cin >> intRollNumber;
			cout << "Enter Marks:";
			cin >> fltMarks;
			
			cout<<endl;
			
			arrintStudents[intArrayIndex] = intRollNumber;
			arrfltMarks[intArrayIndex] = fltMarks;
			
			intArrayIndex++;
		}
		else
		{
			if (number == 2)
			{
				// SHOW STUDENT RECORD
				int intIndex = 0;
				cout << "Enter Roll Number:";
				cin >> intRollNumber;
				
				while(arrintStudents[intIndex] != intRollNumber)
				{
					intIndex++;
				}
				
				if (intIndex < intArrayIndex)
				{
					cout << arrintStudents[intIndex] << " : " << arrfltMarks[intIndex] << endl;
				}
				else
				{
					cout << "Error!";
				}				
			}
			else
			{
			 
				if (number == 3)
				{
					// SHOW COMPLETE RECORDs
					int intCounter = 0;
					for (intCounter = 0; intCounter < intArrayIndex; intCounter++)
					{
						cout << arrintStudents[intCounter] << " : " << arrfltMarks[intCounter] << endl;
					}
					cout << "DONE!"<<endl<<endl;
				}
				else
				{
					if (number == 4)
					{
						// DELETE STUDENT RECORD
						int intIndex = 0;
						cout << "Enter Roll Number:";
						cin >> intRollNumber;
					
						while(arrintStudents[intIndex] != intRollNumber)
						{
							intIndex++;
						}
						if (intIndex < intArrayIndex)
						{
							while(intIndex < intArrayIndex)
							{
								arrintStudents[intIndex] = arrintStudents[intIndex + 1];
								arrfltMarks[intIndex] = arrfltMarks[intIndex + 1];
								intIndex++;
							}
							intArrayIndex--;
						}
						else
						{
							cout << "Error!";
						}		
					}
					else
					{
						//INVALID NUMBER
						cout << "Invalid Number!"<<endl;
					}
				}
			}
		}
	}
	while(number != 5);
	
	return 0;	
}
Just add to your select menu 5th option "sort" and insert in last else
1
2
3
4
5
else
{
	//INVALID NUMBER
	cout << "Invalid Number!" << endl;
}


this code
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
if (number == 5)
{
	int tmpStudent = 0;
	int tmpMark = 0;
	// SORT ROLL NUMBERS (ASCENDING)
	for (int i = 0; i < intArrayIndex; i++)
		for (int j = 0; j < intArrayIndex; j++)
		{
			if (arrintStudents[j] > arrintStudents[i]) {
				tmpStudent = arrintStudents[i];
				arrintStudents[i] = arrintStudents[j];
				arrintStudents[j] = tmpStudent;

				tmpMark = arrfltMarks[i];
				arrfltMarks[i] = arrfltMarks[j];
				arrfltMarks[j] = tmpMark;
			}
										
		}

}
else
{
	//INVALID NUMBER
	cout << "Invalid Number!" << endl;
}
Forgive me!
I forgot to mention that I cannot add another option, what you wrote above is what I'm supposed to do in the 3rd option.
If I type in 3 the program should print out the sorted data.
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
if (number == 3)
{
	int intCounter = 0; 
	int tmpStudent = 0;
	int tmpMark = 0;
	// SORT ROLL NUMBERS (ASCENDING)
	for (int i = 0; i < intArrayIndex; i++)
		for (int j = 0; j < intArrayIndex; j++)
		{
			if (arrintStudents[j] > arrintStudents[i]) {
				tmpStudent = arrintStudents[i];
				arrintStudents[i] = arrintStudents[j];
				arrintStudents[j] = tmpStudent;

				tmpMark = arrfltMarks[i];
				arrfltMarks[i] = arrfltMarks[j];
				arrfltMarks[j] = tmpMark;
			}

		}
	// SHOW COMPLETE RECORDs
	for (intCounter = 0; intCounter < intArrayIndex; intCounter++)
	{
		cout << arrintStudents[intCounter] << " : " << arrfltMarks[intCounter] << endl;
	}
	cout << "DONE!" << endl << endl;
}
Last edited on
Thanks maleman777. God bless ya
Topic archived. No new replies allowed.