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

write a C function that reads an input file and creates a sorted output file. th

ID: 3881785 • Letter: W

Question

write a C function that reads an input file and creates a sorted output file. the function should be void file_sort(char *infile, char *outfile); the file names are the parameters. the input file contains student info in the following format. the first line contains the number of students, each line after that conatins info about one student being id grade and gpa in that order. the id is an int. the grade is a char. the gpa is a double. the function sorts the data in ascending order based on id and writes them to the output file with the same format. the student info should be stored in dynamically allocated arrays that are sorted simultaneously then written to the output file. the arrays should be freed when finished. no structures should be used.

Explanation / Answer

Given below is the code for the question. Make sure you give input fie in the format specified in the question. Scroll down to see a sample file and the output.
Please do rate the answer if it was helpful. Thank you


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void file_sort(char *infile, char *outfile);


int main()
{
char infile[20], outfile[20];
  
printf("Enter input filename: ");
scanf("%s", infile);
  
printf("Enter output filename: ");
scanf("%s", outfile);
  
file_sort(infile, outfile);
return 0;
}

void file_sort(char *infile, char *outfile)
{
FILE *in = fopen(infile, "r");
FILE *out = fopen(outfile, "w");
int *id;
char *grade;
double *gpa;
int n, i, j, minIdx;
  
int tempId;
char tempGrade;
double tempGpa;
  
if(in == NULL)
{
printf("ERROR: Could not open input file %s ", infile);
exit(1);
}
if(out == NULL)
{
printf("ERROR: Could not open output file %s ", outfile);
exit(1);

}
  
fscanf(in, "%d", &n);
id = (int*) malloc(sizeof(int) * n);
grade = (char *) malloc(sizeof(char) * n);
gpa = (double *) malloc(sizeof(double) * n);
  
  
for(i = 0; i < n; i++)
{
fscanf(in, "%d %c %lf", &id[i], &grade[i] , &gpa[i]);
}
fclose(in);
  
printf("Read %d records from file %s ", n, infile);
printf("Sorting records in ascending order of Id ");
  
//sort on id
for(i = 0; i < n; i++)
{
minIdx = i;
for(j = i + 1; j < n; j++)
{
if(id[j] < id[minIdx])
minIdx = j;
}
  
if(i != minIdx)
{
//swap the values
tempId = id[i];
id[i] = id[minIdx];
id[minIdx] = tempId;
  
  
tempGrade = grade[i];
grade[i] = grade[minIdx];
grade[minIdx] = tempGrade;
  
tempGpa = gpa[i];
gpa[i] = gpa[minIdx];
gpa[minIdx] = tempGpa;
}
}
  
  
//now write to output file
printf("Writing sorted records to file %s ", outfile);
fprintf(out,"%d ",n);
for(i = 0; i < n; i++)
fprintf(out, "%5d %5c %7.2lf ", id[i], grade[i], gpa[i]);
  
printf("Please check out file %s for sorted records ", outfile);
fclose(out);
  
free(id);
free(gpa);
free(grade);
  
}


input file: stud1.txt
==================
4
4 A 91.0
1 B 83.0
3 C 73.0
2 A 95.0


output
========

Enter input filename: stud1.txt
Enter output filename: out1.txt
Read 4 records from file stud1.txt
Sorting records in ascending order of Id
Writing sorted records to file out1.txt
Please check out file out1.txt for sorted records

output file: out1.txt
======================
4
1 B 83.00
2 A 95.00
3 C 73.00
4 A 91.00