Array[0]! What am I missing? Thanks :)

Nov 3, 2015 at 6:22am
The output should be like this:
Grade 1: 90
Grade 2: 86
Grade 3: 95
Grade 4: 76
Grade 5: 92
Grade 6: 83
Grade 7: 100
Grade 8: 87
Grade 9: 91
Grade 10: -1
Average Grade: 88%
/******************************************************************
#include <iostream>
using namespace std;
/******************************************************************
* This function will get the Grades!!
****************************************************************/
int getGrades()
{
const int SIZE = 10;
int Grades[SIZE];
int average[SIZE];

average = 0;

for (int i = 0; i < SIZE; i++)
// Fill the list
{
cout << "Grade " << i + 1 << ": ";
cin >> Grades[i];
average = average + Grades[i];
}
// get Average
average = average / SIZE;
cout << "Average Grade: " << average
<< "%"
<< endl;

}

/**************************************************
* main function
****************************************************/
int main()
{
// display the result

getGrades();

return 0;
}
Last edited on Nov 3, 2015 at 6:41am
Nov 3, 2015 at 11:21am
Did you receive any complier error?

Please try changing the line "int average[SIZE];" to "int average;" and see if it works.
Last edited on Nov 3, 2015 at 11:23am
Nov 3, 2015 at 2:44pm
This should be moved the Beginners board.

Post your code in code tags.
What exactly is your issue?
Average should not be an array.
Nov 3, 2015 at 3:39pm
This is my out put!
The output should be like this:
Grade 1: 90
Grade 2: 86
Grade 3: 95
Grade 4: 76
Grade 5: 92
Grade 6: 83
Grade 7: 100
Grade 8: 87
Grade 9: 91
Grade 10: 1
Average Grade: 89%

I don't how can I make it -1 and average grade should be % 88. How can I do that? Thanks a lot!
Nov 3, 2015 at 7:02pm
This is my out put!

What? Where?

I don't how can I make it -1

Make what -1?

average grade should be % 88. How can I do that?

Look up a formula for average.

Again, average should not be array. Your code shouldn't even compile how it is.
Nov 3, 2015 at 7:27pm
Have you corrected the line I mentioned earlier?

Did the code compile properly and did the program and output display?

If they did, please post the code that compiles. Maybe we will be able to see the problem that why you couldn't input a value of (-1) if you do.
Last edited on Nov 3, 2015 at 7:28pm
Nov 4, 2015 at 2:40am
Yes, I did change that and I think I'm very close. I came up with this loop but there is something need to be fixed. My output needs to be like that Exp: Average Grade: 88%\n

This is my code so far:
int getGrades()
{
const int SIZE = 10;
int grades[SIZE];
int average = 0;


for (int i = 0; i < SIZE; i++)
// Fill the list
{
cout << "Grade " << i + 1 << ": ";
cin >> grades[i];

average += grades[i];
if (grades >= 0)
average++;
else
return -1;
}




Started program
> Grade 1: 90
> Grade 2: 86
> Grade 3: 95
> Grade 4: -1
> Grade 5: 92
> Grade 6: 83
> Grade 7: 100
> Grade 8: 87
> Grade 9: 91
> Grade 10: 76
> Average Grade: 80%\n
Exp: Average Grade: 88%\n
Program terminated successfully

Started program
> Grade 1: -1
> Grade 2: -1
> Grade 3: -1
> Grade 4: -1
> Grade 5: -1
> Grade 6: -1
> Grade 7: -1
> Grade 8: -1
> Grade 9: -1
> Grade 10: -1
> Average Grade: 0%\n
Exp: Average Grade: ---%\n
Program terminated successfully

Failed 3/3 tests

Thanks you so much guys your advice has been very helpful!

Last edited on Nov 4, 2015 at 2:41am
Nov 4, 2015 at 6:09am
You should try changing the line "if(grades >= 0)" to "if(grades[i] >= 0)".

Additionally, what is "Exp"? What are you going to do if some certain grade happens to be (-1)?
Last edited on Nov 4, 2015 at 6:18am
Nov 4, 2015 at 6:29am
This is so far what I got for the output. The Exp is the right output. So when I run my program through textBed it gives me the result below. I'm very close but I still need an advice.

int main()
{

getGrades();
getAverage();

return 0;
}


int getGrades()
{
int grades[10];

for (int i = 1; i <= 10; i++)
{
cout << "Grade " << i << ": "; //prompts user for grades
cin >> grades[i]; // adds values to array
}
}



int getAverage()
{
int grades[10];
int average;
int sum = 0;
int count = 0;

cout << "Average Grade: " << average
<< "%"
<< endl;

for (int i = 0; i <= 10; i++)
{
if (grades[i] > 0)
sum += grades[i];
}
average = (count / 10);
if (average == 0)
count++;
return -1;
}



Started program
> Grade 1: 90
> Grade 2: 86
> Grade 3: 95
> Grade 4: 76
> Grade 5: 92
> Grade 6: 83
> Grade 7: 100
> Grade 8: 87
> Grade 9: 91
> Grade 10: -1
> Average Grade: 87%\n
Exp: Average Grade: 88%\n
Program terminated successfully

Started program
> Grade 1: 90
> Grade 2: 86
> Grade 3: 95
> Grade 4: -1
> Grade 5: 92
> Grade 6: 83
> Grade 7: 100
> Grade 8: 87
> Grade 9: 91
> Grade 10: 76
> Average Grade: 87%\n
Exp: Average Grade: 88%\n
Program terminated successfully

Started program
> Grade 1: -1
> Grade 2: -1
> Grade 3: -1
> Grade 4: -1
> Grade 5: -1
> Grade 6: -1
> Grade 7: -1
> Grade 8: -1
> Grade 9: -1
> Grade 10: -1
> Average Grade: -1%\n
Exp: Average Grade: ---%\n
Program terminated successfully
Last edited on Nov 4, 2015 at 6:30am
Nov 4, 2015 at 10:53pm
Thank you so MUCH I got it :)
Topic archived. No new replies allowed.