Advertisement
Guest User

Untitled

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