1. The following structure holds the student grades in homeworks: typedef struct
ID: 3813491 • Letter: 1
Question
1. The following structure holds the student grades in homeworks: typedef struct char name Your name as string char grade Your letter grade as string, can be "A", "A-", "B+", "B", "B- "C+", "C", "C-", "D", "F" hw grade Write a function that compares the letter grades of two students and returns the result as a signed integer int Compare grades (hw grade student 1 hw grade student2) returns 1 when student1's grade is higher than student 2's grade, 0 when grades are equal, and 1 when student''s grade is lower than student2' s gradeExplanation / Answer
#include <stdio.h>
#include <string.h>
typedef struct //structure definition
{
char *name;
char *grade;
}hw_grade;
int compare_grades(hw_grade *student1,hw_grade *student2) //function
{
int result;
if(student1->grade[0] > student2->grade[0]) //compare first char of grade
result = 1;
else if (student1->grade[0] < student2->grade[0])
result = -1;
//compare first and second char of grade
else if(student1->grade[0] == student2->grade[0] && student1->grade[1] != student2->grade[1])
{
if(student1->grade[1] == '' > student2->grade[1] == '-')
result = 1;
else if(student1->grade[1] == '' > student2->grade[1] == '+')
result = -1;
}
//first and seconf char of grade are equal
else if(student1->grade[0] == student2->grade[0] && student1->grade[1] == student2->grade[1])
result = 0;
return result;
}
int main(void)
{
hw_grade student1 = {"Jim Lewis","A"};
hw_grade student2 = {"Jessica Williams","A+"};
int result = compare_grades(&student1,&student2);//pass addresses of structure variables as parameters
if(result == 1 )
printf("Student 1's grade is higher than Student 2's grade");
else if(result == 0)
printf("Student 1's grade is equal to Student 2's grade");
else if(result == -1)
printf("Student 1's grade is lower than Student 2's grade");
return 0;
}
Output:
Student 1's grade is higher than Student 2's grade
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.