IN C PLEASE Given the following structure definition for a student_data: typedef
ID: 3547173 • Letter: I
Question
IN C PLEASE
Given the following structure definition for a student_data:
typedef struct{
char last_name[STRSIZE];
char first_name[STRSIZE];
int id;
double scores[5];
int rank;
} student_data;
Write the following functions:
a. /* function to input data in a student_data structure*/
student_data get_student();
b. /* returns the id (an int) of the student who holds
the highest rank in an array of student_data called list.
Assume no students hold the same rank - NOTE the rank is determined by test scores
and other criteria so it may not reflect test scores only */
/* int n is the number of students in the array of student_data */
int best_student(student_data list[], int n);
c. /* function which returns the highest test score for an INDIVIDUAL student */
double individual_highest_score(student_data person);
d./* function which returns the highest test score of all the students in an array of student_data */
/* int n is the number of students in the array of student_data */
double highest_score_list(student_data list[], int n);
The main program should:
a. get the data for an individual student
b. find the highest score for the individual student and print it out with the student's ID No.
c. get the data for an array of student_data of size 7
d.. find the student in the array with the best rank which is the lowest number i. e.
Rank 1 is the best while Rank 7 is the worst.
and print out the id number of the student with the best rank.
e. find the highest score of all the scores for all the students in the array of student_data and print out the highest score attained by anyone.
Explanation / Answer
#include <stdio.h>
#define STRSIZE 10
typedef struct{
char last_name[STRSIZE];
char first_name[STRSIZE];
int id;
double scores[5];
int rank;
} student_data;
student_data get_student() {
int i;
student_data data;
scanf("%s", data.last_name);
scanf("%s", data.first_name);
scanf("%d", &data.id);
for(i=0;i<5;i++)
scanf("%lf", &data.scores[i]);
scanf("%d", &data.rank);
return data;
}
int best_student(student_data list[], int n)
{
int i;
student_data d = list[0];
for(i = 0; i<n; i++) {
if(list[i].rank < d.rank)
d = list[i];
}
return d.id;
}
double individual_highest_score(student_data person)
{
int i;
double max = -1;
for(i=0;i<5;i++) {
printf("%f ", person.scores[i]);
if(person.scores[i] > max)
max = person.scores[i];
}
return max;
}
double highest_score_list(student_data list[], int n)
{
int i,j;
double score = -1;
for(i = 0; i<n; i++) {
for(j=0;j<5;j++) {
if(score > list[i].scores[j])
score = list[i].scores[j];
}
}
return score;
}
int main()
{
int i = 0, j = 0;
double score;
student_data d, arr[7];
d = get_student(); //get the data for an individual student
score = individual_highest_score(d); // find the highest score for the individual student
printf("ID: %d Score: %f ", d.id, score);
//get the data for an array of student_data of size 7
for(i = 0; i<3; i++) {
arr[i] = get_student();
}
//student in the array with the best rank
printf("Student with best rank: %d", best_student(arr, 7));
//highest score attained by anyone.
printf("Highest score attained: %d", highest_score_list(arr,7));
return 0;
}