0% found this document useful (0 votes)
24 views

Sorting

This C program reads in student records with names, grades, and averages from a text file. It then sorts the records alphabetically by last name and outputs the sorted list. Specifically, it: 1) Defines a structure to store student records with names and grades. 2) Opens a file, reads in records for 3 students, and writes them to a text file. 3) Reopens the file, reads the records back in, calculates averages, and sorts the records alphabetically by last name. 4) Prints the sorted list of student names and averages and writes it back to the text file.

Uploaded by

Rey Supan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Sorting

This C program reads in student records with names, grades, and averages from a text file. It then sorts the records alphabetically by last name and outputs the sorted list. Specifically, it: 1) Defines a structure to store student records with names and grades. 2) Opens a file, reads in records for 3 students, and writes them to a text file. 3) Reopens the file, reads the records back in, calculates averages, and sorts the records alphabetically by last name. 4) Prints the sorted list of student names and averages and writes it back to the text file.

Uploaded by

Rey Supan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

h> struct record{ char name[15]; char lname[15]; char mgrade[15]; char fgrade[15]; float midgrade; float fingrade; float avgrade; }; void main() { clrscr(); FILE *fp; int i, j; struct record st[3]; if((fp=fopen("REY.txt", "w"))==NULL) puts("Error"); else{ for(i=0;i<3;i++){ puts("Your First Name: "); scanf("\n"); gets(st[i].name); puts("Your Last Name: "); scanf("\n"); gets(st[i].lname); puts("Enter Your Mid Term Grade: "); scanf("\n%s", st[i].mgrade); puts("Enter Your Final Grade: "); scanf("\n%s", st[i].fgrade); fprintf(fp, "%s %s %s %s\n", st[i].name, st[i].lname, st[i].mgrade, st[i].fgrade); } } fclose(fp); if((fp=fopen("REY.txt", "w+"))==NULL) printf("Error"); else{ for(i=0;i<3;i++){ fscanf(fp, "%s %s %s %s\n", st[i].name, st[i].lname, st[i].mgrade, st[i].fgrade); st[i].midgrade=atof(st[i].mgrade); st[i].fingrade=atof(st[i].fgrade); st[i].avgrade=(st[i].midgrade+st[i].fingrade)/2; }

char temp[15]; float temp2; for(i=0;i<3;i++){ for(j=i+1;j<3;j++){ if(strcmp(st[i].lname, st[j].lname) > 0){ strcpy(temp, st[i].lname); strcpy(st[i]. lname, st[j].lname); strcpy(st[j]. lname, temp); strcpy(temp, st[i].name); strcpy(st[i].name, st[j].name); strcpy(st[j].name, temp); strcpy(temp, st[i].mgrade); strcpy(st[i].mgrade, st[j].mgrade); strcpy(st[j].mgrade, temp); strcpy(temp, st[i].fgrade); strcpy(st[i].fgrade, st[j].fgrade); strcpy(st[j].fgrade, temp); temp2=st[i].avgrade; st[i].avgrade=st[j].avgrade; st[j].avgrade=temp2; } } } puts("In Alphabetical Order."); for(i=0;i<3;i++){ printf("\nName: %s, %s Grade: %.2f", st[i].lname, st[i].name, st[i].avgrade); } for(i=0;i<3;i++){ fprintf(fp, "%s %s %s %s %.2f\n", st[i].name, st[i].lname, st[i].mgrade, st[i].fgrade, st[i].avgrade); } } fclose(fp); getch(); }

You might also like