Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZE 30
  6.  
  7. typedef struct student{
  8. int id;
  9. int help;
  10. char name[SIZE];
  11. char *task[SIZE];
  12. }student;
  13.  
  14. void swap(student *xp, student *yp)
  15. {
  16. student temp = *xp;
  17. *xp = *yp;
  18. *yp = temp;
  19. }
  20.  
  21. void bubbleSort(student arr[], int n)
  22. {
  23. int i, j;
  24. for (i = 0; i < n - 1; i++)
  25.  
  26. // Last i elements are already in place
  27. for (j = 0; j < n - i - 1; j++)
  28. if (arr[j].help <= arr[j + 1].help)
  29. swap(&arr[j], &arr[j + 1]);
  30. }
  31.  
  32.  
  33. void swap2(int *xp, int *yp)
  34. {
  35. int temp = *xp;
  36. *xp = *yp;
  37. *yp = temp;
  38. }
  39.  
  40. void bubbleSort2(int arr[], int n)
  41. {
  42. int i, j;
  43. for (i = 0; i < n - 1; i++)
  44.  
  45. // Last i elements are already in place
  46. for (j = 0; j < n - i - 1; j++)
  47. if (arr[j] <= arr[j + 1])
  48. swap2(&arr[j], &arr[j + 1]);
  49. }
  50.  
  51.  
  52. int main(void)
  53. {
  54. int i;
  55. int noSessions;
  56. int noStudents;
  57.  
  58. printf("Enter number of consultation sesssions: ");
  59. scanf("%d", &noSessions);
  60.  
  61. //create an array of sessions.
  62. int sessionCount[noSessions];
  63.  
  64. printf("Enter number of students: ");
  65. scanf("%d", &noStudents);
  66.  
  67. student arr[SIZE];
  68.  
  69. for (i = 0; i < noStudents; i++) {
  70. int j = i + 1;
  71. printf("Enter enter student %d ID, name and consultation session.", j);
  72. scanf("%d" "%s" "%d", &arr[i].id, arr[i].name, &arr[i].help);
  73. //increment the number of people in that particular session.
  74. sessionCount[arr[i].help]++;
  75. }
  76.  
  77. printf("Student ID == Full Name == Consultation Session");
  78.  
  79. bubbleSort(arr, noStudents);
  80.  
  81. for (i = 0; i < noStudents; i++) {
  82. printf("\n");
  83. printf(" %d <> %s <> %d", arr[i].id, arr[i].name, arr[i].help);
  84. printf("\n");
  85. }
  86.  
  87. //print the number of people in each session
  88. for (i = 0; i < noSessions; i++) {
  89. printf("\n");
  90. printf("%d, %d", i, sessionCount[i]);
  91. printf("\n");
  92. }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement