#include <iostream>
#include <ctime>
#include <limits>
usingnamespace std;
int main( )
{
int Input;
int x;
int arr[10];
for (x = 0; x < 10; x++)
{
cout << "Enter the # of pancakes person #" << x << " ate:" << endl;
cin >> Input;
arr[x] = Input; // This creates an array of 10 numbers I input
}
int max = 0;
cout << " " << endl;
for (x = 0; x < 10; x++)
{
if (max < arr[x]); // This is my (defective) max function
{
max = arr[x];
}
}
cout << " " << endl;
cout << max << endl;
return 0;
}
I've also tried editing the code to have:
1 2 3 4 5
for (x = 0; x < 10; x++)
{
if (max < arr[x]);
max = arr[x];
}
but the problem still isn't fixed.
For example, if I say person #1 ate 99 pancakes, person #2 - #8 ate 0 pancakes, and person #9 at 2 pancakes, the last statement would say "max = 2" instead of "max = 99."
if (max < arr[x]); //<-- look at the semicolon
{
max = arr[x];
}
that could be writted as
1 2 3
if (max < arr[x])
; //<--
max = arr[x];
As you may note by the indentation the max = arr[x]; statement does not form part of the if.
It is executed always on every iteration, and so, `max' would hold the last value of the array.