Here was the problem I was attempting to solve with this program. Program: Write a program that takes in 50 values and prints out the highest, the lowest, the average and then all 50 input values, one per line.
Below is my solution, just wondering if you all see any errors, or if you would have done anything different... Thank you for your time!
Note: I have only gotten up to the complexity of arrays, I am still learning so if this seems basic its probably because I don't know a more complex/easier method as of yet. This was a problem in the "arrays" chapter of the book.
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int array[50];
int highestValue = array[0];
int lowestValue = array[0];
int findHighest (int array[], int size)
{
for (int i = 0; i < size; i++)
{
if ( array[ i ] > highestValue)
{
highestValue = array[i];
}
}
return highestValue;
}
int findLowest (int array[], int size)
{
for (int i = size; i > 1; i--)
{
if ( array[ i ] < lowestValue)
{
lowestValue = array[i];
}
}
return lowestValue;
}
int findAverage (int array[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += array[i];
}
return sum/50;
}
void displayArray (int array[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0 )
{
cout << "\n";
}
cout << array [i];
}
cout << "}";
}
int main ()
{
srand(time(NULL));
for ( int i = 0; i < 50; i++ )
{
array[i] = rand() % 100;
}
cout << "Highest Value: " << findHighest (array,50) <<"\n";
cout << "Lowest Value: " << findLowest (array,50) <<"\n";
cout << "Average Value: " << findAverage (array,50) <<"\n";
cout << "Array: ";
displayArray(array, 50);
cout << "\n";
return 0;
}