Unable to find smallest number in an array

I am working on the second part of the Pancake Glutton exercise here: http://www.cplusplus.com/forum/articles/12974/ and for some reason I am unable to obtain the smallest value in the array. I've gone through many of the threads here in the forums but none of the solutions met my needs. I understand that I could use sort() but I want to know why my current code is not working so that I can figure out what I am doing wrong. Thank you.

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
//Pancake Glutton

#include <iostream>

using namespace std;

int main()
{
    int person[10] = {0};
    int i = 0;
    int largestNum = person[0];
    int smallestNum = person[0];
    
    do
    {
        cout << "enter the amount of pancakes eaten by "
             << "person number " << i+1 << ":\t";
        cin >> person[i];
        cout << endl;
        
        if (person[i] > largestNum)
           largestNum = person[i];
        
        if (person[i] < smallestNum)
           smallestNum = person[i]; 
           
        i++;
         
    } while (i <= 9);
    
    cout << "\nlargest amnt is: " << largestNum;
    cout << "\nSmallest amnt is: " << smallestNum;
    
    for (i=0; i <= 9; i++)
    {
        if (largestNum == person[i])                              
           cout << "\nThe person that ate the most was "
                << "person #" << i << " with " << largestNum 
                << " pancakes" << endl;
          
    }
        

    return 0; 
    
}
I believe your algorithm is fine. The problem is that you are initializing smallestNum to zero. No person is able to eat less than zero pancakes, so smallestNum always remains zero. Try initializing it to some big value, like 20000 or something, and see if this solves your problem.
Last edited on
You are right and I realized that just before reading your post lol. I initialized it to 5000 and it worked. However, would that be a standard response for a situation like this? Or would the use of sort() be the actual standard? Thanks for your help.
First this has already been done. If you are learning C++ on your own then I suggest that you use the std algorithms when possible. It works fine with C-Arrays. Instead of reinventing the wheel use library functions which will allow you to build your program much faster.
http://cplusplus.com/reference/algorithm/max_element/
http://cplusplus.com/reference/algorithm/min_element/

Secondly, initialize the min value to the largest possible size for that type. m4ster r0shi is correct but I'm just adding more specific info to that suggestion.
http://cplusplus.com/reference/std/limits/numeric_limits/

int smallest(numeric_limits<int>::max());
Although in this case, it would be easier just to make it equal to the first element of the array.

EDIT: That would only work after you have already gotten the data into the array
Last edited on
I believe the standard response to find minimum and maximum elements of an array would be your algorithm with the exception that the variables holding min and max should be initialized to an element of the array. for example you could do it like this:

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
cout << "enter the amount of pancakes eaten by "
         << "person number " << 1 << ":\t";

cin >> person[0];
cout << endl;

largestNum = person[0];
smallestNum = person[0];

i=1;

do
{
    cout << "enter the amount of pancakes eaten by "
         << "person number " << i+1 << ":\t";
    cin >> person[i];
    cout << endl;
    
    if (person[i] > largestNum)
       largestNum = person[i];
    
    if (person[i] < smallestNum)
       smallestNum = person[i]; 
       
    i++;
     
} while (i <= 9);


EDIT: ah, firedraco was one step ahead of me...
Last edited on
of course, if you don't do this for educational reasons, using the standard functions kempofighter suggests would be better.
@kenpo - Thanks for the info. I wasn't trying to reinvent the wheel per se so much as I was trying to create a response using the required parameters of the problem. Despite this, I should start using library functions anyway since that is how it will be done in a professional environment

@m4ster r0shi & firedraco - I had it initialized to the array in the beginning but I foolishly did so before any values were entered into the array, resulting in the 0 value for smallestNum

Thank you all for your help.
Topic archived. No new replies allowed.