Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. //code by Rehan Naeem
  2. #include <iostream>
  3. #include <cstdio>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. enum option {cis32 = 1, otherClasses, quit};
  9. option display_menu();
  10. void switcher(option, double [][4], int [][3], double [][5]);
  11. int record_cis32(double [][4]);
  12. void OtherClasses(int [][3], double [][5]);
  13. void selectionSort(double [][4], int);
  14. void bubbleSort();
  15. bool searchDup1(int , double [][4], int);
  16. bool searchDup2(int , int [][3] , int , int );
  17.  
  18.  
  19.  
  20.  
  21. int main()
  22. {
  23. option choose ;
  24. double arrayOpt1[10][4];
  25. int arrayOpt2[10][3];
  26. double anotherOpt2[10][5];
  27. do
  28. {
  29. choose = display_menu() ;
  30. switcher (choose, arrayOpt1, arrayOpt2, anotherOpt2) ;
  31. } while (choose != 4) ; //****************************************change to 3
  32. return 0 ;
  33. }
  34.  
  35. option display_menu()
  36. {
  37. option choose ;
  38. printf ("Choose an option:\n\n") ;
  39. printf ("1) CIS 32\n");
  40. printf ("2) Other Classes\n");
  41. printf ("3) Quit 32\n\n");
  42. scanf ("%d", &choose);
  43. return choose ;
  44.  
  45. }
  46.  
  47.  
  48. void switcher (option choose, double arrayOpt1[][4], int arrayOpt2[][3], double anotherOpt2[10][5])
  49. {
  50. bool valid;
  51. int effective1 ;
  52. do
  53. {
  54. valid = true;
  55. switch (choose)
  56. {
  57. case cis32:
  58. effective1 = record_cis32(arrayOpt1);
  59. break;
  60.  
  61. case otherClasses:
  62. OtherClasses(arrayOpt2, anotherOpt2);
  63. break;
  64.  
  65. case quit:
  66. selectionSort(arrayOpt1, effective1-1);
  67. for (int i=0; i<effective1; i++)
  68. {
  69. printf ("\n\n%.0lf", arrayOpt1[i][0]) ;
  70. }
  71.  
  72.  
  73. break;
  74.  
  75. default:
  76. printf("Wrong input.\n\n");
  77. choose = display_menu();
  78. valid = false;
  79. }
  80. } while (valid == false);
  81.  
  82. }
  83.  
  84. int record_cis32(double arrayOpt1[10][4])
  85. {
  86. static int effective1 = 0;
  87. int studID , midterm, final, sum=0;
  88. double avg;
  89. bool valid1 , valid2 ;
  90. if (effective1 < 10)
  91. {
  92. effective1 += 1;
  93. do
  94. {
  95. valid2 = true;
  96. printf("\nInput student ID: ");
  97.  
  98. do
  99. {
  100. valid1 = true ;
  101. scanf ("%d", &studID ) ;
  102. if (studID > 100 || studID < 1)
  103. {
  104. printf ("Input a valid student ID:");
  105. valid1 = false ;
  106. }
  107. } while (valid1 != true ) ;
  108. arrayOpt1[effective1][0] = studID ;
  109.  
  110. if (searchDup1(studID , arrayOpt1, effective1))
  111. {
  112. printf ("Duplicate student ID. Re-enter ID.\n");
  113. valid2 = false;
  114. }
  115. }while (valid2 == false) ;
  116.  
  117. printf("Input midterm exam score: ");
  118. do
  119. {
  120. valid1 = true ;
  121.  
  122. scanf ("%d", &midterm);
  123. if (midterm > 100 || midterm < 1)
  124. {
  125. printf ("Input a valid midterm exam score:");
  126. valid1 = false ;
  127. }
  128. }while (valid1 != true ) ;
  129. arrayOpt1[effective1][1] = midterm;
  130.  
  131. printf("Input final exam score: ");
  132. do
  133. {
  134. valid1 = true ;
  135.  
  136. scanf ("%d", &final);
  137. if (final > 100 || final < 1)
  138. {
  139. printf ("Input a valid final exam score:");
  140. valid1 = false ;
  141. }
  142. }while (valid1 != true ) ;
  143. arrayOpt1[effective1-1][2] = final ;
  144.  
  145. sum += midterm + final ;
  146. avg = double(sum) / 2.00 ;
  147. arrayOpt1[effective1-1][3] = avg ;
  148. printf("avg is: %.2lf", arrayOpt1[effective1-1][3]) ;
  149.  
  150.  
  151. printf("\n");
  152. return effective1;
  153.  
  154.  
  155. }
  156. else
  157. printf("\nOut of bounds. Choose another option.\n\n");
  158.  
  159. }
  160.  
  161. bool searchDup1(int studID, double arrayOpt1[][4] , int effective1 )
  162. {
  163. bool found = false;
  164. for (int i = 0; i<effective1; i++)
  165. {
  166. if (arrayOpt1[i][0] == studID)
  167. {
  168. found = true;
  169. break;
  170. }
  171. }
  172. return (found) ;
  173. }
  174.  
  175. void OtherClasses(int arrayOpt2[][3], double anotherOpt2[][5])
  176. {
  177. static int effective2 = 0 , effective3 = 0;
  178. int studID , classCode, score, numbScores, i = 0, sum=0, j ;
  179. double avg ;
  180. bool valid1 , valid2 ;
  181. if (effective2 < 10)
  182. {
  183. effective2 += 1 ;
  184. do
  185. {
  186. valid2 = true;
  187. printf("\nInput student ID: ");
  188.  
  189. do
  190. {
  191. valid1 = true ;
  192. scanf ("%d", &studID ) ;
  193. if (studID > 100 || studID < 1)
  194. {
  195. printf ("Input a valid student ID: ");
  196. valid1 = false;
  197. }
  198. } while (valid1 != true );
  199. arrayOpt2[effective2][0] = studID ;
  200.  
  201. printf("Input class code: ");
  202. do
  203. {
  204. valid1 = true;
  205. scanf ("%d", &classCode );
  206. if (classCode < 1)
  207. {
  208. printf ("Input a valid class code: ");
  209. valid1 = false;
  210. }
  211. } while (valid1 != true );
  212. arrayOpt2[effective2][1] = classCode ;
  213.  
  214. if (searchDup2(studID , arrayOpt2, effective2, classCode))
  215. {
  216. printf ("Duplicate pair of student ID and class code entered. Re-enter the values.\n");
  217. valid2 = false;
  218. }
  219. }while (valid2 == false);
  220.  
  221. printf("Input how many scores you want to record: ");
  222. do
  223. {
  224. valid1 = true ;
  225. scanf ("%d", &numbScores ) ;
  226. if (numbScores > 4 || numbScores < 2)
  227. {
  228. printf ("Input a number of scores (2 to 4): ");
  229. valid1 = false;
  230. }
  231. } while (valid1 != true );
  232. effective3 = numbScores ;
  233. arrayOpt2[effective2][2] = numbScores ;
  234.  
  235.  
  236. for (j = 0; j<effective3 ; j++ )
  237. {
  238. printf ("Enter score %d: ", j+1) ;
  239. do
  240. {
  241. valid1 = true ;
  242. scanf ("%d", & score) ;
  243. if (score < 0)
  244. {
  245. printf ("Input a valid score (greater than 0): ");
  246. valid1 = false;
  247. }
  248. } while (valid1 != true );
  249.  
  250. anotherOpt2[effective2-1][j] = score ;
  251. sum += score ;
  252. }
  253. avg = double(sum) / effective3 ;
  254. anotherOpt2[effective2-1][5] = avg ;
  255. printf("avg is: %.2lf", anotherOpt2[effective2-1][5]) ;
  256.  
  257.  
  258. printf ("\n");
  259. }
  260. else
  261. printf("\nOut of bounds. Choose another option.\n\n");
  262. }
  263.  
  264. bool searchDup2(int studID, int arrayOpt2[][3] , int effective2 , int classCode)
  265. {
  266. bool found = false;
  267. for (int i = 0; i<effective2 ; i++)
  268. {
  269. if (arrayOpt2[i][0] == studID && arrayOpt2[i][1] == classCode)
  270. {
  271. found = true;
  272. break;
  273. }
  274. }
  275. return (found);
  276. }
  277.  
  278. void selectionSort(double arrayOpt1[][4], int effective1)
  279. {
  280. int temp, indexLargest, index;
  281. for (; effective1 > 0; effective1--)
  282. {
  283. indexLargest = 0;
  284. for (index=1; index <= effective1; index++)
  285. {
  286. if (arrayOpt1[index][0] > arrayOpt1[indexLargest][0])
  287. indexLargest = index;
  288. }
  289. if (effective1 != indexLargest)
  290. {
  291. temp = arrayOpt1[effective1][0] ;
  292. arrayOpt1[effective1][0] = arrayOpt1[indexLargest][0] ;
  293. arrayOpt1[indexLargest][0] = temp ;
  294. }
  295. }
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement