2) [42] Write a program to manage the grades of students in a class. To represen
ID: 3607700 • Letter: 2
Question
2) [42] Write a program to manage the grades of students in a class. To represent the information of each student define a structure type named student. The members of this structure should be: 1. 2. 3. 4. 5. 6. an integer that represents the student's ID number, an array of 15 characters storing the first name, an array of 15 characters storing the last name, an integer that represents the project 1 grade an integer that represents the project 2 grade, a floating point representing the final course grade To store the list of students use an array of pointers of type student*. Each pointer in the array has to point to a structure variable of type student representing the information of one student. The array has to be SORTED in increasing order of students' ID numbers.Explanation / Answer
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
typedef struct {
int IDno;
char firstName[15];
char LastName[15];
int gradeProject1;
int gradeProject2;
float finalCourseGrade;
}student;
student **create_class_list(char *filename, int *sizePtr);
void print_list(student **list, int * size);
int find(int idNo, student **list, int size);
void withdraw(int idNo, student **list, int *sizePtr);
void destroy_list(student **list, int *size);
int main(void) {
int size, ind, searchID = 2;
student **studentList = NULL;
studentList = create_class_list("student.txt", & size);
if (!studentList) {
fprintf(stderr, "error: create_class_list failing. ");
return 1;
}
print_list(studentList, & size);
ind = find(searchID, studentList, size);
withdraw(3, studentList, & size);
destroy_list(studentList, & size);
return 0;
}
student **create_class_list(char *filename, int *sizePtr) {
int loc;
FILE *fileptr;
fileptr = fopen(filename, "r");
if (fileptr == NULL) {
printf("Sorry, the file can not be opened. ");
} else {
fscanf(fileptr, "%d", sizePtr);
}
loc = *sizePtr;
student **list;
list = (student**) calloc(loc, sizeof(student* ));
for (int i = 0; i < loc; i++) {
list[i] = (student*) calloc(loc, sizeof(student));
fscanf(fileptr, "%d %[^ ]s", & (list[i] - > IDno), (list[i] - > name));
}
//sorting
for (int i = 0; i < loc; i++) {
if (list[i] - > IDno > list[i + 1] - > IDno) {
student **tempList;
tempList = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
return list;
}
int find(int idNo, student * * list, int size) {
int middle;
int loc = * sizePtr;
int lowest = list[0];
int highest = list[loc];
while (lowest <= highest) {
middle = (lowest + highest) / 2;
printf("%d ", middle);
if (idNo == list[middle] - > IDno)
return list[middle] - > IDno;
if (idNo < list[middle] - > IDno)
highest = middle - 1;
else
lowest = middle + 1;
}
return -1;
}
void print_list(student * * list, int * size) {
for (int i = 0; i < * size; i++) {
printf("%d %s %s ", list[i] - > IDno, list[i] - > firstName, list[i] - > lastName);
}
}
void withdraw(int idNo, student * * list, int * sizePtr) {
for (int i = 0; i < * sizePtr; i++) {
if (idNo == list[i] - > IDno) {
free(list[i]))
}
}
}
void destroy_list(student **list, int *size) {
free(list);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.