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

Wrote this program to get grades from a file and sort them into a chart with gra

ID: 3808920 • Letter: W

Question

Wrote this program to get grades from a file and sort them into a chart with grades for each student on each test and their overall grade. i am supposed to extract some fields into a VLA’s, sort them using insertion
sort, and be able to search for a user-specified target using binary search, then print the result to look like the example below

program compiles with no errors but when i execute it, it gives me nothing. im not quite sure if I have what is necessary to make everything look exactly like this but i think it should be fairly close. if you could figure out why it does not produce any results at all that would be awesome.

Heres the code:

5 Example output There are 5 lines in file grades csv. Original Student. I Exam 1 Exam 2 Exam 3 Grade I 535743 67 I 96 93 112213 87 I 65 72 612778 59 58 97 151774 I 52 l 100 l 86 l Cl 406704 54 72 I 80 Sorted: l Index l Student l Grade I 1 112213 CI 21 151774 CI 3 406704 DI 4 5357 43 BI 5 612778 CI Enter student ID (-1 to quit) 1234 ERROR and enter again ([100 000, 999999 and -1 to quit) 65 4321 l Index l Student l Grade I N/A I N/A I N /A Enter student ID (-1 to quit) 5357 43 l Index l Student l Grade 4 5357 43 BI Enter student ID (-1 to quit) -1

Explanation / Answer

Seems like code you had written so far was not working as you were trying to assign a float value to your student struct record. And as file is csv it is not straightword to read such a file in c.

I have added code to read in file and store values in grades (STUDENT struct). There was no isertion logic for me to improve upon.

Here is content of grades.csv file

535743;67;96;93;B
112213;87;65;72;C
61278;59;58;97;C
151774;52;100;86;C
406704;54;72;80;D

Please note you have to enter grades.csv (filename) when running program as it is asking user to enter a filename to be read.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TNARRAY_MAX 4096
#define LINE_MAX 200
#define FILENAME_MAX 4096
#define ID 6
#define SCORE_MAX 4

typedef struct
{
char id[ID+1];
int score[SCORE_MAX];
char Grade;
} STUDENT;

int checkGrades(STUDENT grades[], int maxrecs, FILE* fp);

char* getfield(char* line, int num);

int main()
{
char *file_name[30];
FILE* fp;
int rc;
int i;
int count = 0;
STUDENT grades[TNARRAY_MAX];

scanf("%s", file_name);
fp = fopen("grades.csv", "r");
  
if(fp == NULL)
{
printf("Error opening file for read. ");
exit(1);
}

i = 0;
char line[1024];
while (fgets(line, 1024, fp))
{
char* tmp = strdup(line);
int num = 1;
char* tok;
for (tok = strtok(tmp, ";");
tok && *tok;
tok = strtok(NULL, "; "))
{
if (num == 1)
strcpy(grades[i].id, tok);
if (num == 2)
sscanf(tok, "%d", &grades[i].score[0]);
if (num == 3)
sscanf(tok, "%d", &grades[i].score[1]);
if (num == 4)
sscanf(tok, "%d", &grades[i].score[2]);
if (num == 5)
grades[i].Grade = tok[0];
num++;
}
// NOTE strtok clobbers tmp
free(tmp);
i++;
}
  
fclose(fp);
int n = i;
for(i = 0; i < n; i++)
{
printf("Student: %s %d %d %d %c", grades[i].id, grades[i].score[0], grades[i].score[1], grades[i].score[2], grades[i].Grade);
printf(" ");
}

//count = checkGrades(grades, TNARRAY_MAX, fp);
return 0;
}

int checkGrades(STUDENT grades[], int maxrecs, FILE* fp)
{
char line[LINE_MAX];
int rc;
int count = 0;
int score;
while(fgets(line,LINE_MAX, fp) != NULL)
{
rc = sscanf(line, "%25[^:]%*c%25[^:]%*c%6s%*c%3d%*c%3d%*c%3d%*c%3d%*c%3d%*c%2c",
grades[count].id,
&grades[count].score[1],
&grades[count].score[2],
&grades[count].score[3],
&grades[count].Grade);
if((rc != 5) ||

atoi(grades[count].id) < 100000 ||
atoi(grades[count].id) > 999999 ||
grades[count].Grade < 0 ||
grades[count].Grade > 110)
{


printf("Invalid Record: %s", line);
}
else
{
count++;
if(count == TNARRAY_MAX)break;
}
printf("ID: ", grades-> id);
}

return 0;
}