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

Write a program that reads a comma-delimited file of student information, and st

ID: 3601231 • Letter: W

Question


Write a program that reads a comma-delimited file of student information, and stores them in an array of structures. Then provide following functionalities for the user: 1- Print records. 2- Sort by Last name. 3- Search by Id. 4- Exit. The student data is in student.txt file. Your program should read the file and store Id, First Name, Last Name and score in an array of structure. o Id: integer o First Name: array of char of size 50. o Last Name: array of char of size 50. o Score: double. In student.txt: o Token are separated by comma. o The first line contains the header of columns. o The file contains the information of 200 students. o The maximum character at each line is 150. o There are some irrelevant columns, that your program should ignore them. When user enters 1 the program should print all records in the “output.txt” file. The records include id, first name, last name and score. When user enters 2 the program should sort all records based on students’ last name. When user enters 3 the program asks user to input the id, e.g. sid, and then prints the first name and last name of the of the student whose id is sid. Program keeps asking user to enter the command, unless the user enters 4. Note that the program may produce different “output.txt” file. If user sort records before printing, the file contains sorted records. If not, records will be printed in the same order as the “student.txt” file. The file is in the link above, if you can't get to the link above here are the screenshots of the file. Write the code in C. Write a program that reads a comma-delimited file of student information, and stores them in an array of structures. Then provide following functionalities for the user: 1- Print records. 2- Sort by Last name. 3- Search by Id. 4- Exit. The student data is in student.txt file. Your program should read the file and store Id, First Name, Last Name and score in an array of structure. o Id: integer o First Name: array of char of size 50. o Last Name: array of char of size 50. o Score: double. In student.txt: o Token are separated by comma. o The first line contains the header of columns. o The file contains the information of 200 students. o The maximum character at each line is 150. o There are some irrelevant columns, that your program should ignore them. When user enters 1 the program should print all records in the “output.txt” file. The records include id, first name, last name and score. When user enters 2 the program should sort all records based on students’ last name. When user enters 3 the program asks user to input the id, e.g. sid, and then prints the first name and last name of the of the student whose id is sid. Program keeps asking user to enter the command, unless the user enters 4. Note that the program may produce different “output.txt” file. If user sort records before printing, the file contains sorted records. If not, records will be printed in the same order as the “student.txt” file. The file is in the link above, if you can't get to the link above here are the screenshots of the file. Write the code in C.
Sample output: (user input are bold) o Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by Id (press 3) Exit (press 4) 1 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 2 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 1 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 3 Please enter Id: 10100 10100,Golda,Schlager Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 4 Sample output: (user input are bold) o Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by Id (press 3) Exit (press 4) 1 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 2 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 1 Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 3 Please enter Id: 10100 10100,Golda,Schlager Please enter the command: Print records (press 1) Sort by Last name (press 2) Search by score (press 3) Exit (press 4) 4 Id,Pirst Nane, Last Name, Email,Year of Birth, City,State,Score 10001,Delana, Touchard, Delana.Touchard10001 labc.com, 2002,Napa, CA, 41.26 10002,Inell, Genera, Inell.General00028abc.com, 1995,Mankato,MN,77.18 10003,Dorla, Komatz,Dorla.Komatzl0003 abc.com,1996,Collier County, FL, 95.94 10004,Reta,Menedez, Reta. Menedez100048abc.com,1992,Acton,MA,58.20 10005,Jinny, Pelchat, Jinny.Pelchat10005eabc.com, 1993, Fullerton, CA, 43.36 10006, Dorinda, Chon, Dorinda.Chon100068abc.com,1990,Cutler Bay, FL, 82.26 10007, Deidre, Arone, Deidre.Aronel0007 abc.com,2001,Richardson, TX, 63.71 10008,clarence, Panzella,Clarence. Panzella10008eabe.com, 1994,Edina,MN 41.64 10009, Laree,Melland, Laree.Helland10009abc.com, 2000,Tallahassee,FL 86.48 10010, Sherri,Diegidio, Sherri.Diegidio10010eabe.com, 1990,Santa Barbara County, CA,55.10 10011,Marci,Malcom, Marei.Malcom10011eabe.com,1997,Cookeville, TN, 95.12 10012,Santa, Lipper, Santa.Lipper10012abe.com,2002,Rock Island,IL 65.88 10013,Jeraldine,Fickas,Jeraldine.Fickas10013eabc.com, 1990, Lawrence,KS,91.99 10014,Shelley, Valiente,Shelley-Valiente10014eabe.com,1996,Homer, AK, 70.18 10015,Janar, Swyers,Jamar.Svyers10015abe.com, 1992,Rancho Cucamonga, CA, 70.46 10016, Kermit, Zalwsky,Kermit.Zalwsky10016eabc.com, 1992,Raleigh,NC, 54.39 10017,Linsey, Anecelle, Linsey.Anecelle10017eabc.com, 2001,Rolling Hills EstateS,CA,96.39 10018,Sandy, Rondinelli,Sandy Rondinel1i10018eabc.com, 1993,Pittsburg, CA, 63.95 10019,Cassandra, Paylor, Cassandra.Paylor10019eabe.com 1994, Decatur,GA,88.36 10020,Amie,Kuszynski,Amie.Kuszynski10020eabc.com, 1993,Dublin, CA, 41.27 10021,Sharmaine,Elmendorf, Sharmaine.Elmendorf10021eabe.com, 1996, Hennepin County,MN,46.83 10022, Dorine, Zimnerebner, Dorine.Zimmerebner10022eabc.com, 1999, Hingham, MA, 90.16 10023,Leora, Picerni, Leora.Picerni10023eabc.com, 2001,Lafayette,CA, 43.29 10024,Chet,Zaborac, Chet.Zaborac100248abc.com, 1991,Tampa, FL,56-47 10025,Jeannetta,Lagroon, Jeannetta.Lagroon10025abc.com,1991,Whatcom County,WA, 85.21 10026,Blair,Fitchpatrick,Blair.Fitchpatrick10026eabc.com 1999, Rosendale, NY,41.90 10027,Janey, Houltberg,Jamey. Houltberg10027eabe.com, 1990, Novato, CA, 91.88

