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;
 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 ---> ";
 cin >> Grades;
 while ( x < 11)
 {
        if (Grades < 60)
        {
         cout << "F ---> "<<students[x];
         }
         else if ( Grades >=60 || Grades <= 69)
         {
              cout << "D ----> " <<students[x];
           }
           else if (Grades >= 70 || Grades <= 79)
             {
             cout << "C ---> " <<students [x];
               }
           else if ( Grades >= 80 || Grades <= 89)
              {
                   cout << "B ---> "<<students [x];
                    }
                   else if (Grades >= 90 || Grades <= 99)
                  {
                        cout << "A ---> "<<students [x];
                        }
                     x = x + 1;                       
             }
             // Call calculateGrades function here		
 
   system("PAUSE");
   return EXIT_SUCCESS;
 } // End of main() function
	
   // Write calculateGrades function here

Edited 7 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

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()
{
...
...
    foo( grades, students, size );
}

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;
 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 ---> ";
 cin >> grades;
 while ( x < 11)
 {
        if (grades < 60)
        {
         cout << "f ---> "<<students[x];
         }
         else if ( grades >=60 || grades <= 69)
         {
              cout << "d ----> " <<students[x];
           }
           else if (grades >= 70 || grades <= 79)
             {
             cout << "c ---> " <<students [x];
               }
           else if ( grades >= 80 || grades <= 89)
              {
                   cout << "b ---> "<<students [x];
                    }
                   else if (grades >= 90 || grades <= 99)
                  {
                        cout << "a ---> "<<students [x];
                        }
                     x = x + 1;                       
             }
             // call calculategrades function here		
 
   system("pause");
   return exit_success;
 } // end of main() function
	
   // write calculategrades function here

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
char get_grade(int&);

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

//Function Definition
char get_grade(int& score)
{
     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';
}

Edited 7 Years Ago by Clinton Portis: n/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
char get_grade(int&);

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

//Function Definition
char get_grade(int& score)
{
     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
char get_grade(int&);

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
 char get_grade(int score)  // Write calculateGrades function here
{
    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';
 }

Edited 7 Years Ago by Nick Evan: Removed linenumbers and added code-tags

Comments
Stop screaming at us by using all UPPER CASE characters.

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.

Edited 7 Years Ago by Clinton Portis: dark magic.

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)
        grade = 'F';
    else if(score <= 69)
        grade =  'D';
    else if(score <= 79)
          grade =  'C';
    else if(score <= 89)
        grade =  'B';
    else
        grade = 'A';
   return grade;

Edited 7 Years Ago by Ancient Dragon: n/a

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

thanks a lot....i was working for this program since past 3 days, thanks to you and all who helped me solving this program.

This article has been dead for over six months. Start a new discussion instead.