This program passes two parallel arrays ta a function that prints the grade reports i.e Jay Rino -- Grade: A, I had coded this program as per my knowledge but it does not executed well please help me solve this. I know there is no user input needed but I dont know how to pass that using arrays & functions, please help me.

``````// StudentGrades.cpp - This program assigns a letter grade to a student.
// Input:  None
// Output:  Student name and grade

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

// Write function declaration here
int main(int argc, char *argv[])
{
const int size=10;
double grades[size] = {76, 65, 59, 98, 92, 88, 76, 54, 93, 78};
string students[size] = {"Ray Evans", "Sonia Bell", "Tim Egan", "Karen Davis", "Dan Peters",            "Jean Janson", "Ellen Thomas", "Susan Swanson", "Ellie Rodriguez", "Gupta Patel"};
int x=0;
cout << "Enter grade ---> ";
while ( x < 11)
{
{
cout << "F ---> "<<students[x];
}
{
cout << "D ----> " <<students[x];
}
{
cout << "C ---> " <<students [x];
}
{
cout << "B ---> "<<students [x];
}
{
cout << "A ---> "<<students [x];
}
x = x + 1;
}

system("PAUSE");
return EXIT_SUCCESS;
} // End of main() function

## All 11 Replies

Why did you make grades a double instead of an integer? doubles are not necessary in your program because nobody will have a grade of something like 75.123 (at least I never heard of such a thing).

First thing is to make a function that takes two arrays as arguments

``````void foo( int grades[], string students[] )
{

}``````

Notice in the above you don't know how many elements are in each of the two arrays. One way to fix that is to pass it as another parameter

``````void foo( int grades[], string students[] , const int size)
{

}``````

In the above function just create a simple for loop and display the information for each of the elements of the arrays.

in main() all you have to do is call that function

``````int main()
{
...
...
}``````

But then how can i convert the grades to grades (letter) i.e. (a, b, c, c, or f)? I am still confused.

this program passes two parallel arrays ta a function that prints the grade reports i.e jay rino -- grade: A, i had coded this program as per my knowledge but it does not executed well please help me solve this. I know there is no user input needed but i dont know how to pass that using arrays & functions, please help me.

``````// studentgrades.cpp - this program assigns a letter grade to a student.
// input:  None
// output:  Student name and grade

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

// write function declaration here
int main(int argc, char *argv[])
{
const int size=10;
double grades[size] = {76, 65, 59, 98, 92, 88, 76, 54, 93, 78};
string students[size] = {"ray evans", "sonia bell", "tim egan", "karen davis", "dan peters",            "jean janson", "ellen thomas", "susan swanson", "ellie rodriguez", "gupta patel"};
int x=0;
cout << "enter grade ---> ";
while ( x < 11)
{
{
cout << "f ---> "<<students[x];
}
{
cout << "d ----> " <<students[x];
}
{
cout << "c ---> " <<students [x];
}
{
cout << "b ---> "<<students [x];
}
{
cout << "a ---> "<<students [x];
}
x = x + 1;
}

system("pause");
return exit_success;
} // end of main() function

But then how can i convert the grades to grades (letter) i.e. (a, b, c, c, or f)? I am still confused.

I'm not sure what you are trying to do, but maybe this will help:

``````//Function Prototype

for(int i=0; i<10; i++)
{
cout << students[i] << " scored " << grades[i] << "% which is a " << get_grade(grades[i]) << endl;
}

//Function Definition
{
if(score < 60)

return 'F';

else if(score >= 60 || score <= 69)

return 'D';

else if(score >= 70 || score <= 79)

return 'C';

else if(score >= 80 || score <= 89)

return 'B';

else

return 'A';
}``````

WHAT I AM TRYING TO DO IS THIS PROGRAM WILL DISPLAY STUDENTS NAME AND GRADE (LETTER) WITHOUT ANY USER INPUT I.E. Ray Evans ---> C AND SO ON....

I'm not sure what you are trying to do, but maybe this will help:

``````//Function Prototype

for(int i=0; i<10; i++)
{
cout << students[i] << " scored " << grades[i] << "% which is a " << get_grade(grades[i]) << endl;
}

//Function Definition
{
if(score < 60)

return 'F';

else if(score >= 60 || score <= 69)

return 'D';

else if(score >= 70 || score <= 79)

return 'C';

else if(score >= 80 || score <= 89)

return 'B';

else

return 'A';
}``````

You now have all the pieces you need, all you need to do now is put them together. In your original program, just delete lines 19-45 because they are not necessary. Then just write a simple for loop that displays the values of students and grades arrays, like the code Clinton already gave you.

WHAT I AM TRYING TO DO IS THIS PROGRAM WILL DISPLAY STUDENTS NAME AND GRADE (LETTER) WITHOUT ANY USER INPUT I.E. Ray Evans ---> C AND SO ON....

THIS IS WHAT I DONE AND WHEN I TRIED TO RUN THIS PROGRAM HAVING BUNCH OF ERRORS. PLEASE HELP.

``````// StudentGrades.cpp - This program assigns a letter grade to a student.
// Input:  None
// Output:  Student name and grade

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

// Write function declaration here

int main(int argc, char *argv[])
{
double grades[] = {76, 65, 59, 98, 92, 88, 76, 54, 93, 78};
string students[] = {"Ray Evans", "Sonia Bell", "Tim Egan", "Karen Davis", "Dan Peters",            "Jean Janson", "Ellen Thomas", "Susan Swanson", "Ellie Rodriguez", "Gupta Patel"};

for (int i=0; i<10; i++)
{
cout << students[i] << " scored " << grades[i] << "% which is a " << get_grade(grades[i]) << endl;
}
}
system("PAUSE");
End of main() function
{
if(score < 60)
return 'F';
else if(score >= 60 || score <= 69)
return 'D';
else if(score >= 70 || score <= 79)
return 'C';
else if(score >= 80 || score <= 89)
return 'B';
else
return 'A';
}``````
commented: Stop screaming at us by using all UPPER CASE characters. -5

Line #23: you have an unecessary } curly brace

main() does not return any value, although you have it prototyped to return an 'int'.

Line #25: should be // commented out

You should have a } curly brace after line #24

The parameter list in line #26 does not match its prototype in line #12.

Line #23: you have an unecessary } curly brace

main() does not return any value, although you have it prototyped to return an 'int'.

Line #25: should be // commented out

You should have a } curly brace after line #24

The parameter list in line #26 does not match its prototype in line #12.

I did what you told me, and program is working good, but problem is I am getting output grades is D and F only, non of other grades were showing up i.e. if the score is 89 ---> D instead of B please help me with this.

I think your || should be && cause 89 is greater than 60 so it short circuits and you get anything over 60 as a D.

could be re-written like this: Notice you don't need || at all. And that function should only have a return statement at the end of the function, not on every line.

``````char grade;
if(score < 60)
else if(score <= 69)
else if(score <= 79)