First time poster here. Glad to join the forum. I'm currently reading "Jumping into C++".
I'm stuck solving this practice problem on arrays: 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.
I've written a program but the functions I've written for outputting the lowest and highest values
of the array aren't working as expected. I restricted the 50 values to modulus 5. But on occasion I get
the highest value to be greater than 4 or less than 4 when 4 is clearly the highest number value in the array.
same goes for my "lowest" function.
My highest values function:
1 2 3 4 5 6 7 8 9 10 11 12
|
int highest_value (int array[], int size)
{
int highest = 0;
for (int i = highest + 1; i < size; i++)
{
if ( array [ i ] > array [ highest ] )
{
highest = i;
}
}
return highest;
}
|
My lowest value function:
1 2 3 4 5 6 7 8 9 10 11 12
|
int lowest_value (int array[], int size)
{
int lowest = 0;
for (int i = lowest + 1; i < size; i++)
{
if ( array [i] < array [lowest] )
{
lowest = i;
}
}
return lowest;
}
|
This problem has halted my reading. I would like some help on figuring out why the functions are malfunctioning (couldn't resist)
P.S I'm new to this in case I don't post the codes well. I apologize if the post is lost.I'm open to any suggestions on code syntax and anything else. Thank you
The full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// functions for highest, lowest, average and displaying the array
int average_value (int array[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += array[i];
}
int average = sum / size;
return average;
}
int highest_value (int array[], int size)
{
int highest = 0;
for (int i = highest + 1; i < size; i++)
{
if ( array [ i ] > array [ highest ] )
{
highest = i;
}
}
return highest;
}
int lowest_value (int array[], int size)
{
int lowest = 0;
for (int i = lowest + 1; i < size; i++)
{
if ( array [i] < array [lowest] )
{
lowest = i;
}
}
return lowest;
}
void DisplayArray (int array[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if ( i != 0)
{
cout << ", ";
}
cout << array [i];
}
cout << "}";
}
int main()
{
const int size = 50;
int value[size];
srand ( time ( NULL ) );
for ( int i = 0; i < size; i++ )
{
value [i] = rand() % 5;
}
cout << "The average value of the array of elements is: " << average_value(value, size) << endl;
cout << "The highest value of the array of elements is: " << highest_value(value, size) << endl;
cout << "The lowest value of the array of elements is: " << lowest_value(value, size) << endl;
// note the name of array and the array arguments in the prototype of the functions
DisplayArray(value, size);
cout << endl;
return 0;
}
|