# Thread: trouble creating an array

1. ## trouble creating an array

I'm trying to write an array that prompts for 20 grades. I then want to calculate the average and then list the grades in the order they were entered and how many points (+ or -) the individual grade was from the average. I've started the first part but now I'm stuck how to output the corrct way using an array. Here is what I have so far:

Code:
```#include<iostream.h>
#include<stdlib.h>

int main()
{
float x[21}

for (int i=0; i<=20; i++)
{
cin>>x[i];
}

count=count+1;
average=total/20;```

2. Code:
`float x[21}`
Should be
Code:
`float x[21]`
You should initalize total to 0 and add the grades to it as they are inputed

Code:
`for (int i=0; i <=20; i++)`
this will get 21 grades not 20

Ok once you get your average just go back through the loop and display the difference:

Code:
```for (int i=0; i < 20; i++ )
cout << "The grade of "<<x[i]<< " has a " << (x[i]-average) << "point difference from the average of "<<average<<endl;```

3. Ok...thanks Thantos. I made the changes but the compiler is kicking back errors at me. The error I get is:

c:\dev-c_~1\array.cpp: In function `int main()':
c:\dev-c_~1\array.cpp:16: invalid operands `float' and `float[21]' to binary `operator +'

Here is my code:

Code:
```#include<iostream.h>
#include<stdlib.h>

int main()
{

float x[21], average, total;
int count;

for (int i=0; i<=20; i++)
{
cin>>x[i];
}

total=total+x;
count=count+1;
average=total/20;

{
for (int i=0; i < 20; i++ )
cout << "The grade of "<<x[i]<< " has a " << (x[i]-average) << "point difference from the average of "<<average<<"\n";
}

system("PAUSE");
return 0;
}```

4. you can't add an array to a float.

Also you need to indent and you still haven't initalized total so you are still adding garbage.

5. I done it before without the compiler having a problem.

6. total=total+x;

I done it before without the compiler having a problem.
Not likely.

7. It is still good practice to initialize it to 0 anyway.

8. ...also how about when you post code, you but a big red THE ERROR IS ON THIS LINE on the line in your code with the error.