208k views
4 votes
Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find and display the maximum and minimum elements.

1 Answer

4 votes

Answer:

Following are the code to this question:

#include<iostream>//defining header file

using namespace std;

int main ()//defining main method

{

int x[20], n, i, maximum, minimum;//defining an integer array x and another integer variable

cout <<"Enter the number of element you want to insert: ";//print message

cin >>n;//input size value from user end

if(n>20)//defining if block that check n is greater then 20

{

cout <<"The size of array should not exceed 20 element";//print message

return 0;//return 0

}

else//defining else block

{

cout<<"Enter array value: ";//print message

for(i=0;i<n;i++)//defining for loop

cin>>x[i];//defining for loop for input array value

maximum =x[0];//defining maximum variable that hold array value

for (i=0;i<n;i++)//defining for loop for check value

{

if(maximum<x[i])//defining if block to check maximum value

{

maximum=x[i];//hold maximum value

}

}

minimum=x[0];//use minimum variable to hold array value

for (i=0;i<n;i++)//defining for loop to hold minimum value

{

if (minimum>x[i])//defining if block to check minimum value

minimum =x[i];//hold minimum value

}

cout << "Maximum element : " << maximum ;//print maximum value

cout << "Minimum element : " << minimum ;//print minimum value

return 0;

}

}

Output:

Enter the number of element you want to insert: 7

Enter array value:

8

6

22

13

98

554

3

Maximum element : 554

Minimum element : 3

Step-by-step explanation:

In the above-given program, an integer array "x", and four integer variable "n, i, maximum, and minimum" is defined, which uses the if block to check the size of the array.

In the else block, it inputs the array value from the user end and after input value, it uses the conditional statement to check the "maximum and minimum" value in its variable and print its value.

User Buley
by
4.2k points