0

I am trying to find the minimum number when the user enters any 5 numbers

I am not sure why this code is not working, every time I run this function I always get 0. Can someone please tell me what is wrong!

    int i;
    int num1 = 5;
    int min;
    min = arraySize[0];
    arraySize[num1];

    cout<<"Enter values (press enter after inputting every number): "<<endl;
    for (i=0; i<5; i++)
    {
    cin >> arraySize[i];      
    }

    for(i=1; i<5; i++)
    {
       if(min > arraySize[i])
       {
       min = arraySize[i];
       }
    }
     cout << "Your mininum number is " << min << endl; 
3
Contributors
4
Replies
6
Views
4 Years
Discussion Span
Last Post by r0vanz01
Featured Replies
  • 1
    WaltP 2,905   4 Years Ago

    Not quite. After you input all the numbers, set **min** to **arraySize[0]**. Then you code should work fine. Read More

0

You have assigned min from arraySize[0] before it was initialized. You are lucking that it was initialized to zero - it just as easily could have been garbage. So, change then initial assignment from min = arraySize[0] to min = 0, and then change the for() loop to determine min from for (i = 1; i < 5; i++) to for (i = 0; i < 5; i++)

0

I did what you said, and it still returns 0; I have my arraySize declared as int arraySize[]; where I have my methods declared in my class. I am not sure if this has anything to do with it.

1

Not quite. After you input all the numbers, set min to arraySize[0].
Then you code should work fine.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.