Advertisement
M4ritimeSeeker

PASS Week 11/03

Nov 1st, 2020 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. PROBLEM #1
  2.  
  3. Create a program that will declare an array of 100 integers of values between 1 and
  4. 999. Create a user defined function that will load that array in the caller (main) with
  5. 100 random variables. Create a second function that will then be called to print the
  6. array to the screen 10 numbers per line for 10 lines. Use a minimum width specifier
  7. to ensure the numbers are ordered. The main function should ONLY CONTAIN the array
  8. declaration, two function calls, and a return statement. You must use srand() in
  9. main.
  10.  
  11.  
  12.  
  13. /*** SKELETON CODE ***/
  14. // All of the problems build off one another, so only this one skeleton is needed
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #define MAXSZ 100
  20.  
  21. void LoadArray(int[], int);
  22. void PrintArray(int[], int);
  23.  
  24. int main(void)
  25. {
  26. /*** LOCAL STATEMENTS AND DECLARATIONS GO HERE ***/
  27.  
  28. return 0;
  29. }//end main
  30.  
  31. /*** LOAD ARRAY FUNCTION GOES HERE ***/
  32.  
  33. /*** PRINT ARRAY FUNCTION GOES HERE ***/
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. Problem #2
  49.  
  50. Using the program you created in problem #1, create a third function that will be
  51. called after the array is loaded and comment out the function call to print the array.
  52. The new function will prompt the user for an integer value between 1 and 999. Then
  53. the function will use a sequential search to search the loaded array.
  54.  
  55. If the value is found, print out to the user the index location of the item and continue to search to
  56. the end of the array. (There may be more than one occurrence of the same value in
  57. the array). What about duplicate values?
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. Problem #3
  76.  
  77. Add yet another function to the program. This function will sort the array. I am
  78. asking you to use the bubblesort algorithm found in the Canvas material.
  79. Now change main to load the array, print the array, sort the array, and then print out the
  80. array again using your functions. Did it sort all the items? You should see the
  81. contents out of order and then you should see them in order. How could you change
  82. it from ascending to descending order? Walk through this function logically using an
  83. unsorted array of your own creation to understand it. Use a paper and pencil if need
  84. be.
  85.  
  86. Here is the code for Bubblesort to add to your program:
  87.  
  88. void BubbleSort(int aryIn[],int last)
  89. {
  90. int temp = 0;
  91. for(int current = 0; current < last; current++)
  92. {
  93. for(int walker = last; walker > current; walker--)
  94. {
  95. if(aryIn[walker] < aryIn[walker -1])
  96. {
  97. temp = aryIn[walker];
  98. aryIn[walker] = aryIn[walker - 1];
  99. aryIn[walker - 1] = temp;
  100.  
  101. }//end if
  102. }//end inner for
  103. }//end outer for
  104.  
  105. return;
  106. }//end bubbleSort
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. Problem #4
  126.  
  127. Add a new search function to your program that is a binary search. In main load the
  128. array, sort the array, then search the array (You can comment out your selection sort).
  129. Your function should return the index location of the item found.
  130. Recall that the item could be at index 0, so your function needs to check that.
  131. You will need to alter how you test the results in main. Walk through
  132. this function logically using a sorted array of your own creation to
  133. understand it. Use a paper and pencil if need be.
  134.  
  135. Here is the function for the Binary Search to add to your program:
  136.  
  137. //This function only works on sorted arrays!
  138. int BinarySearch(int list[], int end, int target)
  139. {
  140. int location = 0, first = 0, mid, last = end;
  141.  
  142. while(first <= last)
  143. {
  144. mid = (first + last) /2;//Find the middle
  145.  
  146. if(target > list[mid])
  147. first = mid + 1; //Look in the upper half of list
  148. else if (target < list[mid])
  149. last = mid - 1; //Look in the lower half of list
  150. else
  151. first = last + 1;//Found the item force quit
  152. }//end while
  153.  
  154. if(list[mid] == target)//What if item is at index 0?
  155. return mid;
  156. else
  157. return -1;
  158. }//end BinarySearch
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. { SECTION 1 CHALLENGE PROBLEM }
  168.  
  169. Using the program from problem #2, modify the load array function to not insert
  170. repeat values. Modify the SequentialSearch() function to take an added parameter
  171. as an integer and take the user prompts out and move them to main. Now you can call
  172. your search function in your load array function to examine each random value for a
  173. match in the array before adding the new element.
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement