Page 1 of 1

Please help me with this code: #include "leak_detector_c.h" #include #include #include #

Posted: Thu May 26, 2022 9:18 am
by answerhappygod
Please help me with this code:
Please Help Me With This Code Include Leak Detector C H Include Stdio H Include Stdlib H Include String H 1
Please Help Me With This Code Include Leak Detector C H Include Stdio H Include Stdlib H Include String H 1 (85.21 KiB) Viewed 15 times
#include "leak_detector_c.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAMEMAX 51
typedef struct student {
char *lname;
int assignment;
int finalExam;
int total;
int *quizzes;
} student;
student **readCourses(FILE *in, int *C, int *N, int *M)
{
student **courses;
char lname[NAMEMAX];
int i, j;
fscanf(in, "%d %d %d", C, N, M);
courses = malloc(sizeof(struct student *) *
(*C));
int cn, sn, qn;
for (cn = 0; cn < *C; cn++) {
courses[cn] = malloc((*N) * sizeof(struct
student));
for (sn = 0; sn < *N; sn++) {
int total = 0;
fscanf(in, "%s", lname);
int length = strlen(lname);
courses[cn][sn].lname = malloc((length + 1) *
sizeof(char));
strcpy(courses)[cn][sn].lname;
fscanf(in, "%d",
&courses[cn][sn].assignment);
total +=
courses[cn][sn].assignment;
courses[cn][sn].quizzes =
malloc(sizeof(int) * (*M));
for (qn = 0; qn < *M; qn++) {
fscanf(in, "%d",
&courses[cn][sn].quizzes[qn]);
total +=
courses[cn][sn].quizzes[qn];
}
fscanf(in, "%d",
&courses[cn][sn].finalExam);
total += courses[cn][sn].finalExam;
courses[cn][sn].total = total;
}
}
return courses;
}
void printHighestTotal(FILE *outFile, student **courses,
int C, int N, int M);
{
int maxTotal = -1;
int maxCourseIdx = 0;
int maxStudentIdx = 0;
for (int cn = 0; cn < C; cn++) {
for (sn = 0; sn < N; sn++) {
if (maxTotal < courses[cn][sn].total)
{
maxTotal = courses[cn][sn].total;
maxStudentIdx = sn;
maxCourseIdx = cn;
}
}
}
printf("Quizzes: ", j);
fprintf(outfile, "Quizzes: ");
for (int q = 0; q < M; q++) {
printf("%d",
courses[maxCourseIdx][maxStudentIdx].quizzes[q]);
fprintf(outFile, "%d",
courses[maxCourseIdx][maxStudentIdx].quizzes[q]);
}
}
void release_memory(student **st, int C, int N, int M) {
for (int cn = 0; cn < C; cn++) {
for (int sn = 0; sn < N; sn++) {
free(st[cn][sn].lname);
free(st[cn][sn].quizzes);
}
}
}
int main() {
atexit(report_mem_leak);
student **courseList;
FILE *inFile, *outFile;
int i, j, C, N, M;
inFile = fopen("in.txt", "r");
if (inFile != NULL) {
courseList = readCourses(inFile, &C, &N,
&M);
outFile = fopen("out.txt", "w");
printHighestTotal(outFile, courseList, C, N,
M);
release_memory(courseList, C, N, M);
fclose(inFile);
fclose(outFile);
} else {
printf("Please provide correct input file");
exit(-1);
}
return 0;
}
The Coding Problem: In this problem, you will read a set of students data and their grading information from a file and then process them and then write the requested data to another file. Problem: ABC training center offers C number of courses. A course has N number of students. A student has a last name (single word string with maximum length 50 characters) and a set of course activities, including one assignment, M number of quizzes and a final exam. The total score of a student is calculated as follows: The total score = sum of scores from M quizzes + score in the assignment + score in the final exam The student structure should follow the following definition. typedef struct student { char *1name; //this will require DMA to store a string int assignment; int finalExam; int total; int *quizzes; //this will require DMA to store quizzes } student; You can visualize it by the following picture. student[0] Student[N-1] Course[0] q[0] q[0] q[0] q[0] **** **** **** q[M-1] q[M-1] q[M-1] q[M-1] student[0] q[0] q[0] q[0] .... **** **** q[M-1] q[M-1] q[M-1] Course[1] Course[2] Course[C-1] Student[N-1] q[0] **** q[M-1]
All the inputs are available in a text file in.txt. Take number of Courses C, number of students N and number of quizzes M as the input from the first line of the input file. Then Dynamically allocate memory for C courses with N students for each course. For each student, dynamically allocate memory to store scores of M quizzes. Take input for all the scores for quizzes, assignments, and final exams for all the students. Calculate the total scores for each student and store it in the corresponding structure. Do this whole task in a separate method and return the appropriate pointer. The function header should look like this: student** readCourses(FILE *in, int *C, int *N, int *M); Display the student's details who achieved the highest total score across all the courses. You do not have to consider if a student with same name is available in multiple courses. ● Also write the same details into a file out.txt Also note that, in the in.txt file, there will be a blank line after each course. However, fscanf will automatically take care of it. ● After writing the result, you should call the release_memory function to free up all the memory.
The function prototype is: void release_memroy(student **st, int C, int n, int M) • You should also use the memory leak detector code as instructed in the webcourses. Sample Input: All the inputs, including N and M will come from an input file called input.txt The input file is structured as follows: Sample in.txt 342 ///CNM adel 10 12 9 45 // last name, assignment score, scores for M number of quizzes, and final exam score smile 689 39 mahmud 10 12 10 15 jose 8 11 7 41 adam 10 12 9 45 //second course smith 6 8 9 39 muhammad 10 12 10 45 jones 8 11 7 41 adil 10 12 9 45 //third course samuel 6 8 9 39 miguel 5 12 10 40 jerry 8 11 7 41
Sample out.txt as well as console output: Name: muhammad Assignment: 10 Quizzes: 12 10 Final exam: 45 Total: 77 Course number: 2 The main function of your code should be the following: int main() { atexit (report_mem_leak); //for memory leak detector. student **courseList; FILE *inFile, *outFile; int i, j, C, N, M; inFile = fopen("in.txt", "r"); if(inFile!=NULL) { // printf("Reading data from input.txt...\n"); //passing reference of C, N, and M so that we get to know what we have in the file courseList = readCourses (inFile, &C, &N, &M); out File = fopen("out.txt", "w"); printHighest Total (out File, courseList, C, N, M); release_memroy (courseList, C, N, M); fclose (inFile); fclose (outFile); } else { printf ("Please provide correct input file"); exit (-1); return 0; }