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
|
#include <iostream>
using namespace std;
//Prototypes
int get_Max(int[], int);
int get_Min(int[], int);
void BubbleSort(int[], int[], int);
void PrintList(int[], int[], int);
int main()
{
int person[10], // stores persons
numPancakes[10]; // stores number of pancakes
// Fill up person array with person #'s
for(int i=0; i<=10; i++)
{
person[i] = i+1;
}
// Fill up numPancakes array with user input
for(int i=0; i<10; i++)
{
cout << "Enter number of panacakes eaten by person " << person[i] << ": ";
cin >> numPancakes[i];
}
int maxPerson = get_Max(numPancakes, 10); // call get_Max function
cout << "\nMost number of pancakes were eaten by person " << maxPerson << endl;
int minPerson = get_Min(numPancakes, 10); // call get_Min function
cout << "Least numer of pancakes were eaten by person " << minPerson << endl;
cout << "\n";
BubbleSort(person, numPancakes, 10); // Call Bubble Sort function
PrintList(person, numPancakes, 10); // Call PrinList function
cin.get();
}
int get_Max(int numPancakes[], int elements)
{
int maxPerson = 1; // default is person 1 in case the if-condition is never true
int temp = numPancakes[0]; // temp stores the number of pancakes, not the person #
int i; // loop counter
for(i=0; i<elements; i++)
if(numPancakes[i]>temp)
{
temp = numPancakes[i];
maxPerson = i+1; // Stores person # (exactly what we want) into maxPerson
}
return maxPerson;
}
int get_Min(int numPancakes[], int elements)
{
int minPerson = 1; // default is person 1 in case the if-condition in never true
int temp = numPancakes[0]; // temp stores number of pancakes, not person #
int i; // Loop counter
for(i=0; i<elements; i++)
if(numPancakes[i]<temp)
{
temp = numPancakes[i];
minPerson = i+1; // Stores person # into minPerson
}
return minPerson;
}
void BubbleSort(int person[], int numPancakes[], int elements)
{
int temp1, temp2;
// Bubble Sort
for(int pass=0; pass<elements; pass++)
for(int i=0; i<elements-1; i++)
if(numPancakes[i]<numPancakes[i+1])
{
// Swap numPancakes[i] and numPancakes[i+1]
temp1 = numPancakes[i];
numPancakes[i] = numPancakes[i+1];
numPancakes[i+1] = temp1;
// Swap person[i] and person [i+1]
temp2 = person[i];
person[i] = person[i+1];
person[i+1] = temp2;
}
return;
}
// Simply prints the list
void PrintList(int person[], int numPancakes[], int elements)
{
for(int i=0; i<elements; i++)
{
cout << "Person " << person[i] << ": ate " << numPancakes[i] << " pancakes" << endl;
}
}
|