Finding Largest and Smallest Element in Array

 

# include  <iostream>
using namespace std ;
int main(){
int length_of_array ;

cout << "Enter the no. of Elements :" ;
cin >> length_of_array ;

int array[length_of_array];

for (int i = 0; i < length_of_array; ++i) {
cin >> array[i] ;
}

int largest{INT16_MIN} ;
for (int i = 0; i < length_of_array; ++i) {

if (array[i] > largest) {
largest = array[i] ;
}
}
int smallest{INT32_MAX};
for (int i = 0; i < length_of_array; ++i) {

if (array[i] < smallest){
smallest = array[i] ;
}

}
cout << "Largest no. in the array is " << largest << '\n' ;
cout << "Smallest no. in the array is " << smallest << '\n' ;
return 0 ;
}

Comments

Popular posts from this blog

CodeChef::CSUB

How Recursion Works?

CodeChef::TREE2