Sort the largest and smallest value in a C ++ array

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 
+5
source share
3 answers

You have an array of size 11, but you make a loop of only 10 elements, leaving the first element uninitialized. This means that it contains unwanted (undefined behavior), in this case 1606416304, which is the maximum, right? =)

Change your loops:

 for (int i = 1; i < 11; i++) 

in

 for (int i = 0; i < 11; i++) 

std :: minmaxelement () , then it should work as you want.


Aftermath:

In general, one common mistake when working with functions that give you something other than the expected result is to check your data that you give to this function. That way, you know if the data has problems and / or a function. In your case, printing an array will make you realize that your data was out of order!

+7
source
 auto minmax = minmax_element(begin(pan), end(pan)); 

really finds min / max, but indexing the array in C ++ starts at 0. You populate int pan[11]; starting at index 1,

 big=small=pan[1]; //assigns element to be highest or lowest value; change to pan[0] for (int i = 1; i < 11; i++){...} // change to i=0 

therefore pan[0] will contain garbage, which (in your case the value 1606416304 ) will be considered by minmax_element .

In fact, reading from an uninitialized variable is undefined behavior in C and C ++, everything can happen, although most of the time you just read what happened to save this memory address.

If you are using C ++ 11 (which you should now), you can also use a range for the loop to process pancakes :)

 for(auto& pancake: pan) // note the reference, we are reading { cin >> pancake; // to read } 

and

 for(auto pancake: pan) { // further processing here, like if(pancake < small) { small = pancake;} // etc } 
+9
source

Your pan array is defined as having 11 elements, but you only initialize 10 of them. It should be noted that pan[0] never initialized and will have some random value. I assume your random value turned out to be 1606416304.

+2
source

Source: https://habr.com/ru/post/1265308/


All Articles