Dados los registros de los estudiantes con cada registro que contiene la identificación, el nombre y la edad de un estudiante. Escriba un programa en C para leer estos registros y mostrarlos ordenados por edad o id.
Clasificación por edad
Ejemplos:
Input: Student Records = { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {Id = 5, Name = az, Age = 10 } } Output: {{Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {Id = 2, Name = ba, Age = 10 }, {Id = 5, Name = az, Age = 10 }, {Id = 1, Name = bd, Age = 12 } }
Planteamiento: Este problema se resuelve en los siguientes pasos:
- Cree una estructura con los campos id, nombre y edad.
- Leer los registros de los estudiantes en la estructura.
- Defina un comparador configurando reglas para la comparación. Aquí la edad se puede ordenar con la ayuda de la diferencia de la edad de 2 estudiantes. (Estudiante1 -> edad – Alumno2 -> edad)
- Ahora ordene la estructura según el comparador definido con la ayuda del método qsort() .
- Imprime los expedientes de los alumnos ordenados.
Programa:
// C program to read Student records // like id, name and age, // and display them in sorted order by Age #include <stdio.h> #include <stdlib.h> #include <string.h> // struct person with 3 fields struct Student { char* name; int id; char age; }; // setting up rules for comparison // to sort the students based on age int comparator(const void* p, const void* q) { return (((struct Student*)p)->age - ((struct Student*)q)->age); } // Driver program int main() { int i = 0, n = 5; struct Student arr[n]; // Get the students data arr[0].id = 1; arr[0].name = "bd"; arr[0].age = 12; arr[1].id = 2; arr[1].name = "ba"; arr[1].age = 10; arr[2].id = 3; arr[2].name = "bc"; arr[2].age = 8; arr[3].id = 4; arr[3].name = "aaz"; arr[3].age = 9; arr[4].id = 5; arr[4].name = "az"; arr[4].age = 10; // Print the Unsorted Structure printf("Unsorted Student Records:\n"); for (i = 0; i < n; i++) { printf("Id = %d, Name = %s, Age = %d \n", arr[i].id, arr[i].name, arr[i].age); } // Sort the structure // based on the specified comparator qsort(arr, n, sizeof(struct Student), comparator); // Print the Sorted Structure printf("\n\nStudent Records sorted by Age:\n"); for (i = 0; i < n; i++) { printf("Id = %d, Name = %s, Age = %d \n", arr[i].id, arr[i].name, arr[i].age); } return 0; }
Producción:
Unsorted Student Records: Id = 1, Name = bd, Age = 12 Id = 2, Name = ba, Age = 10 Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 5, Name = az, Age = 10 Student Records sorted by Age: Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 2, Name = ba, Age = 10 Id = 5, Name = az, Age = 10 Id = 1, Name = bd, Age = 12
Clasificación por identificación
Ejemplos:
Input: Student Records = { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {Id = 5, Name = az, Age = 10 } } Output: {{Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name = bc, Age = 8 }, {Id = 4, Name = aaz, Age = 9 }, {Id = 5, Name = az, Age = 10 } }
Planteamiento: Este problema se resuelve en los siguientes pasos:
- Cree una estructura con los campos id, nombre y edad.
- Leer los registros de los estudiantes en la estructura.
- Defina un comparador configurando reglas para la comparación. Aquí la identificación se puede ordenar con la ayuda de la diferencia de la identificación de 2 estudiantes. (Estudiante1 -> id – Estudiante2 -> id)
- Ahora ordene la estructura según el comparador definido con la ayuda del método qsort() .
- Imprime los expedientes de los alumnos ordenados.
Programa:
// C program to read Student records // like id, name and age, // and display them in sorted order by ID #include <stdio.h> #include <stdlib.h> #include <string.h> // struct person with 3 fields struct Student { char* name; int id; char age; }; // setting up rules for comparison // to sort the students based on ID int comparator(const void* p, const void* q) { return (((struct Student*)p)->id - ((struct Student*)q)->id); } // Driver program int main() { int i = 0, n = 5; struct Student arr[n]; // Get the students data arr[0].id = 1; arr[0].name = "bd"; arr[0].age = 12; arr[1].id = 2; arr[1].name = "ba"; arr[1].age = 10; arr[2].id = 3; arr[2].name = "bc"; arr[2].age = 8; arr[3].id = 4; arr[3].name = "aaz"; arr[3].age = 9; arr[4].id = 5; arr[4].name = "az"; arr[4].age = 10; // Print the Unsorted Structure printf("Unsorted Student Records:\n"); for (i = 0; i < n; i++) { printf("Id = %d, Name = %s, Age = %d \n", arr[i].id, arr[i].name, arr[i].age); } // Sort the structure // based on the specified comparator qsort(arr, n, sizeof(struct Student), comparator); // Print the Sorted Structure printf("\n\nStudent Records sorted by ID:\n"); for (i = 0; i < n; i++) { printf("Id = %d, Name = %s, Age = %d \n", arr[i].id, arr[i].name, arr[i].age); } return 0; }
Producción:
Unsorted Student Records: Id = 1, Name = bd, Age = 12 Id = 2, Name = ba, Age = 10 Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 5, Name = az, Age = 10 Student Records sorted by ID: Id = 1, Name = bd, Age = 12 Id = 2, Name = ba, Age = 10 Id = 3, Name = bc, Age = 8 Id = 4, Name = aaz, Age = 9 Id = 5, Name = az, Age = 10