Finding the highest and lowest values in a numeric array

Oct 29, 2012 at 5:35pm

#include <iostream> // need for, cout, cin and endl
using namespace std;

int main ()
{
const int TEN_INTEGERS = 10; // number of integers
int integers [TEN_INTEGERS]; // integers
int count; // loop counter


// Get the ten integers from the keyboard

cout << "Please enter ten integers\n";

for (count = 0; count < TEN_INTEGERS; count++)
{
cout << "\nInteger " << (count +1) <<": ";

cin >> integers [count];
}

// Display the average value of the intergers entered

long double average; // datatype for average
double total = 0; // Accumulator

for (count = 0; count < TEN_INTEGERS; count ++)
total += integers [count];
{
average = total / TEN_INTEGERS;

cout << "\nThe average value of the integers entered is: " <<
average << endl;
}

// Display the highest value of the integers entered

long int highest; // datatype for highest integer value
highest = integers [0];

for (count = 0; count < TEN_INTEGERS; count++)
{
if (integers [count] > highest)
highest = integers [count];

{
cout << "\nThe highest value of the integers entered is: " << highest << endl;
}
}

// Display the lowest value of the integers entered

long int lowest = integers [0]; // datatype for the lowest integer value

for (count = 0; count < TEN_INTEGERS; count++)
{
if (integers [count] < lowest)
lowest = integers [count];
{
cout << "\nThe lowest value of the integers entered is: " << lowest << endl;
}
}
return 0;
}

I am able to prompt the user for ten integers, and the average is calculated perfectly, but the highest and lowest values are giving me trouble. It displays ten values for the highest and lowest values when I only want one displayed for each...the highest and lowest value out of the ten integers...Please help
Oct 29, 2012 at 5:40pm
Just place the output statement outside the loop. For example

1
2
3
4
5
6
7
8
9
10
11
long int highest; // datatype for highest integer value 
highest = integers [0]; 

for (count = 0; count < TEN_INTEGERS; count++)
 {
 if (integers [count] > highest)
 highest = integers [count];

 }

 cout << "\nThe highest value of the integers entered is: " << highest << endl;
Last edited on Oct 29, 2012 at 5:41pm
Oct 29, 2012 at 5:46pm
Thanks a lot, who would have ever thought it was that simple
Topic archived. No new replies allowed.