# array problem

• 03-30-2007
SMAlvarez
array problem
I need help fixing a problem with an array.

The code is as follows:

Code:

```#include <iostream> using namespace std; int max(int a[]) { int maximum = a[0]; for (int i=1; i < sizeof(a); i++) { if (a[i] > maximum) { maximum = a[i]; } } return maximum; } int main() { int j=1; int total=0; cout << "Please enter the amount of numbers you want to calculate: "; cin >> j; int numbers[j]; for (int i=0; i<j; i++) { cout << "Please enter number " << i << ":" << endl; cin >> numbers[i]; total=total+numbers[i]; } double average; int findmax; average=total/j; findmax=max(numbers); cout << average; cout << findmax; return 0; }```

I have a problem with this line:

Code:

`int numbers[j];`
it gives me these errors:

Quote:

j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2057: expected constant expression
j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2466: cannot allocate an array of constant size 0
j:\MaxAndAvg\MaxAndAvg.cpp(20): error C2133: 'numbers' : unknown size

All this needs to do is ask the user for how many numbers they want to calculate, add them up, then get the average and the largest number. Thanks for any and all help!
• 03-30-2007
The Brain
Thou shalt not attempt to create an array of variable size
Code:

```cout << "Please enter the amount of numbers you want to calculate: "; cin >> j; int numbers[j];  <-- illegal in c++```

1. declare an array of an arbitrary fixed size
Code:

`int numbers[100];`
2. use a dynamic array and create an array of any given size at runtime
Code:

```cout << "Please enter the amount of numbers you want to calculate: "; cin >> j; int *numbers = new int[j];```
3. Use an STL object such as <vector>. A vector class object simulates a resizable array.
Code:

```#include<vector> vector<int> numbers; int input = 0; cout << "Please enter number " << i << ":" << endl; cin >> input; numbers.push_back(input);```
• 03-30-2007
robatino
When you pass a to max(), it gets converted to a pointer, so sizeof(a) will just return the size of the pointer, not the array as you intended. You need to pass the size of the array to max() as a second argument.

Edit: You should also initialize maximum to INT_MIN (or the long-winded but macro-free numeric_limits<int>::min()) for sensible behavior in case the array's size is 0.

Edit: Also, if the array's size is 0, then a[0] doesn't even exist, so you can't use it. Initialize to one of the above and then loop from 0 to size-1.

Edit: Better yet, just use max() from the Standard Library.
• 03-30-2007
Sentral
Take out the line
Code:

`int numbers[j];`
And the program will work.

There is no need to use an array there.
• 03-30-2007
robatino
He needs to store all the numbers in order to call max() later (though the array could be avoided just by computing the maximum on the fly instead).
• 03-30-2007
robatino
I realized that j must be > 0, since the average of 0 numbers is 0/0 which is indeterminate. So just compute a running total and maximum as the numbers are entered in, and at the end, divide the total by j to get the average (remember that when dividing two ints, the result is truncated to an int, so convert either total or j to double before dividing). Since j > 0, you can just initialize total and maximum to the first number you enter. Forget about the INT_MIN stuff.