Thread: trouble creating an array

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    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 grade, average, total;
    float x[21}
    
    for (int i=0; i<=20; i++)
    {
    cout<<"Enter grade:";
    cin>>x[i];
    }
    
    total=total+grade;
    count=count+1;
    average=total/20;

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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

    You've never used grade

    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. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    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++)
    {
    cout<<"Enter grade:";
    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. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I done it before without the compiler having a problem.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    total=total+x;

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

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    5
    It is still good practice to initialize it to 0 anyway.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM