Bubble sort not working
Apr 22, 2018 at 11:22pm UTC
The 'bubbleSort' function is meant to sort all the rows in the 'studentRecords' 2D array. However, it only sorts the 1st and 3rd rows.
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 167 168 169 170 171 172 173 174 175
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
class Functions
{
private :
int menuChoice=0;
int studentRecords[4][5];
int studentID=0;
int x=0;
public :
void arrayFill ()
{
studentRecords[0][0]=1;
studentRecords[1][0]=2;
studentRecords[2][0]=3;
studentRecords[3][0]=4;
//srand(time(0));
for (int j=0;j<5;j++)
{
for (int i=1;i<5;i++)
{
studentRecords[j][i]=(rand()%100)+1;
}
}
}
void viewAllRecords ()
{
cout << "| ID | Quiz 1 | Quiz 2 | Midterm | Final |" << endl;
for (int j=0;j<4;j++)
{
for (int i=0;i<5;i++)
{
cout << " " << studentRecords[j][i] << "\t" ;
}
cout << endl;
}
cout << endl;
menu();
}
void idSearch ()
{
cout << "Enter the student ID: " ;
cin >> studentID;
cout << endl;
cout << "| ID | Quiz 1 | Quiz 2 | Midterm | Final |" << endl;
switch (studentID)
{
case 1:
for (int j=0;j<5;j++)
{
cout << " " << studentRecords[0][j] << "\t" ;
}
break ;
case 2:
for (int j=0;j<5;j++)
{
cout << " " << studentRecords[1][j] << "\t" ;
}
break ;
case 3:
for (int j=0;j<5;j++)
{
cout << " " << studentRecords[2][j] << "\t" ;
}
break ;
case 4:
for (int j=0;j<5;j++)
{
cout << " " << studentRecords[3][j] << "\t" ;
}
break ;
}
cout << endl << endl;
menu();
}
void bubbleSort ()
{
for (int j=0;j<4;j++)
{
for (int i=1;i<4;i++)
{
if (studentRecords[j][i] < studentRecords[j][i+1])
{
x=studentRecords[j][i];
studentRecords[j][i]=studentRecords[j][i+1];
studentRecords[j][i+1]=x;
}
}
}
cout << "| ID | Quiz 1 | Quiz 2 | Midterm | Final |" << endl;
for (int j=0;j<4;j++)
{
for (int i=0;i<5;i++)
{
cout << " " << studentRecords[j][i] << "\t" ;
}
cout << endl;
}
cout << endl;
menu();
}
void menu ()
{
cout << "1. View all student records" << endl;
cout << "2. View individual student record" << endl;
cout << "3. Show highest and lowest final scores" << endl << endl;
cout << "Enter the number of the menu option you want to proceed with: " ;
cin >> menuChoice;
cout << endl;
while (menuChoice!=4)
{
switch (menuChoice)
{
case 1:
viewAllRecords();
break ;
case 2:
idSearch();
break ;
case 3:
bubbleSort();
break ;
}
}
}
};
int main()
{
Functions program;
program.arrayFill();
program.menu();
return 0;
}
Apr 23, 2018 at 1:02am UTC
You need yet another loop outside of the usual double loop for bubble sort. The outermost loop would go through the row (leftmost) index values. The two inner loop indices are for the bubble sort on the current row.
Topic archived. No new replies allowed.