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
|
#include <iostream>
using namespace std;
void MAXminPancakesEATIN (int array[], int size);
int FINDhighestELEMENT (int array[], int size, int index);
void displayARRAY (int array[], int size)
{
cout << "Here is the list of pancakes eatin from greatest to least." << endl;
cout << "\n";
cout << "{";
for (int i = 0; i < size; ++i)
// if pass first element, add a comma
{
if (i != 0)
{
cout << ",";
}
cout << array[i];
}
cout << "}";
}
void swap (int array [], int first_index, int second_index)
{
int temp = array [first_index];
array[first_index] = array [second_index];
array [second_index] = temp;
}
void insertion_sort (int array[], int size)
{
for (int i = 0; i < size; ++i)
{
int index = FINDhighestELEMENT (array, size, i);
swap(array, i, index);
}
}
int main()
{
int person = 1;
int num_pancakes_people_ate [10];
cout << "Enter the number of pancakes that each person ate." << endl;
for (int i = 0; i < 10; ++i)
{
cin >> num_pancakes_people_ate[i];
cout << "Person number " << person << " ate " << num_pancakes_people_ate [i] << " pancakes." << endl;
cout << "\n";
++person;
}
cout << "\n";
MAXminPancakesEATIN(num_pancakes_people_ate, 10);
cout << "\n";
insertion_sort(num_pancakes_people_ate,10);
displayARRAY(num_pancakes_people_ate, 10);
}
void MAXminPancakesEATIN(int array[], int size)
{
int highest_person = 1;
int least_person = 1;
int max = array[0];
int min = array[0];
for (int i = 0; i < size; i++)
{
if (array[i] > max)
{
max = array[i];
}
if (array[i] < min)
{
min = array[i];
}
}
for (int j = 0; j < size; ++j)
{
if (array[j] == max)
{
break;
}
else
{
++highest_person;
}
}
cout << "\n";
cout << "Person " << highest_person << " ate the most pancakes." << endl;
for (int x = 0; x < size; ++x)
{
if (array[x] == min)
{
break;
}
else
{
++least_person;
}
}
cout << "\n";
cout << "Person " << least_person << " ate the least pancakes." << endl;
}
int FINDhighestELEMENT (int array[], int size, int index)
{
int highest_value = index;
for (int i = index + 1; i < size; ++i)
{
if (array [i] > array [highest_value])
{
highest_value = i;
}
}
return highest_value;
}
|