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:
it gives me these errors:Code:int numbers[j];
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!