Explanation / Answer

Given below is the code for the question. The print option writes to a file output.txt . You can check that file when program terminates.

Please don't forget to rate the answer if it helped. Thank you.

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

struct student
{
int id;
char firstname[50];
char lastname[50];
double score;
};

int readFile(char *filename, struct student s[]);
void print(struct student s[], int n);
void sort(struct student s[], int n);
int search(struct student s[], int n, int id);
int main()
{
char filename[25];
struct student students[200];
int n;
int choice = 0;
int id, index;
printf("Enter input filename: ");
scanf("%s", filename);
  
n = readFile(filename, students);
while(choice != 4)
{
printf(" 1. Print records ");
printf("2. Sort by Last name ");
printf("3. Search by ID ");
printf("4. Exit ");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
print(students, n);
break;
case 2:
sort(students, n);
printf("Students sorted on last name ");
break;
case 3:
printf("Enter the student id: ");
scanf("%d", &id);
index = search(students, n, id);
if(index == -1)
printf("Student with id %d not found. ", id);
else
printf("%d,%s,%s ", students[index].id, students[index].firstname, students[index].lastname);
break;
case 4:
break;
default:
printf("Invalid choice! ");
}
}
printf("Goodbye!! ");
}

int readFile(char *filename, struct student s[])
{
FILE* fp = fopen(filename, "r");
char line[151];
int count = 0;
char* token;
int i;
if(fp == NULL)
{
printf("Error opening input file %s ", filename);
return 0;
}
  
fgets(line, 150, fp); //read the column names
  
while(fgets(line, 150, fp))
{
token = strtok(line, ",");//get id
s[count].id = atoi(token);
token = strtok(NULL, ",");//get firstname
strcpy(s[count].firstname, token);
token = strtok(NULL, ",");//get lastname
strcpy(s[count].lastname, token);
for(i = 1; i <= 4; i++) //skip over 4 values - email, year, city, state
strtok(NULL, ",");
token = strtok(NULL," ");//get score
s[count].score = atof(token);
count++;
}
fclose(fp);
return count;
}
void print(struct student s[], int n)//writes to file output.txt
{
FILE* fp = fopen("output.txt", "w");
int i;
if(fp == NULL)
{
printf("Error opening output file output.txt ");
return;
}
fprintf(fp, "Id,First Name,Last Name,Score ");
for(i = 0; i < n; i++)
fprintf(fp, "%d,%s,%s,%.2f ",s[i].id, s[i].firstname, s[i].lastname, s[i].score );
fclose(fp);
printf("Student list printed to output.txt ");
}
void sort(struct student s[], int n)
{
int i ,j, minIdx;
struct student temp;
  
for ( i = 0; i < n; ++i )
{
minIdx = i;
for(j = i+1; j < n; j++)
{
if(strcmp(s[j].lastname, s[minIdx].lastname) < 0)
minIdx = j;
}
if(i != minIdx)
{
temp = s[i];
s[i] = s[minIdx];
s[minIdx] = temp;
}
}
}
int search(struct student s[], int n, int id)
{
int i;
for(i = 0; i < n; i++)
{
if(s[i].id == id)
return i;
}
return -1;
}

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

struct student
{
int id;
char firstname[50];
char lastname[50];
double score;
};

int readFile(char *filename, struct student s[]);
void print(struct student s[], int n);
void sort(struct student s[], int n);
int search(struct student s[], int n, int id);
int main()
{
char filename[25];
struct student students[200];
int n;
int choice = 0;
int id, index;
printf("Enter input filename: ");
scanf("%s", filename);
  
n = readFile(filename, students);
while(choice != 4)
{
printf(" 1. Print records ");
printf("2. Sort by Last name ");
printf("3. Search by ID ");
printf("4. Exit ");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
print(students, n);
break;
case 2:
sort(students, n);
printf("Students sorted on last name ");
break;
case 3:
printf("Enter the student id: ");
scanf("%d", &id);
index = search(students, n, id);
if(index == -1)
printf("Student with id %d not found. ", id);
else
printf("%d,%s,%s ", students[index].id, students[index].firstname, students[index].lastname);
break;
case 4:
break;
default:
printf("Invalid choice! ");
}
}
printf("Goodbye!! ");
}

int readFile(char *filename, struct student s[])
{
FILE* fp = fopen(filename, "r");
char line[151];
int count = 0;
char* token;
int i;
if(fp == NULL)
{
printf("Error opening input file %s ", filename);
return 0;
}
  
fgets(line, 150, fp); //read the column names
  
while(fgets(line, 150, fp))
{
token = strtok(line, ",");//get id
s[count].id = atoi(token);
token = strtok(NULL, ",");//get firstname
strcpy(s[count].firstname, token);
token = strtok(NULL, ",");//get lastname
strcpy(s[count].lastname, token);
for(i = 1; i <= 4; i++) //skip over 4 values - email, year, city, state
strtok(NULL, ",");
token = strtok(NULL," ");//get score
s[count].score = atof(token);
count++;
}
fclose(fp);
return count;
}
void print(struct student s[], int n)//writes to file output.txt
{
FILE* fp = fopen("output.txt", "w");
int i;
if(fp == NULL)
{
printf("Error opening output file output.txt ");
return;
}
fprintf(fp, "Id,First Name,Last Name,Score ");
for(i = 0; i < n; i++)
fprintf(fp, "%d,%s,%s,%.2f ",s[i].id, s[i].firstname, s[i].lastname, s[i].score );
fclose(fp);
printf("Student list printed to output.txt ");
}
void sort(struct student s[], int n)
{
int i ,j, minIdx;
struct student temp;
  
for ( i = 0; i < n; ++i )
{
minIdx = i;
for(j = i+1; j < n; j++)
{
if(strcmp(s[j].lastname, s[minIdx].lastname) < 0)
minIdx = j;
}
if(i != minIdx)
{
temp = s[i];
s[i] = s[minIdx];
s[minIdx] = temp;
}
}
}
int search(struct student s[], int n, int id)
{
int i;
for(i = 0; i < n; i++)
{
if(s[i].id == id)
return i;
}
return -1;
}

output

Enter input filename: students1.txt

1. Print records
2. Sort by Last name
3. Search by ID
4. Exit
Enter your choice: 2
Students sorted on last name

1. Print records
2. Sort by Last name
3. Search by ID
4. Exit
Enter your choice: 1
Student list printed to output.txt

1. Print records
2. Sort by Last name
3. Search by ID
4. Exit
Enter your choice: 3
Enter the student id: 10100
10100,Golda,Schlager

1. Print records
2. Sort by Last name
3. Search by ID
4. Exit
Enter your choice: 3
Enter the student id: 5
Student with id 5 not found.

1. Print records
2. Sort by Last name
3. Search by ID
4. Exit
Enter your choice: 4
Goodbye!!