I'm trying to write a program so the user can enter 10 values and then the program will display the largest and smallest value. I haven't gotten to the smallest value yet. I'm just trying to get the largest to work first. Help please?
#include <iostream>
usingnamespace std;
int main()
{
constint SIZE = 10; // Constant for array size
int values[SIZE]; // Array to hold values
// Get the values.
for (int index = 0; index < SIZE; index++)
{
cout << "Enter value "
<< index + 1 << ": ";
cin >> values[index];
}
// Declare an array.
int numbers[SIZE] = { values[index] };
// Declare a variable to hold the highest value, and
// initialize it with the first value in the array.
int highest = numbers[0];
// Step trough the rest of teh array, beginning at
// element 1. When a value greater than highest is found,
// assign that value to highest.
for (int index = 1; index < SIZE; index++)
{
if (number[index] > highest)
{
highest = numbers[index];
}
}
// Display the highest value.
cout << "The highest value is " << highest << endl;
return 0;
}
Well, you use index on several places, so it is better to declare it in the beginning of the program. You don't need the numbers array, just go through the values.
#include <iostream>
usingnamespace std;
int main()
{
constint SIZE = 5; // Constant for array size
int values[SIZE]; // Array to hold values
int index = 0;
// Get the values.
for (; index < SIZE; index++)
{
cout << "Enter value "
<< index + 1 << ": ";
cin >> values[index];
}
int highest = values[0];
// Step trough the rest of teh array, beginning at
// element 1. When a value greater than highest is found,
// assign that value to highest.
for (int index = 0; index < SIZE; index++)
{
if (values[index] > highest)
{
highest = values[index];
}
}
// Display the highest value.
cout << "The highest value is " << highest << endl;
return 0;
}
#include <iostream>
#include <limits>
usingnamespace std;
int main()
{
constint SIZE = 10; // Constant for size
// Get the values.
int highest = std::numeric_limits<int>::min();
int n;
for (int index = 0; index < SIZE; index++)
{
cout << "Enter value " << index + 1 << ": ";
cin >> n;
if (n > highest)
highest = n;
}
// Display the highest value.
cout << "The highest value is " << highest << endl;
return 0;
}