954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array asking for 4 grades and solving the average?

Hello Guys!

I need help for an assignment. Somehow my code will not correspond to what I'm looking for. I'm having a hard time to find what the problem is. If you guys can please help me, I'll be more than appreciate !!!!!

1) Create a program that will read in four grades entered by the user and store them in an array. The program will calculate the average of the four grades and print the average to the screen as well as the four grades that the user entered. Test your program.

Input: four grades
Output: The four grades and the average of the four grades.

This is what I have so far:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int grade[4];
    int count;
    int total = 0;
    double average;

    for (count = 0; count <= 3; count++)
    {
        cout << "Enter grade " << count << " ";
        {
        while(grade[count] < 0.0 || grade[count] > 4.0)
        cin >> grade[count];
        }
    }

    for(count = 0; count <= 3; count++)
        total = total + grade[count];
    average = total/4;
    cout << "Your average of four grades are: " << average << endl;

    return 0;
}


Thank you so much for your time.

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

int types are integers, that means they can only store whole numbers. You need the float type, which stores floating point numbers.

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

If you run the code for what I have, it don't look right. :( and I can't seem to find what the problem is..

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Enter grade 0 Enter grade 1 Enter grade 2 Enter grade 3 Your average of four grades are: 0


^^ that's what pop out on the screen. i can't even enter a grade for it.

did you mean instead of putting "int" put float?

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Your asking the user to enter a grade, then testing the array's data before they even enter any information. Once you declare your array, its data is going to be garbage, so that's what you'll be testing against.

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

You don't need the while loop. What you are saying is while grade[count] is less than 0 or greater than 4 which doesn't make sense to me. Just like Labdabeta said you need to change the int to float because if you keep the int instead of saying 56.90 it will say 57. Hopes this helps

Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Thank you so much! I finally got it.

The output is now

Enter grade 0 88
Enter grade 1 80
Enter grade 2 99
Enter grade 3 68
Your average of four grades are: 83


How can I make it start from "1" instead of "0" ?

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

I would say make both (count = 1; count <= 4; count++) so that it starts from 1 and goes up to 4 instead of 0 to 3.

Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

How would I print the four grades that the user entered along with the average? Because the assignment stated:

The program will calculate the average of the four grades and print the average to the screen as well as the four grades that the user entered. Test your program.

What is display on my screen is:


Enter grade 1: 94
Enter grade 2: 88
Enter grade 3: 80
Enter grade 4: 95
Your average of four grades are: 89.25


^^ seems about right?

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Just print out the array starting from 1 to 4.

Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry, how would I do that?

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Just like you were outputing "Enter grade" you do the exact same way for displaying array elements. Try it and post the code.

Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Thanks!

Do you know how I will allow the user to input as many sets of student grades as he/she wants and the user end at any time...? I'm trying to make each student have four grades and one average grade. Then the user can decide to stop at any time before entering a new set of four grades for a student. You know what I'm getting at? lol

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

you can use a do-while loop with a if-statement to determine the stoppage.

int main()
{
bool something = true;
do{

// statements


if(input is true) bool equal false
}while(bool is true);
}
Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
#include <iostream>

using namespace std;
int main()
{
    //Declare variables
    float grade[4]; // 4 array slots
    int count;
    float total = 0; //Set to 0 so total will start with 0 before calculating average.
    double average; //Average will be display in decimal



    for (count = 1; count <= 4; count++) //Will display number 1-4 (++) will increase each number.
    {
        cout << "Enter grade " << count << ": ";
        {
        cin >> grade[count]; //Output of grade entered with the number increasing 1
        } //End of cout grade

    } //End of array


    //printing the grades entered to be display on screen again.
    cout << "You have entered the following grades: " << XXXXXXXXXXXXXX << endl; 


    for(count = 1; count <= 4; count++) //Starting with number 1. Number will increase eachtime after number is entered.
        total = total + grade[count];
    average = total/4;
    cout << "Your average of four grades are: " << average << endl; //Output of average


    return 0;
} //End of program


You can see where I put XXXXXXX on line 37 I didn't know what to insert there so the grades 1-4 can be display again along with the average at the bottom.

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

you cant you cin because you are outputting things to the screen. Search for Printing array in c++, and you will find it.

Mopikope
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I just noticed and just now changed it. Thanks, i will do! Thank you so much for your help. :-)

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

grade[4] is undefined, since the array goes from 0 to 3. If you want to prompt the user to print the first through fourth grades, then keep your loop from 0 to <= 3 but simply print count + 1.

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

What do you mean undefined? I was trying to make the grades display again along with the average calculated. You know?

If you look at the code I have posted above, you will see what I mean. Where I have put "XXXXXXX" on line 37

itzcarol
Newbie Poster
24 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Your array has subscripts ranging from 0 to 3 (4 elements), so when you try to access the array past that point, such as grade[4], it is going to cause you problems.

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: