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.
1
Expert's answer
2013-01-23T08:48:25-0500
#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";
Comments
Leave a comment