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
|
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
const int ROWS = 10;
const int COLUMNS = 2;
int person[ROWS][COLUMNS] =
{
{ 1,100 },
{ 2,20 },
{ 3,80 },
{ 4,10 },
{ 5,50 },
{ 6,150 },
{ 7,30 },
{ 8,60 },
{ 9,40 },
{ 10,1 }
};
for (int i = 0; i < ROWS; ++i)
{
cout << "Person " << person[i][0] << " ate: " << person[i][1] << " pancakes";
{
cout << endl;
}
}
// Sort lowest to highest
int loop = 1;
int count;
int limit = ROWS;
for (int j = ROWS - 1; j > 0; --j, loop++, limit--)
{
cout << "Loop " << loop << endl;
cout << "Loop limit: " << limit << endl;
count = 0;
for (int i = 0; i < ROWS; ++i, ++count )
{
if (loop + count == ROWS + 1)// skips uneccessary checks
{
break;
}
if (person[i][1] > person[i + 1][1] && i != ROWS - 1)
/* added the && condition to stop falling off array, but don't know why it worked*/
{
int temp = person[i][1];
person[i][1] = person[i + 1][1];
person[i + 1][1] = temp;
}
//cout << person[i][0] << " " << person[i][1]<<endl;
}
cout << "Number of inner loops: " << count << "\n" << endl;
}
for (int i = 0; i < ROWS; ++i)
{
cout << person[i][0] << " " << person[i][1] << endl;
}
int pause;
cin >> pause;
return 0;
}
|