K
K
Kirill Sinyavsky2016-01-15 12:17:10
C++ / C#
Kirill Sinyavsky, 2016-01-15 12:17:10

How to find the largest and smallest value in an array?

Good day! I would like to clarify one thing. There is no error in the code, just find the largest and smallest values ​​of the array and sum these values:

#include<iostream>

using namespace std;

int main()

{
    const int N = 5;
    int Array[N];
    int sum = 0;


    for(int i = 0; i < N; i++)
    {
        cout<<"Your massive ["<<i<<"] = ";
        cin>>Array[i];
    }
    int Max = Array[0],Min = Array[0];
    for(int i = 1; i < N; i++)
    {
        if(Max < Array[i])
            Max = Array[i];
        if(Min > Array[i])
            Min = Array[i];
    }
    cout<<"Max: "<< Max << endl;
    cout<<"Min: "<< Min << endl;

    sum = Max + Min;
    cout<<"Your summa of massive's elements is = "<< sum << endl;
    return 0;
}

The question is: why should I write the line where I declare the variables Max and Min immediately before the loop, where I specify the condition, and not before the start of the program, where I declare, for example, the variable sum ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
alexxandr, 2016-01-15
@KirillGo

Since then the array will not be initialized and values ​​from memory will get into Max and Min (do not understand which ones)

A
Armenian Radio, 2016-01-15
@gbg

Because if you remember the values ​​of the array elements before the user enters them, instead of the values ​​​​in Max and Min there will be garbage, don't you think?
The program works strictly sequentially - copying the value from place to place occurs at the moment when the assignment operator is encountered.
PS Your English is terrible.

O
Oleg Tsilyurik, 2016-01-15
@Olej

int Max = Array[0], Min = Array[0];
for( int &x : Array ) {
   Max = Max < x ? x : Max;
   Min = Min > x ? x : Min;            
}

V
Vladusch, 2016-01-27
@Vladusch

At the start of the loop, the values ​​of these two variables must:
1. Either belong to the range of values ​​in the array, i.e. it is enough that the Min and Max values ​​are initialized by any element of the array (it can be different for each variable). In this case, you can either declare these variables after you have initialized the array, or declare them earlier (at the beginning of the program), but you need to initialize them later, when the array values ​​are known.
2. Or you need to initialize them so that the Min value is greater than or equal to the largest value in the array, and the Max value is less than or equal to the smallest value in the array (I did not mix up Min and Max) . In your case, this will be the maximum and minimum values ​​for the int type, because you have an array of values ​​of this type. Then you can move the declaration and initialization of the variables Max and Min to the beginning of the program:

int Max = INT_MIN, Min = INT_MAX;  // необходим заголовок <climits> (limits.h)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question