user15873758
user15873758

Reputation:

finding maximum and minimum number in array

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?

Upvotes: -3

Views: 3997

Answers (5)

javid salihe
javid salihe

Reputation: 1

 int maAndmi[] = {2, 4, 5, 100};
    int max = maAndmi[0];
    int min = maAndmi[0];

    for (int i = 1; i< sizeof(maAndmi) / sizeof(int) ; i++) {
        if (maAndmi[i] > max)
            max = maAndmi[i];
        if (maAndmi[i] < min)
            min = maAndmi[i];
    }

    cout << "This is the MAX number: " << max << endl;
    cout << "This is the MIN number: " << min << endl;

Upvotes: 0

cpp_newbie
cpp_newbie

Reputation: 11

I know you already have your answer but here's another way to find min/max element of std::array (which I prefer using if given the choice).

#include <iostream>
#include <algorithm>
#include <array>

int main()
{
    std::array<int, 100> arr;
    int arrSize = 0;
    
    std::cout << "Please enter the size of your array (<=100): ";
    std::cin >> arrSize;
    
    std::cout << "Please enter your elements one at a time followed by pressing enter: ";
    for (int i = 0; i < arrSize; i++)
    {
        std::cin >> arr[i];
    }
    
    std::cout << "Your array: ";
    for (int i = 0; i < arrSize; i++)
    {
        std::cout << arr[i] << ", ";
    }
    std::cout << "\n";
    
    std::cout << "Min element: " << *std::min_element(arr.data(), arr.data() + arrSize) << "\n";
    std::cout << "Max element: " << *std::max_element(arr.data(), arr.data() + arrSize) << "\n\n";
    
    return 0; 
}

Upvotes: 0

Thomas Matthews
Thomas Matthews

Reputation: 57729

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.

Upvotes: 0

Kundan
Kundan

Reputation: 1952

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;
}

Upvotes: 0

Alan
Alan

Reputation: 1

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.

Upvotes: 1

Related Questions