Home > Blockchain >  Min and Max function return gibberish value when array is not full
Min and Max function return gibberish value when array is not full

Time:04-28

I need to create a program that collects user input until the user presses 0 or has inputted 10 values. Then I need to create a function, without using built-in to get the minimum and maximum values. However, when 9 or less values are inputted, the minimum and maximum function return a random value. Here is my code:

#include <iostream>
using namespace std;

int getMin(int array[], int n);
int getMax(int array[], int n);

int main()
{
    int array[10];

    cout << "This program will ask the user to input various numbers (positive or negative but not zero) and perform basic statistics on these numbers.\n";

    for(int i=0; i<10; i  ){
        cout<<"\nInput an integer: ";
        cin>>array[i];

        if(array[i]==0) {
        break; 
        }
    }

    int n = sizeof(array) / sizeof(array[0]);
    cout << "Minimum element of array: " << getMin(array,n) << "\n";
    cout << "Maximum element of array: " << getMax(array,n);
    return 0;
}
int getMin(int array[], int n)
{
    int minimum = array[0];
    for (int i = 1; i < n; i  )
        if (minimum > array[i]) {
            minimum=array[I];
        }
    return minimum;
}

int getMax(int array[], int n)
{
    int maximum = array[0];
    for (int i = 1; i < n; i  )
        if (maximum < array[i]) {
           maximum=array[I];
        }
    return maximum;
}

How do I make it so even if I input less than 10 values, it returns the correct minimum and maximum value?

CodePudding user response:

There's no such thing as a not full array. When you declare an array like this:

int array[10];

it has 10 ints in it, always and forever, whether you like it or not.

You just don't know what they are. Most likely, they are basically nonsense numbers, some pointer values from the system converted to ints. If you're unlucky, you'll get zeroes. That's unlucky, not lucky, because you won't spot the problem, and next week when the teacher grades it they might not be zeroes any more.

Your loop stops after it reads a 0. If I type in 5 numbers 1,2,3,4,5, then the array is full of

array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 0
array[6] = 2130374633
array[7] = -1845723111
array[8] = 346214765
array[9] = -678554534

for example.

Then when your code looks for the minimum it would find -1845723111 and the maximum is 2130374633.

The easy solution is to only look at the first 5 numbers. Instead of int n = sizeof(array) / sizeof(array[0]); (which calculates 10, the number of ints in the array) you should set n to be the number of numbers the user has typed in. I won't write that code for you, since it would be doing your homework, but it shouldn't be very hard.

CodePudding user response:

You are breaking the loop on less than 10 values, if need be, but still iterate over the entire array. Instead you should only iterate over those values that actually have been written to, reading the others results in undefined behaviour as the array is not initialised at these locations (and thus contains the 'giberish' you see...).

So:

size_t n;
for(n = 0; n < sizeof(array)/sizeof(*array);   n)
{
    // use n instead of i
}

getMin(array, n);
getMax(array, n);

CodePudding user response:

When the user inputs 0, you are not storing the value, and you stop reading any other values

if(array[i]==0) {
    break; 
}

So, in your getMin and getMax functions, you should update to this

for (int i = 1; i < n; i  ){
   if(array[i] == 0)
      break;
   // your code to get min or max goes here
}

Please note that, for this approach, your array cannot be empty (cannot have the first element zeroed)

  • Related