Thread: Help with Array Program!

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Help with Array Program!

    Hey Guys, I'm Having trouble with this program..

    Assignment: ask how many students test scores user wants to input. Using loop structure, enter the test scores(must be at least 6). Program should count and print number of A's, B's, C's, D's, and F's. It should Average and find the largest of all scores.

    MY PROBLEM: finding the largest of all scores...here is my code.

    #include <iostream.h>
    #include <stdlib.h>

    main()
    {int Num, C, Sum=0, Tot1=0, Tot2=0, Tot3=0, Tot4=0, Tot5=0, Tot6=0, Grade=0; float Avg;

    cout<<"\nHow Many Test Scores do you want to enter?- You Must enter at least 6: ";cin>>Num;

    if(Num<6)
    {cout<<"\nSorry, You Must enter at least 6 Test Scores: ";}
    else{
    int Test[Num-1];

    for(C=0;C<Num;C++)
    {cout<<"\n Enter Score "<<C+1<<": "; cin>>Test[C];

    if(Test[C]<=100 && Test[C]>=90)
    {Grade=1;
    Tot1=Tot1+Grade;}
    else if(Test[C]<90 && Test[C]>=80)
    {Grade=1;
    Tot2=Tot2+Grade;}
    else if(Test[C]<80 && Test[C]>=70)
    {Grade=1;
    Tot3=Tot3+Grade;}
    else if(Test[C]<70 && Test[C]>=60)
    {Grade=1;
    Tot4=Tot4+Grade;}
    else if(Test[C]<60 && Test[C]>=0)
    {Grade=1;
    Tot5=Tot5+Grade;}
    else
    {Grade=1;
    Tot6=Tot6+Grade;}

    Sum=Sum + Test[C];
    }
    Avg=Sum/Num;

    cout<<"\n The Average is "<<Avg;
    cout<<"\n There are "<<Tot1<<" A's ";
    cout<<"\n There are "<<Tot2<<" B's ";
    cout<<"\n There are "<<Tot3<<" C's ";
    cout<<"\n There are "<<Tot4<<" D's ";
    cout<<"\n There are "<<Tot5<<" F's ";
    cout<<"\n There are "<<Tot6<<" Invalid Grades ";
    }
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This is wrong:

    int Test[Num-1];
    Aside from the fact that the number of entries should be Num and not Num-1 you will need to use the new operator to dynamically declare the array like such:

    Code:
    int *Test = new int[Num];
    Don't forget to call delete right after the last cout to free up the memory you allocated:

    Code:
            cout<<"\n There are "<<Tot5<<" F's "; 
            cout<<"\n There are "<<Tot6<<" Invalid Grades "; 
            delete [] Test;
        } 
        system("PAUSE");
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >MY PROBLEM: finding the largest of all scores
    Keep a maximum variable, test each score against that and if the score is greater, assign the score to the maximum variable. When you get to the end of the score the maximum variable will contain the largest score.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  3. help with small program. array issue
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 12:26 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM