Hey, all. I'm just stuck on this problem. It basically asks us to use an array named fmax that takes in 10 integers inputted from the user and find the maximum number out of the ten. It also asks us to use only one loop to do make this program work. Here's what I have so far:
#include <iostream>
using namespace std;
int main ()
{
const int NUMELS = 10;
int i, fmax[NUMELS], maxNum = 0;
for (i = 0; i < NUMELS; i++)
{
cout << "Enter a number: ";
cin >> fmax[NUMELS];
#include <iostream>
usingnamespace std;
int main ()
{
constint NUMELS = 10;
int i, fmax[NUMELS], maxNum = 0; //also maybe start maxNum at a much lower number
// in case of negative entries or (see below, line 17)
for (i = 0; i < NUMELS; i++)
{
cout << "Enter a number: ";
// cin >> fmax[NUMELS]; //you are storing the input out of the array's bounds
cin >> fmax[i]; //use this instead
if (fmax[i] > maxNum)
// if(fmax[i] > maxNum || i == 0) //if i is 0 no number has been entered yet
// so it must be the maximum so far
{
maxNum = fmax[i];
}
}
cout << maxNum;
return 0;
}
@firedraco- Well, when I'm finished entering the 10th number, I get an abort error that basically says "Stack around the variable 'fmax' was corrupted. I actually followed Mathhead200's code and it worked.
The comment about the array's bounds is probably what gave me the abort error. Thanks, guys.
^Ah. Mathhead got it right, you were writing out of bounds of the array. The message you got was a helper message trying to help you find out of bounds access errors.