Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <dos.h>
  4. #define max 60
  5.  
  6. #define g gotoxy
  7. #define p printf
  8. #define s scanf
  9.  
  10. int r, ratings[max]={0};
  11.  
  12. void menu();
  13. void input();
  14. void rating();
  15. void mean();
  16. void median();
  17. void sort_use();
  18. void selection_sort();
  19. void insertion_sort();
  20. void binary_search();
  21. void exit();
  22. void choose_again();
  23. void border();
  24.  
  25. void main ()
  26. {
  27. clrscr();
  28. border();
  29. menu();
  30. getch();
  31. }
  32.  
  33. //this function contains the choices of the menu
  34. void menu()
  35. {
  36. int choice;
  37. p ("\t\t\t\t\tMENU:\n");
  38. p ("\t\t\t1. Input Ratings\n");
  39. p ("\t\t\t2. Mean\n");
  40. p ("\t\t\t3. Median\n");
  41. p ("\t\t\t4. Output Ratings, Mean & Median\n");
  42. p ("\t\t\t5. Binary Search\n");
  43. p ("\t\t\t6. Exit\n");
  44. p ("\t\t\tAnswer: ");
  45. s ("%d",&choice);
  46. switch(choice)
  47. {
  48. case 1: input();
  49. choose_again();
  50. break;
  51. case 2: mean();
  52. choose_again();
  53. break;
  54. case 3: sort_use();
  55. median();
  56. choose_again();
  57. break;
  58. case 4: rating();
  59. mean();
  60. median();
  61. choose_again();
  62. break;
  63. case 5: binary_search();
  64. choose_again();
  65. break;
  66. case 6: exit();
  67. break;
  68. }
  69. }
  70.  
  71. //this function is for the input of ratings, Option 1
  72. void input()
  73. {
  74. clrscr();
  75. p ("How many ratings would you like? ");
  76. s ("%d",&r);
  77. if (r > 60)
  78. {
  79. p ("Sorry, this program will only accomodate a maximum of 60 ratings.\n");
  80. choose_again();
  81. }
  82. else
  83. p ("Please Input %d ratings: ", r);
  84. for (int i=0; i<r; i++)
  85. s ("%d",&ratings[i]);
  86. }
  87.  
  88. //this function if for displaying the ratings entered by the user
  89. void rating()
  90. {
  91. for (int i=0; i<r; i++)
  92. p ("\nRating %d: %d", i+1, ratings[i]);
  93. }
  94.  
  95. //this function is for getting the mean of the ratings, Option 2
  96. void mean()
  97. {
  98. float sum=0, mean;
  99. for (int i=0; i<r; i++)
  100. sum = sum + ratings[i];
  101. mean = sum / r;
  102. p ("\nThe mean is: %.2f.\n", mean);
  103. }
  104.  
  105. //this function is for the user to choose what type of sort will be used, Option 3
  106. void sort_use()
  107. {
  108. char sort, i, I;
  109. p ("\nWhat kind of sorting would you prefer? ");
  110. p ("\n(I is for Insertion sorting and S is for Selection sorting) ");
  111. s (" %c", &sort);
  112. if (sort=='I' || sort == 'i')
  113. insertion_sort();
  114. else
  115. selection_sort();
  116. }
  117.  
  118. //this function is for insertion sort
  119. void insertion_sort()
  120. {
  121. int l, d, temp;
  122. for (l=1; l<=r-1; l++)
  123. {
  124. d = l;
  125. while ( d>0 && ratings[d-1] > ratings[d])
  126. {
  127. temp = ratings[d];
  128. ratings[d] = ratings[d-1];
  129. ratings[d-1] = temp;
  130. d--;
  131. }
  132. }
  133. p ("Sorted list in ascending order:\n");
  134. for (l=0; l<=r-1; l++)
  135. p ("%d\t", ratings[l]);
  136. }
  137.  
  138. //this function is for selection sort
  139. void selection_sort()
  140. {
  141.  
  142. int i, j, loc, temp=0, min;
  143. for (i=0; i<r-1; i++)
  144. {
  145. min=ratings[i];
  146. loc=i;
  147. for (j=i+1; j<r; j++)
  148. {
  149. if (min>ratings[j])
  150. {
  151. min=ratings[j];
  152. loc=j;
  153. }
  154. }
  155. temp=ratings[i];
  156. ratings[i]=ratings[loc];
  157. ratings[loc]=temp;
  158. }
  159. p ("Sorted list is as follows:\n");
  160. for (i=0; i<r; i++)
  161. p ("%d\t", ratings[i]);
  162. }
  163.  
  164. //this function is for getting the median of the ratings entered by the user, Option 3
  165. void median()
  166. {
  167. float median=0;
  168. median= ( ratings[0] + ratings[r-1] ) / 2;
  169. p ("\nThe median is %.2f.\n", median);
  170. // gotoxy() c
  171. // printf("\nThe formula for solving the median is the lowest and highest array to be added then divided by 2");
  172. }
  173.  
  174. //this function is for binary search, Option 5
  175. void binary_search()
  176. {
  177. int search, first, last, middle;
  178. p ("Enter element to find: ");
  179. s ("%d", &search);
  180. first=0;
  181. last=r-1;
  182. middle=(first+last)/2;
  183. while (first<=last)
  184. {
  185. if (ratings[middle]<search)
  186. first=middle+1;
  187. else if (ratings[middle]==search)
  188. {
  189. p ("%d found at location %d and array %d.\n", search, middle+1, middle);
  190. break;
  191. }
  192. else last=middle-1;
  193. middle=(first+last)/2;
  194. }
  195. if (first>last)
  196. p ("%d is not present on the list.\n", search);
  197. }
  198.  
  199. //this function allows the user to choose another option from the menu again
  200. void choose_again()
  201. {
  202. char sagot;
  203. char Y, y;
  204. p ("Would you like to choose from our menu again? ");
  205. s (" %c", &sagot);
  206. if (sagot == 'y' || sagot == 'Y')
  207. {
  208. clrscr();
  209. menu();
  210. }
  211. else exit();
  212. }
  213.  
  214. //this function exits the program
  215. void exit()
  216. {
  217. clrscr();
  218. p ("Thank you and have a good day!\n");
  219. p ("\t Bronola & Cuyong");
  220. }
  221.  
  222. void border()
  223. {
  224. int top, left, right, bottom;
  225. clrscr();
  226. /*top*/ for (top=1; top<=78; top++)
  227. {
  228. g (top, 1);
  229. cprintf ("%c", 219);
  230. //delay (10);
  231. }
  232. /*left*/ for (left=1; left<=25; left++)
  233. {
  234. g (1, left);
  235. cprintf ("%c", 219);
  236. //delay (10);
  237. }
  238. /*bottom*/ for (bottom=25; bottom<=101; bottom++)
  239. {
  240. g (bottom, 40);
  241. cprintf ("%c", 219);
  242. //delay (10);
  243. }
  244. /*right*/ for (right=25; right>=1; right--)
  245. {
  246. g (79, right);
  247. cprintf ("%c", 219);
  248. //delay (10);
  249. }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement