This is a very easy and common exercise, although I encounter a mistake that I cannot understand, and I cannot find an explanation anywhere, as this may be too specific.
The program simply prompts the user to enter the number of pancakes that were eaten by Person 1-10, and then prints out what the largest number of pancakes someone is eating. My problem is that the โmanual loopโ to determine the largest and smallest value works, but the algorithm (which is strongly recommended to use this forum instead of hand tools) does not output the correct largest value , but works for the smallest .
Here is my code:
void pancakes() { int pan[11]; int small, big; for (int i = 1; i < 11; i++) // counts to 11-1 and prompts user for pancakes // eaten by person 1==>10 { cout << "How many pancakes did person " << i << " eat?\n"; cin >> pan[i]; } big = small = pan[1]; // assigns element to be highest or lowest value for (int i = 1; i < 11; i++) { if (pan[i] > big) // compare biggest value with current "big" element { big = pan[i]; } if (pan[i] < small) // compares smallest value with current "small" element { small = pan[i]; } } cout << "The person who ate the most pancakes ate " << big << " of them." << endl; // prints biggest value cout << "The person who ate the least pancakes ate " << small << " of them." << endl; // prints smallest value auto minmax = minmax_element(begin(pan), end(pan)); cout << "min element " << *(minmax.first) << "\n"; cout << "max element " << *(minmax.second) << "\n"; }
And here is what the console returns:
How many pancakes did person 1 eat? 45 How many pancakes did person 2 eat? 64 How many pancakes did person 3 eat? 7 How many pancakes did person 4 eat? 34 How many pancakes did person 5 eat? 87 How many pancakes did person 6 eat? 45 How many pancakes did person 7 eat? 89 How many pancakes did person 8 eat? 32 How many pancakes did person 9 eat? 55 How many pancakes did person 10 eat? 66 The person who ate the most pancakes ate 89 of them. The person who ate the least pancakes ate 7 of them. min element 7 max element 1606416304
source share