Help with this code for smallest and largest

closed account (iAk3T05o)
This is the code o found on the net:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int max = 0;
int min = 0;
int arrai[5];

for (int i = 0; i<5; ++i)
{
cout << "enter value " << i << ": ";
cin >> arrai[i];

if (arrai[i] > max)
{
max = arrai[i];
}

min = max;

if (arrai[i] < min)
{
min = arrai[i];
}
}
cout << "largest is " << max;

cout << "lowest is " << min;
 


It does what i want if the last element is not the largest. I don't really understand the code either.
Can you use any example to find the largest and smallest element in an unsorted array and explain.
Thanks.
Last edited on
This doesn't sort. It just finds the smallest largest values.

Comments describe what's going on:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int max = 0;  // self-explanitory
int min = 0;  // self-explanitory, but I would init to a large number
int arrai[5]; // self-explanitory

// iterate through five values
for (int i = 0; i<5; ++i)
{
  cout << "enter value " << i << ": ";
  cin >> arrai[i]; // input a value

  if (arrai[i] > max) // if the input is the largest so far
  {
    max = arrai[i]; // then save it
  }

  min = max; // this doesn't make sense

  if (arrai[i] < min) // if the input is smaller than the last min value
  {
    min = arrai[i]; // then save it
  }
}
cout << "largest is " << max; // report max

cout << "lowest is " << min; // report min 


This might be easier to read. It's pretty much the same and you don't need an array when you only have one loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
int max = 0, min = 999, input;
for (int i = 0; i < 5; ++i)
{
  cout << "enter value " << i << ": ";
  cin >> input;
  
  if (input > max)
    max = input;
  if (input < min)
    min = input;
}
cout << "largest is " << max;
cout << "lowest is "  << min;
closed account (iAk3T05o)
i need it done with an array
I suggest a for loop to get the input, and another for loop to find the min and max. Also after you enter the values I suggest setting the default value of min and max to arrai[0];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int max = 0;
int min = 0;
int arrai[5];

for ( int i = 0; i < 5; i++ )
{
    cout << "enter value " << i << ": ";
    cin >> arrai[i];
}
max = arrai[0];
min = arrai[0];
for ( int i = 0; i < 5; i++ )
{
    if (arrai[i] > max)
    {
        max = arrai[i];
    }
    if (arrai[i] < min)
    {
        min = arrai[i];
    }
}
cout << "largest is " << max << endl;

cout << "lowest is " << min << endl;
closed account (iAk3T05o)
Vasilenko, that works.
Thanks.

Time to try it with a struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Stuff{
  int max = 0;
  int min = 0;
  int arrai[5];
} stuff;

for ( int i = 0; i < 5; i++ )
{
    cout << "enter value " << i << ": ";
    cin >> stuff.arrai[i];
}
stuff.max = stuff.arrai[0];
stuff.min = stuff.arrai[0];
for ( int i = 0; i < 5; i++ )
{
    if (stuff.arrai[i] > stuff.max)
    {
        stuff.max = stuff.arrai[i];
    }
    if (stuff.arrai[i] < stuff.min)
    {
        stuff.min = stuff.arrai[i];
    }
}
cout << "largest is " << stuff.max << endl;

cout << "lowest is " << stuff.min << endl;

Enter value #1 :3
Enter value #2 :-1
Enter value #3 :7
Enter value #4 :10
Enter value #5 :4

Largest 10
Smallest -1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//LargestSmallest.cpp
//##

#include <iostream>
using std::cout;
using std::cin;
using std::endl;



int main(){

int const ARRAY_SIZE=5;
int max=0;
int min=0;
int array[ARRAY_SIZE]={};
        

for(int i=0;i<ARRAY_SIZE;i++){
        cout<<"Enter value #"<<i+1<<" :";
        cin>>array[i];

        if(i==0){ //first input is the largest and smallest number by default 
                max=array[i];
                min=array[i];
        }else{ //is not the first input then compare the numbers
                if(array[i]<min) //if the current input is less than the min number
                        min=array[i]; //store current number in min
                if(array[i]>max) //if current number is greater than max number
                        max=array[i]; //store in max 
        }//end if..else


}//end outer for

//prints largest -max- and smallest -min- numbers
cout<<"\nLargest "<<max<<endl;
cout<<"Smallest "<<min<<endl;


return 0; //indicates success
}//end of main 
@Stewbond
It does not help Nathan learn C++ if you just do it for him. He wanted to try rewriting our code with structures, now he will just copy and paste it.
closed account (iAk3T05o)
thanks for the struct code but i needed to try it myself first.
Topic archived. No new replies allowed.