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

For this project, you are writing a program to read a file of records, extract s

ID: 3774019 • Letter: F

Question

For this project, you are writing a program to read a file of records, extract some fields into a VLA’s, sort them using insertion sort, search for a user-specified target using binary search, and print out the result.

The program should open the input file “grades.csv”, a comma separate value, where each line contains 4 integers and 1 character separated by commas. These objects represent a student’s ID, score of Exam 1, score of Exam 2, score of Exam 3, and a grade letter based on the average of the three scores. A student ID can be any 6-digit integral value, i.e., it is in range from 100000 to 999999, both inclusive. The scores are from 0 to 100, and a grade letter is either ’A’, ’B’, ’C’, ’D’, or ’F’. We can assume the input file is non-empty, but, again, we do NOT know the number of lines in grades.csv

The program should read student ID’s and grade letters into single dimensional VLA’s (variable-length arrays). You are required to use fgets and sscanf functions to read a line as a C string from file and format the line to construct the VLA’s. The length is variable because we do not know its length until the program is running. The program should print the number of records and the records in the input file following the format in the Example Output section. The program should then sort the student ID’s in ascending order using insertion sort. During this sorting process, make sure the grade letters are synchronized with the student ID’s The program should print the sorted records, each containing an index number, a student ID, and his/her grade letter The program should repetitively prompt the user to enter a student ID which the program will search for in the sorted records. If the user enters “-1” (negative one), the program stops and quits; if an invalid student ID, the program prompts again for user input; if a valid student ID, the program launches the binary search module and prints out the result in the format shown also in the Example Output section.

Other requirements:

No global variables, and at least 3 functions should be declared, defined, and called (from main).

Our professor has just introduced us to arrays and strings, with not nearly enough examples for us to even get remotely close to the right source code.

I have the file "grades.csv" as well. If it is possible to write the code out where all I'd have to do is put in "grades.csv" where necessary (say like, opening a file), then that could work too.

If you could upload the code (under the requirements and conditions listed above), not only will I appreciate it but I'll actually be able to use it as study material for an upcoming exam.

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

Explanation / Answer

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

//records
struct student{
   int index, id;
   int e1,e2,e3;
   char grade;
};

//original data before sorting
void displayOriginalData(struct student *db, int size){
   int i;
   printf("Original: ");
   printf("STUDENT|EXAM 1|EXAM 2|EXAM 3|GRADE ");
   for(i=0;i<size;i++){
       printf(" %d| %d| %d| %d| %c ", db[i].id, db[i].e1, db[i].e2, db[i].e3, db[i].grade);
   }
}

//display sorted data of all
void displayData(struct student *db, int size){
   int i;
   printf("INDEX|STUDENT|GRADE ");
   for(i=0;i<size;i++){
       printf(" %d| %d| %c ", i+1, db[i].id, db[i].grade);
   }
}

//individual data records
void displayDataOf(struct student *db, int size, int id){
   int i;
   int first = 0;
int last = size - 1;
int middle = (first+last)/2;
  
   printf(" INDEX|STUDENT|GRADE ");  
   //binary search
   while (first <= last) {
       if (db[middle].id < id)
           first = middle + 1;
       else if (db[middle].id == id)
       {
           printf(" %d| %d| %c ", middle+1, db[middle].id, db[middle].grade);
           break;
       }
       else
           last = middle - 1;
  
       middle = (first + last)/2;
   }
   if (first > last)
       printf(" N/A| N/A| N/A ");
}

void sortData(struct student *db, int size){
   //insertion sort
   int i, j;
   struct student key;
   for (i = 1; i < size; i++)
   {
   key = db[i];
   j = i-1;  
     
   while (j >= 0 && db[j].id > key.id)
   {
   db[j+1] = db[j];
   j = j-1;
   }
   db[j+1] = key;
   }

}

int main(){
  
   FILE *fp;
   char str[100];
   struct student *db;
   int lines, i = 0, ch;
  
   //opening file
   fp = fopen("grades.csv","r");
   if(fp == NULL)
   {
       printf("Error opening file");
       return   -1;
   }
  
   //count number of lines
   while(!feof(fp))
   {
       ch = fgetc(fp);
       if(ch == ' ')
           lines++;      
   }  
  
   //reset to staring point;
   fseek( fp, 0, SEEK_SET );
  
   db = (struct student*) malloc(sizeof(struct student) * (lines+1));
  
   //read data   & store
   while(fgets (str, 100, fp))
   {  
       struct student d;
       sscanf(str, "%d,%d,%d,%d,%c", &d.id, &d.e1, &d.e2, &d.e3, &d.grade);
       db[i] = d;
       i++;
   }

   displayOriginalData(db, i);
  
   sortData(db, i);  
   printf(" Sorted: ");
   displayData(db, i);
  
   //query user for db
   ch = 0;
   while(ch != -1){
       printf(" Enter student id(-1 to quit): ");
       scanf("%d", &ch);
      
       if(ch != -1){
          
           if(ch < 100000 || ch > 999999)
           {   //invalid query
               printf("ERROR and Enter again([100000, 999999])");
           }
           else
               displayDataOf(db, i, ch);
       }
   }
  
   //freeup memory
   fclose(fp);
   free(db);
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote