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.
1
Expert's answer
2013-01-17T09:26:06-0500
#include <iostream> using namespace std; int main() { int minX,minXpos, maxX, maxXpos, i; int n=5; //number of elements in the array
int * x = newint[n]; // get memory for the array x
// get datafrom the user cout <<"Enter " << n << " values" << endl; for(i=0;i<n; i++) { cout<< "x[" << i << "] = "; cin>> x[i]; }
// look for themaximal and minimal elements and their positions minX = maxX =x[0]; minXpos =maxXpos = 0; for(i=1;i<n; i++) { if(x[i]<minX) { minX =x[i]; minXpos= i; } if (maxX<x[i]) { maxX =x[i]; maxXpos= i; } }
Comments
Leave a comment