M4ritimeSeeker

PASS Week 3/31

Mar 28th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. PROBLEM #1
  2.  
  3. Create a program that will declare an array of size 100 that contains random values between 1 and 999.
  4.  
  5. 1) Create a user defined function that will load that array with 100 random integers.
  6. 2) Create a second function that prints the array to the screen 10 numbers per line.
  7. Use a minimum width specifier to ensure the numbers line up (For ease of reading/testing).
  8.  
  9. The main function should ONLY CONTAIN 5 LINES OF CODE: srand, the array declaration, two function calls, and a return statement.
  10. You must use srand() in main. Sample output and Skeleton code provided below.
  11.  
  12. Sample Output:
  13.  
  14. 945 832 578 934 24 75 617 471 262 95
  15. 726 536 997 657 252 858 943 752 435 4
  16. 542 762 369 571 891 325 495 103 815 838
  17. 453 479 389 32 414 412 824 749 882 86
  18. 843 609 340 840 266 592 417 927 344 852
  19. 931 604 614 300 175 505 343 388 607 159
  20. 226 778 637 614 528 769 27 352 518 627
  21. 156 362 236 496 920 220 806 338 148 868
  22. 190 797 472 522 815 365 27 159 472 352
  23. 36 697 848 672 31 377 442 775 447 678
  24.  
  25.  
  26.  
  27.  
  28. /*** SKELETON CODE ***/
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33.  
  34. #define MAXSZ 100 //Defined constant for size of array
  35.  
  36. void LoadArray(int[], int); //First parameter is array, second is size of array
  37. void PrintArray(int[], int); //First parameter is array, second is size of array
  38.  
  39.  
  40. int main(void)
  41. {
  42. srand(Time(NULL));
  43.  
  44. int ary[MAXSZ]; //Declare Array of size 100 (Variable MAXSZ is equal to 100)
  45.  
  46. LoadArray(ary, MAXSZ);
  47. PrintArray(ary, MAXSZ);
  48.  
  49. return 0;
  50. }//end main
  51.  
  52. /*** LOAD ARRAY FUNCTION GOES HERE ***/
  53.  
  54. /*** PRINT ARRAY FUNCTION GOES HERE ***/
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Problem #2
  70.  
  71. Using the program you created in Problem #1, do the following:
  72.  
  73. 1) Create a third function that will prompt the user for an integer value between
  74. 1 and 999, and search for that number. (Sequential Search). The 2 parameters
  75. needed for this function are the array, and the size of the array.
  76. 2) Put your user prompts for the value to search for INSIDE the function. DON'T
  77. prompt the user in main.
  78. 3) Call that function AFTER the array is loaded.
  79.  
  80. If the value is found, print out the index location of the item and continue to search to
  81. the end of the array. (There may be more than one occurrence of the same value in
  82. the array, try to accommodate for repeats in your program).
  83.  
  84. Sample output (Value found):
  85.  
  86. Enter a value to be searched for: 5
  87. Number 5 found at index 91.
  88.  
  89.  
  90.  
  91. Sample output (Value not found):
  92.  
  93. Enter a value to be searched for: 5
  94. Number not found
  95.  
  96.  
  97.  
  98. Sample output (Multiple values found):
  99.  
  100. Enter a value to be searched for: 99
  101. Number 99 found at index 5.
  102. Number 99 found at index 77.
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. Problem #3
  119.  
  120. Using the program you made in Problem #2, do the following:
  121.  
  122. 1) Implement the Bubble Sort function in your program from Problem #2. This can be called
  123. in main to sort the array from least to greatest. THIS FUNCTION IS GIVEN TO YOU, PROVIDED BELOW.
  124. 2) Now change main to call the functions in this order: load the array, print the array, sort the array,
  125. and then print out the array again.
  126. 3) Comment out your search function call from problem #2.
  127. 4) Run the program and make sure it sorted all of the items. If it isn't sorted, check your functions & calls.
  128. If the first or last few values aren't sorted but the rest is, check your loop condition or iterator.
  129.  
  130. Visualizing how the bubble sort can be difficult, try drawing it out on paper with your array
  131. step-by-step and see what it is doing. Refer to modules, teammates, or me for help.
  132. There are also good videos on YouTube that show the bubble sort in action.
  133.  
  134.  
  135. Sample Output:
  136.  
  137. 391 198 748 560 255 641 660 547 825 624
  138. 851 747 772 469 974 893 323 520 461 571
  139. 410 348 828 949 667 931 10 173 860 148
  140. 733 969 345 481 529 318 122 907 865 947
  141. 251 716 694 23 185 387 634 226 625 96
  142. 516 35 162 63 984 547 994 993 719 854
  143. 141 452 542 485 651 72 803 773 697 387
  144. 439 947 103 133 689 6 239 323 950 863
  145. 137 466 617 18 528 601 283 241 313 721
  146. 813 454 173 356 657 543 146 179 316 842
  147.  
  148. 6 10 18 23 35 63 72 96 103 122
  149. 133 137 141 146 148 162 173 173 179 185
  150. 198 226 239 241 251 255 283 313 316 318
  151. 323 323 345 348 356 387 387 391 410 439
  152. 452 454 461 466 469 481 485 516 520 528
  153. 529 542 543 547 547 560 571 601 617 624
  154. 625 634 641 651 657 660 667 689 694 697
  155. 716 719 721 733 747 748 772 773 803 813
  156. 825 828 842 851 854 860 863 865 893 907
  157. 931 947 947 949 950 969 974 984 993 994
  158.  
  159.  
  160.  
  161.  
  162. HERE IS THE CODE FOR BubbleSort TO ADD TO YOUR PROGRAM:
  163.  
  164. //Function prototype (Put this above main)
  165. void BubbleSort (int[], int);
  166.  
  167. //Function definition (Put this below main)
  168. //Parameter "aryIn" is your Array
  169. //Parameter "last" is the end index in your array
  170. void BubbleSort(int aryIn[],int last)
  171. {
  172. int temp = 0;
  173. for(int current = 0; current < last; current++)
  174. {
  175. for(int walker = last; walker > current; walker--)
  176. {
  177. if(aryIn[walker] < aryIn[walker -1])
  178. {
  179. temp = aryIn[walker];
  180. aryIn[walker] = aryIn[walker - 1];
  181. aryIn[walker - 1] = temp;
  182.  
  183. }//end if
  184. }//end inner for
  185. }//end outer for
  186.  
  187. return;
  188. }//end bubbleSort
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. Problem #4
  208.  
  209. Using your program from Problem #3, do the following:
  210.  
  211. 1) Add a new search function to your program that performs a binary search.
  212. THIS FUNCTION IS GIVEN TO YOU, PROVIDED BELOW.
  213. 2) In main: comment out your sequential search function call (if you haven't already),
  214. then load the array, bubble sort the array, then search the array with your new binary search function.
  215. 3) The search function should return the index location of the item found.
  216.  
  217. Recall that the item could be at index 0, so you may need to check that.
  218. You will need to alter how you test the results in main. Same as the bubble sort,
  219. if you have trouble visualizing it refer to your teammates, modules, me, or the internet.
  220. There are a lot of good videos that visualize the process.
  221.  
  222. Sample Output:
  223.  
  224. 389 777 677 792 509 558 170 969 104 649
  225. 947 93 587 388 623 432 227 928 804 869
  226. 445 109 722 440 473 369 427 797 406 260
  227. 852 513 755 529 305 265 806 194 952 909
  228. 842 619 721 148 7 344 580 952 990 103
  229. 821 154 211 262 593 683 631 21 200 755
  230. 998 770 269 753 19 292 736 824 485 408
  231. 452 47 27 173 194 33 235 773 703 226
  232. 595 524 379 805 505 972 208 136 711 407
  233. 609 709 177 877 462 195 170 917 737 373
  234.  
  235. Enter a value to be searched for: 7
  236. Number found at index 0
  237.  
  238.  
  239.  
  240.  
  241. Here is the function for the Binary Search to add to your program:
  242.  
  243. //Function prototype (Put this above main)
  244. int BinarySearch (int[], int, int);
  245.  
  246. //Function definition (Put this below main)
  247. //This function only works on sorted arrays!
  248. //Parameter "list" is your array
  249. //Parameter "end" is the last index of your array
  250. //Parameter "target" is the number you are searching for
  251. //THIS FUNCTION RETURNS THE INDEX LOCATION OF THE NUMBER YOU SEARCH FOR
  252.  
  253. int BinarySearch(int list[], int end, int target)
  254. {
  255. int location = 0, first = 0, mid, last = end;
  256.  
  257. while(first <= last)
  258. {
  259. mid = (first + last) /2;//Find the middle
  260.  
  261. if(target > list[mid])
  262. first = mid + 1; //Look in the upper half of list
  263. else if (target < list[mid])
  264. last = mid - 1; //Look in the lower half of list
  265. else
  266. first = last + 1;//Found the item force quit
  267. }//end while
  268.  
  269. if(list[mid] == target)//What if item is at index 0?
  270. return mid;
  271. else
  272. return -1;
  273. }//end BinarySearch
  274.  
  275.  
Add Comment
Please, Sign In to add comment