fmax array to find max number

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];

if (fmax[i] > maxNum)
{
maxNum = fmax[i];
}
}
cout << maxNum;
return 0;
}

// I do get an abort message when trying to run it. I know I'm doing something wrong. Just wondering if you can help me along the way. :D Thanks!
Last edited on
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
#include <iostream>
using namespace std;

int main ()
{
	const int 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;
}


Please ask if you have any more questions...
Last edited on
Use [code][/code] tags next time.

But anyway...it looks fine to me, except if they put in only negative numbers. What exactly is the error you are getting?
@Mathhead200- Thanks, I'll give it a try.

@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.
Last edited on
^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.
Topic archived. No new replies allowed.