Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. //Declaring the functions.
  5. void printrecords(char **firstname, char **lastname, float *score, int *numofrecords);
  6. void addnewrecord(char **firstname, char **lastname, float *score, int *numofrecords);
  7. void deleterecords(char **firstname, char **lastname, float *score, int *numofrecords);
  8. void searchbylastname(char **firstname, char **lastname, float *score, int *numofrecords);
  9. void sortbyscore(char **firstname, char **lastname, float *score, int *numofrecords);
  10. void sortbylastname(char **firstname, char **lastname, float *score, int *numofrecords);
  11. void findmedianscore(char **firstname, char **lastname, float *score, int *numofrecords);
  12. int main(){
  13. //Defining empty arrays for records to be stored.
  14. char **firstname;
  15. char **lastname;
  16. float *score;
  17. //Taking input of the number of records.
  18. int numofrecords = 0;
  19. while(numofrecords<5){
  20. printf("Please indicate number of records you want to enter (min 5): ");
  21. scanf("%d", &numofrecords);
  22. }
  23. //Dynamic allocation ( making pointers ) for score, firstname, lastname
  24. score=(float*)malloc(numofrecords*sizeof(float*));
  25. firstname=(char**)malloc(numofrecords*sizeof(char**));
  26. lastname=(char**)malloc(numofrecords*sizeof(char**));
  27. for(int x=0; x<numofrecords; x++)
  28. firstname[x]=(char*)malloc(21*sizeof(char*));
  29. for(int x=0; x<numofrecords; x++)
  30. lastname[x]=(char*)malloc(21*sizeof(char*));
  31. //Print statements for input of records.
  32. printf("Please input records of students \n(enter a new line after each record), with following format;\n");
  33. printf("first_name last_name score \n");
  34. //Taking input of the of records.
  35. for(int x=0; x<numofrecords; x++){
  36. scanf("%s %s %f", firstname[x], lastname[x], &score[x]);
  37. }
  38. //Providing the options and taking input of the users option.
  39. int opt;
  40. int x=0;
  41. while (x==0){
  42. printf("\n");
  43. printf("Print records (press 1)\n");
  44. printf("Add a new record (press 2)\n");
  45. printf("Delete record(s) (press 3)\n");
  46. printf("Search by last name (press 4)\n");
  47. printf("Sort by score (press 5)\n");
  48. printf("Sort by last name (press 6)\n");
  49. printf("Find median score (press 7)\n");
  50. printf("Exit the program (press 0)\n");
  51. scanf("%d", &opt);
  52. printf("\n");
  53. //Calling the respective function that the user chooses.
  54. if(opt==0){
  55. x++;
  56. }
  57. if(opt==1){
  58. printrecords(firstname, lastname, score, &numofrecords);
  59. }
  60. if(opt==2){
  61. addnewrecord(firstname, lastname, score, &numofrecords);
  62. }
  63. if(opt==3){
  64. deleterecords(firstname, lastname, score, &numofrecords);
  65. }
  66. if(opt==4){
  67. searchbylastname(firstname, lastname, score, &numofrecords);
  68. }
  69. if(opt==5){
  70. sortbyscore(firstname, lastname, score, &numofrecords);
  71. }
  72. if(opt==6){
  73. sortbylastname(firstname, lastname, score, &numofrecords);
  74. }
  75. if(opt==7){
  76. findmedianscore(firstname, lastname, score, &numofrecords);
  77. }
  78. }
  79. return 0;
  80. }
  81. //Defining the functions
  82. void printrecords(char **firstname, char **lastname, float *score, int *numofrecords){
  83. for(int i=0; i<*numofrecords; i++){
  84. printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
  85. }
  86. }
  87. void addnewrecord(char **firstname, char **lastname, float *score, int *numofrecords){
  88. *numofrecords+=1;
  89. firstname=(char**)realloc(firstname,(*numofrecords)*sizeof(char**));
  90. firstname[*numofrecords-1]=(char*)calloc(20,sizeof(char*));
  91. lastname=(char**)realloc(lastname,(*numofrecords)*sizeof(char**));
  92. lastname[*numofrecords-1]=(char*)calloc(20,sizeof(char*));
  93. score=(float*)realloc(score,(*numofrecords)*sizeof(float*));
  94. scanf("%s %s %f", firstname[*numofrecords-1], lastname[*numofrecords-1], &score[*numofrecords-1]);
  95. for(int i=0; i<*numofrecords; i++){
  96. printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
  97. }
  98. }
  99. void deleterecords(char **firstname, char **lastname, float *score, int *numofrecords){
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. }
  108.  
  109. void searchbylastname(char **firstname, char **lastname, float *score, int *numofrecords){
  110. char searchlastname[21];
  111. //Taking input of the last name the user wants to search.
  112. printf("Please enter the Last Name: ");
  113. scanf("%s", searchlastname);
  114. for(int i=0; i<*numofrecords; i++){
  115. //Comparing the input first name with the last names in the records.
  116. if (strcmp(lastname[i], searchlastname) == 0){
  117. printf("First Name: %s, Last Name: %s, Score: %.2f \n", firstname[i], lastname[i], score[i]);
  118. }
  119. }
  120. }
  121. void sortbyscore(char **firstname, char **lastname, float *score, int *numofrecords){
  122. char tempfirstname[21];
  123. char templastname[21];
  124. float tempscore;
  125. for (int i = 0; i < *numofrecords - 1; ++i){
  126. for (int j = i + 1; j < *numofrecords; ++j){
  127. //Comparing two consequtive scores and sorting in ascending order.
  128. if (score[i] > score[j]){
  129. //Using strcpy to sort the First Name array.
  130. strcpy(tempfirstname, firstname[i]);
  131. strcpy(firstname[i], firstname[j]);
  132. strcpy(firstname[j], tempfirstname);
  133. //Using strcpy to sort the Last Name array.
  134. strcpy(templastname, lastname[i]);
  135. strcpy(lastname[i], lastname[j]);
  136. strcpy(lastname[j], templastname);
  137. //Using strcpy to sort the Score array.
  138. tempscore = score[i];
  139. score[i] = score[j];
  140. score[j] = tempscore;
  141. }
  142. }
  143. }
  144. //Calling the print function to print records.
  145. printrecords(firstname, lastname, score, numofrecords);
  146. }
  147. void sortbylastname(char **firstname, char **lastname, float *score, int *numofrecords){
  148. char tempfirstname[21];
  149. char templastname[21];
  150. float tempscore;
  151. for (int i = 0; i < *numofrecords - 1 ; ++i){
  152. for (int j = i + 1; j < *numofrecords; ++j){
  153. //Comparing two consequtive lastnames and sorting in ascending order.
  154. if (strcmp(lastname[i], lastname[j])>0){
  155. //Using strcpy to sort the First Name array.
  156. strcpy(tempfirstname, firstname[i]);
  157. strcpy(firstname[i], firstname[j]);
  158. strcpy(firstname[j], tempfirstname);
  159. //Using strcpy to sort the Last Name array.
  160. strcpy(templastname, lastname[i]);
  161. strcpy(lastname[i], lastname[j]);
  162. strcpy(lastname[j], templastname);
  163. //Using strcpy to sort the Score array.
  164. tempscore = score[i];
  165. score[i] = score[j];
  166. score[j] = tempscore;
  167. }
  168. }
  169. }
  170. //Calling the print function to print records.
  171. printrecords(firstname, lastname, score, numofrecords);
  172. }
  173. void findmedianscore(char **firstname, char **lastname, float *score, int *numofrecords){
  174.  
  175.  
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement