0% found this document useful (0 votes)
16 views1 page

Below You Can Find The Reqiured Implementation With Detailed Comments

Howard is a teacher who had student lists in a random order. He wants the lists sorted by student ID number. The C program reads student data from a file with IDs and names. It uses a bubble sort algorithm to sort the student structures by ID. The sorted list is then printed out with IDs and names.

Uploaded by

priscilla helen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Below You Can Find The Reqiured Implementation With Detailed Comments

Howard is a teacher who had student lists in a random order. He wants the lists sorted by student ID number. The C program reads student data from a file with IDs and names. It uses a bubble sort algorithm to sort the student structures by ID. The sorted list is then printed out with IDs and names.

Uploaded by

priscilla helen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question:

Solve this in C programming language

Student Sorting

Howard is a teacher. He has many students. One day, Howard combined all his student lists into a document randomly. Howard wants to make the documents ordered by student identification number. He asks for your help.

Format Input

Read from file “testdata.in”. First line, consists of one integer N, number of students. Next N lines consists of two field per line which are student identification number R and students’ name M. The fields are separated by space.

Format Output

Output should be expressed in format “R M” - sorted by R in ascending order.

Constraints

• 1 ≤ N ≤ 1000
• |R| = 10 (|R| means length of string R) • 1≤|M|≤20
• R only consists of digits 0-9.
• M only consists of letters a-z and A-Z.

Sample Input (testdata.in)


10
2020123464 Oliver
2020123461 Benjamin
2020123458 William
2020123460 Logan
2020123457 Noah
2020123462 Mason
2020123456 Liam
2020123459 James
2020123463 Elijah
2020123465 Jacob

Sample Output (standard output)


2020123456 Liam
2020123457 Noah
2020123458 William
2020123459 James
2020123460 Logan
2020123461 Benjamin
2020123462 Mason
2020123463 Elijah
2020123464 Oliver
2020123465 Jacob

Expert answer:

Answer 1:

Below you can find the reqiured implementation with detailed comments:

Code snippet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 256

//declare structure for student


typedef struct {
char id[100];
char name[100];
} Student;

//method to read student data from file


int read_students(const char *filename, Student *students) {

//open the file pointer


FILE *fin = fopen(filename, "r");

int i = 0;

//read the size of students list first


int n;
fscanf(fin,"%d",&n);

//then read line by line all students data


while (i<n)
{
fscanf(fin, "%s %s", students[i].id,students[i].name);
++i;
}

//close the file pointer


fclose(fin);
return i;
}

//method to sort and print students based on id


void print_students(Student *students, int n) {

Student temp;
int i,j;

//use bubble sort to sort the students array by id


for (i = 1; i < n; i++)
for (j = 0; j < n - i; j++) {
//compare the id of two students
if (strcmp(students[j].id, students[j + 1].id) > 0) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}

//print all the students


for (int k = 0; k < n; ++k) {
Student *m = &students[k];
printf("%s %s\n",m->id,m->name);
}
}

//driver method
int main() {
//declare the array of struct
Student students[MAX_SIZE];
//read the file
int size = read_students("testdata.in", students);
//sort and print students
print_students(students, size);
return 0;
}

Code screenshot:

Results screeenshot:

You might also like