Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using c++ define a struct student which has three properties: id, score and grad

ID: 3683348 • Letter: U

Question

using c++ define a struct student which has three properties: id, score and grade. id is in integer type , score is a double type and grade is a char type. using a student to declare 20 elements in the main(); assign the ids of the student from 1 to 20, assign the scores of each student with random within [10,99]; then define a function and pass the student array as parameter(s). in the function ,assign the grade os each student according to his scores (e.g if score is >=90 A else if score >=80 B else if score >=70 C else if score >=60 D othewise F. after the function returns , print all students ids, score , and grades accordingly. b) redefine the function to assign grades, but change it to a recursive one .

Explanation / Answer

Iterative AssignGrade Method

#include<iostream>
#include<cstdlib>

using namespace std;

struct Student{
   int id;
   double score;
   char grade;
};

// function to generate double random number in range 10-99
double fRand();
// function that assign grade to each student
void assignGrade(Student students[]);

int main(){
  
   // declaring Student array of size 20
   Student students[20];
  
   // assigning id and numbers for each student
   for(int i=0; i<20; i++){
       students[i].id = i+1;
       students[i].score = fRand(); // random number in range 10-99
   }
  
   //assigning grade to each student
   assignGrade(students);
  
   //printing is, score and grade of each student
   for(int i=0; i<20; i++){
       cout<<"Id: "<<students[i].id<<", Score: "<<students[i].score<<", Grade: "<<students[i].grade<<endl;
   }
  
   return 0;
}

void assignGrade(Student students[]){
  
   for(int i=0; i<20; i++){
      
       double score = students[i].score;
      
       if(score >= 90){
           students[i].grade = 'A';
       }
       else if(score >= 80){
           students[i].grade = 'B';
       }
       else if(score >= 70){
           students[i].grade = 'C';
       }
       else if(score >= 60){
           students[i].grade = 'D';
       }
       else{
           students[i].grade = 'F';
       }
   }
}

double fRand()
{
   double fMin = 10;
   double fMax = 99;
  
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}

/*

Sample run:

Id: 1, Score: 84.7767, Grade: B
Id: 2, Score: 45.1001, Grade: F
Id: 3, Score: 79.6958, Grade: C
Id: 4, Score: 81.0612, Grade: B
Id: 5, Score: 91.1366, Grade: A
Id: 6, Score: 27.5821, Grade: F
Id: 7, Score: 39.8348, Grade: F
Id: 8, Score: 78.3724, Grade: C
Id: 9, Score: 34.7219, Grade: F
Id: 10, Score: 59.3033, Grade: F
Id: 11, Score: 52.4883, Grade: F
Id: 12, Score: 65.9695, Grade: D
Id: 13, Score: 42.4658, Grade: F
Id: 14, Score: 55.6927, Grade: F
Id: 15, Score: 94.7484, Grade: A
Id: 16, Score: 91.5414, Grade: A
Id: 17, Score: 66.5783, Grade: D
Id: 18, Score: 73.8394, Grade: C
Id: 19, Score: 22.6026, Grade: F
Id: 20, Score: 64.0202, Grade: D

*/

Using Recursive Assign Grade Method

#include<iostream>
#include<cstdlib>

using namespace std;

struct Student{
   int id;
   double score;
   char grade;
};

// function to generate double random number in range 10-99
double fRand();
// function that assign grade to each student
void assignGradeRecursive(Student students[], int index);

int main(){
  
   // declaring Student array of size 20
   Student students[20];
  
   // assigning id and numbers for each student
   for(int i=0; i<20; i++){
       students[i].id = i+1;
       students[i].score = fRand(); // random number in range 10-99
   }
  
   //assigning grade to each student
   assignGradeRecursive(students, 0); // passing initial index 0
  
   //printing is, score and grade of each student
   for(int i=0; i<20; i++){
       cout<<"Id: "<<students[i].id<<", Score: "<<students[i].score<<", Grade: "<<students[i].grade<<endl;
   }
  
   return 0;
}


double fRand()
{
   double fMin = 10;
   double fMax = 99;
  
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}

void assignGradeRecursive(Student students[], int index){
  
       //base case
       if(index == 20) // done with all students
       return ;
      
       // otherwise assign grade to student stored at index
       double score = students[index].score;
      
       if(score >= 90){
           students[index].grade = 'A';
       }
       else if(score >= 80){
           students[index].grade = 'B';
       }
       else if(score >= 70){
           students[index].grade = 'C';
       }
       else if(score >= 60){
           students[index].grade = 'D';
       }
       else{
           students[index].grade = 'F';
       }
      
       // recursively calling for index+1 student
       assignGradeRecursive(students, index+1);
}