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.

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

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..

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?

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.

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

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" ?

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.

Edited 5 Years Ago by Mopikope: n/a

Comments
Thank you

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?

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

Edited 5 Years Ago by Mopikope: n/a

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

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);
}
#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.

Edited 5 Years Ago by itzcarol: n/a

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

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

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.

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

Edited 5 Years Ago by itzcarol: n/a

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.

Edited 5 Years Ago by Chilton: n/a

#include <iostream>

using namespace std;
int main()
{
    //Declare variables
    float grade[3]; // 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 = 0; count <= 3; count+1) //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 = 0; count <= 3; count+1) //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

Did you mean like this? I have changed the float grade[4] to grade[3] and for lines 15 and 29, I've changed them like how you have suggested..

#include <iostream>

using namespace std;
int main()
{
    //Declare variables
    float grade[3]; // 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 = 0; count <= 3; count+1) //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 = 0; count <= 3; count+1) //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

Did you mean like this? I have changed the float grade[4] to grade[3] and for lines 15 and 29, I've changed them like how yo have suggested..

Actually that's it 0, 1, 2. It's should be grade[5]. 0,1,2,3,4. The number of elements is (size-1).

Oh, Okay.. I get it now. So how exactly would I have to change

[B]for (count = 0; count <= 3; count+1)[/B]
    {
        cout << "Enter grade " << count << ": ";
        {
        cin >> grade[count]; 
        } //End of cout grade

    } //End of array


    cout << "You have entered the following grades: " << XXXXXXXXXXXXXX << endl; 


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

Thank you so much for your time and help. My lecture instructor is not every good. :/ So I seek youtube and this forum for help!

In your code there, you're not incrementing count in your for loop. Look your code over a bit.

It's confusing. :(

I'm looking back into my notes and are you talking about something like this?

for(initialization; condition; update statement;)
{
    block of code to repeat

}

example:

for( count = 0; count < 5; count = count + 1)

Edited 3 Years Ago by Nick Evan: Fixed formatting

Yes, so what you are looking for is something like this:

for (count=0;count<3;count++)
{
    cout<<"Element #"<<count+1/*to make it start counting at 1 instead of 0*/<<":"<<myarray[count]<<endl;
}
This article has been dead for over six months. Start a new discussion instead.