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.
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;
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];
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;
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;
//LargestSmallest.cpp
//##
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
intconst 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.