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
|
#include <iostream>
using namespace std;
//Prototype functions
int maximum(int pancakes[], int elements);
int minimum(int pancakes[], int elements);
int countmax(int pancakes[], int elements);
int countmin(int pancakes[], int elements);
void sort(int pancakes[], int elements);
int printArray(int pancakes[], int person[], int elements);
int main()
{
int pancakes[10];
int min, max, personmax, personmin;
cout << "Person #\tPancakes" << "\n\n"; // Header
for(int i=0; i<10; i++)
{
pancakes[i]=0; //initializes all elements to 0
cout << "Person " << i+1 << "\t"; //Person number starts at 1
cin >> pancakes[i];
}
max = maximum(pancakes, 10);
min = minimum(pancakes, 10);
int cntmax = countmax(pancakes, 10);
int cntmin = countmin(pancakes, 10);
cout << cout << "\n\nMaximum # of pancakes eaten: " << max << " ";
for(int m=0; m<10; m++) // Finds person number who ate maximum pancakes
{
if(pancakes[m]==max)
{
personmax = m+1;
if(cntmax==1)
cout << "By person " << personmax;
else cout << "\nBy person " << personmax << "; ";
}
}
cout << "\n\nMinimum # of pancakes eaten: " << min << " ";
for(int n=0; n<10; n++) // Finds person number who ate minimum pancakes
{
if(pancakes[n]==min)
{
personmin = n+1;
if(cntmin==1)
cout << "By person " << personmin;
else cout << "\nBy person " << personmin << "; ";
}
}
cout << "\n\n\n";
sort(pancakes, 10);
cin.get();
}
int maximum(int pancakes[], int elements)
{
int max = pancakes[0];
for(int i=0; i<elements; i++)
if(pancakes[i]>max)
max = pancakes[i];
return max;
}
int minimum(int pancakes[], int elements)
{
int min = maximum(pancakes, 10);
for(int i=0; i<elements; i++)
if(pancakes[i]<min)
min = pancakes[i];
return min;
}
int countmax(int pancakes[], int elements) //calculates # of persons who ate max
{
int cntmax=0;
int max = (pancakes, 10);
for(int m=0; m<elements; m++)
{
if(pancakes[m]==max)
cntmax++;
}
return cntmax;
}
int countmin(int pancakes[], int elements) // calculates # of person who at min
{
int cntmin=0;
int min = (pancakes, 10);
for(int m=0; m<elements; m++)
{
if(pancakes[m]==min)
cntmin++;
}
return cntmin;
}
void sort(int pancakes[], int elements)
{
int temp1, temp2;
int person[10];
for(int n=0; n<elements; n++)
person[n] = n;
for(int pass=0; pass<=elements-1; pass++) // Bubble sort
{
for(int i=0; i<elements-1; i++)
if(pancakes[i]<pancakes[i+1])
{
temp1=pancakes[i];
pancakes[i]= pancakes[i+1];
pancakes[i+1] = temp1;
temp2=person[i];
person[i]=person[i+1];
person[i+1]=temp2;
}
}
printArray(pancakes, person, 10); // Calls function printArray
}
int printArray(int pancakes[], int person[], int elements)
{
for(int i=0;i<elements; i++)
{
cout << "Person " << person[i]+1 << ": ate " << pancakes[i] << " pancakes" << endl;
}
}
|