Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting biggest and smallest value in array C++

This is a very easy and common exercise although I'm coming across an error I can't seem to understand and I can't find an explanation anywhere as it might be too specific.

The program simply prompts user to input how many pancakes were eaten by Person 1 through 10 and then prints out what's the biggest number of pancakes eaten by someone. My problem is that the "hand-made loop" to sort out the biggest and smallest value works but the algorithm (which is highly recommended on this forum to use instead of hand-made loops) doesn't print out the correct biggest 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 the here's 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
like image 762
SecureCake Avatar asked Mar 10 '17 18:03

SecureCake


2 Answers

auto minmax = minmax_element(begin(pan), end(pan));

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

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

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

In fact, reading from an un-initialized variable is undefined behaviour in C and C++, anything can happen, although most of the time you just read what happened to be stored at that memory address.

If you use C++11 (which you should by now), then you can also use a range-based for loop to process the 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
}
like image 116
vsoftco Avatar answered Sep 23 '22 12:09

vsoftco


You have an array of size 11, but you do loop over 10 elements only, leaving the first element uninitialized. That means that it contains junk (undefined behavior), in that case 1606416304, which is the max, isn't it? =)

Change your loops from:

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

to:

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

std::minmaxelement() then shall as you want it to work.


Aftermath:

In general, one common mistake when working with functions that give you something different than the expected result is to check your data you give that function. That way you know whether the data have the problem or/and the function. In your case, printing the array would have make you understand that your data were not OK!

like image 44
gsamaras Avatar answered Sep 22 '22 12:09

gsamaras