Home > Net >  finding maximum and minimum number in array
finding maximum and minimum number in array

Time:10-11

I have written following code which allows user to input an array and give out maximum and minimum value in that array

when i input length of array value = 4 and elements of array as:- 32,152,38 it give output as max = 1973314801 and min = 24(unexpected values)

for following code:-

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"input length of array = ";
    cin>>n;
    int array[n];
    for(int i=0; i<n;i  )
    {
        int a;
        cout<<"input element "<<i 1<<" = ";
        cin>>a;
    }
    int max=array[0];
    int min=array[0];
    for(int i=0; i<n;i  )
    {
        if(array[i]>max)
        {
            max=array[i];
        }
        if(array[i]<min)
        {
            min=array[i];
        }
    }
    cout<<"max = "<<max<<endl;
    cout<<"min = "<<min;
}

but when i define value of array in code it give expected output (min=3 and max=12)

code(code in which value of array is already defined):-

#include<iostream>
using namespace std;
int main()
{
    int array[4]={3,6,9,12};
    int max=array[0];
    int min=array[0];
    for(int i=0; i<4;i  )
    {
        if(array[i]>max)
        {
            max=array[i];
        }
        if(array[i]<min)
        {
            min=array[i];
        }
    }
    cout<<"max = "<<max<<endl;
    cout<<"min = "<<min;
}

what can be the problem here?

CodePudding user response:

According to the C Standard the size of a C array must be compile time constant. So your first code example is not correct. That is you cannot take the size of the array from the user as input. You have to explicitly specify the size of the array as you did in your 2nd example.

You can use std::vector for this purpose instead.

CodePudding user response:

You are storing input data in variable a instead of array array and you can't assign array size like that. You have to specify the size as constant before using it.

#include<iostream>
using namespace std;
int main()
{
    const int n = 4;
    /*cout<<"input length of array = ";
    cin>>n;*/
    int array[n];
    for(int i=0; i<n;i  )
    {
        cout<<"input element "<<i 1<<" = ";
        cin>>array[i];
    }
    int max=array[0];
    int min=array[0];
    for(int i=0; i<n;i  )
    {
        if(array[i]>max)
        {
            max=array[i];
        }
        if(array[i]<min)
        {
            min=array[i];
        }
    }
    cout<<"max = "<<max<<endl;
    cout<<"min = "<<min;
}

But if you still want to take size as user input then you have to create that array in heap memory

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"input length of array = ";
    cin>>n;
    int* array = new int[n]; //array in heap
    for(int i=0; i<n;i  )
    {
        cout<<"input element "<<i 1<<" = ";
        cin>>array[i];
    }
    int max=array[0];
    int min=array[0];
    for(int i=0; i<n;i  )
    {
        if(array[i]>max)
        {
            max=array[i];
        }
        if(array[i]<min)
        {
            min=array[i];
        }
    }
    cout<<"max = "<<max<<endl;
    cout<<"min = "<<min;
    delete [] array;
}

CodePudding user response:

You don't need an array; use a running min and max calculation:

int minimum;
int maximum;
cin >> minimum;
maximum = minimum;
int number;
while (cin >> number)
{
    if (number > maximum) maximum = number;
    if (number < minimum) minimum = number;
}
cout << "Maximum: " << maximum
     << ", Minimum: " << minimum
     << "\n";

There is no need to store the numbers, only to compare the number to determine the maximum and minimum.

The requirements are still met, because the program is entering an array or bunch, of numbers.

One nice feature of the running min and max, is there is no limit to the quantity of numbers that can be input. The only limit is the range of values that an int can hold.

  • Related