SHARE
TWEET

Untitled

a guest Nov 19th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "string.h"
  2. #include "stdlib.h"
  3. #include <stdio.h>
  4.  
  5. struct date
  6. {
  7.     int day, month, year;
  8. };
  9.  
  10. struct student
  11. {
  12.     char firstName[50];
  13.     char surname[50];
  14.     int ID;
  15.     struct date DOB;
  16. };
  17.  
  18.  
  19. int addStudent(struct student students[], int numStudents);
  20. void printStudents(struct student students[], int numStudents);
  21. void sortByID(struct student students[], int numStudents);
  22. void sortBySurname(struct student students[], int numStudents);
  23.  
  24. int main()
  25. {
  26.  
  27.     struct student students[100] = { {"Rolando","Gamble", 17560001, {1,1,2000}}, {"Kyra","Eaton", 17973061, {11,10,2016}}, {"Chandler","Humphrey", 17427400, {6,12,1999}}, {"Makayla","Huang", 17127058, {2,2,2000}}, {"Jazmyn","Terrell", 17746780, {29,6,2000}}, {"Erica","Glass", 17058753, {13,8,2000}}, {"Alyvia","Blair", 17599020, {11,9,2000}}, {"Talia","Chen", 17619205, {17,10,2000}}, {"Rebekah","Hopkins", 17912255, {25,11,2000}}, {"Jocelynn","Ware", 17899236, {27,4,1999}} };
  28.  
  29.     int numStudents = 10;
  30.  
  31.     // use addStudent to add a new student and it returns the updated number of students
  32.     // obviously, numStudents is the number of structures in the array (students[]) that contain data
  33.     numStudents = addStudent(students, numStudents);
  34.  
  35.     // sort by ID number and print out list
  36.     sortByID(students, numStudents);
  37.     printf("\n\n%d Students, Sorted by ID: \n", numStudents);
  38.     printStudents(students, numStudents);
  39.  
  40.     // sort by surname and print out list
  41.     sortBySurname(students, numStudents);
  42.     printf("\n\n%d Students, Sorted by Surname: \n", numStudents);
  43.     printStudents(students, numStudents);
  44.  
  45.     return 0;
  46. }
  47.  
  48. int addStudent(struct student students[], int numStudents)
  49. {
  50.     char first[50], last[50];
  51.     int id, dd, mm, yyyy;
  52.  
  53.     // Ask user for data for new student and add to the array of students
  54.     // your code here
  55.    
  56.  
  57.     return numStudents;
  58. }
  59.  
  60. void printStudents(struct student students[], int numStudents)
  61. {
  62.     int i;
  63.  
  64.     printf("%20s%20s%15s\t\t%10s\n", "First Name", "Surname", "ID Number", "DOB");
  65.  
  66.     // write a for loop to print out each student structure
  67.     // your code here
  68.  
  69.     for (i = 0; i < numStudents; i++) {
  70.         printf("%20s%20s%15i\t\t%10i-%i-%i\n", students[i].firstName, students[i].surname, students[i].ID, students[i].DOB.day, students[i].DOB.month, students[i].DOB.year);
  71.     }
  72. }
  73.  
  74. void sortByID(struct student students[], int numStudents)
  75. {
  76.     struct student temp;
  77.     int i, j, n;
  78.     for (i = 0; i < numStudents - 1; i++) {
  79.         for (j = 0; j < numStudents - i - 1; j++) {
  80.            
  81.             if (students[j].ID > students[j + 1].ID) {
  82.                 temp = students[j];
  83.                 students[j] = students[j + 1];
  84.                 students[j + 1] = temp;
  85.             }
  86.         }
  87.     }
  88. }
  89.  
  90. void sortBySurname(struct student students[], int numStudents)
  91. {
  92.     struct student temp;
  93.     int i, j, n;
  94.  
  95.     // use the Bubble sort algorithm to sort the students by Surname
  96.     // your code here
  97.  
  98.     for (i = 0; i < numStudents - 1; i++) {
  99.         for (j = 0; j < numStudents - i - 1; j++) {
  100.             if (students[j].surname[0] > students[j+1].surname[0]) {
  101.                 temp = students[j];
  102.                 students[j] = students[j + 1];
  103.                 students[j + 1] = temp;
  104.             }
  105.         }
  106.     }
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top