Question #21971

How can I write a program that lets the user enter 50 elements in an array whose data type is integer? Locate the smallest and largest elements and display their corresponding value and location, Thanks.

Expert's answer

#include <iostream>
using namespace std;

int a[10];
int i, ma, mi, i_ma, i_mi;

void main(){

i = 0;
while (i<51){
cout<<"enter a number: ";
cin>>a[i];
i++;
}

ma = a[0]; mi = a[0]; i_ma = 1; i_mi = 1;
for (i=0; i<51; i++){
if (ma<a[i]) { ma=a[i]; i_ma = i+1; }
if (mi>a[i]) { mi=a[i]; i_mi = i+1; }
}

cout<<"maximum: "<<ma<<", position "<<i_ma<<"\nminimum: "<<mi<<", position "<<i_mi<<"\n";

system("pause");

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS