Advertisement
M_c_Ruer

homework 2 guide

Mar 4th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. CS 1050 Homework Assignment 2 – Array Operations –SPRING 2012
  2. Due date: Sunday, March 4th by 8 PM
  3.  
  4. WARNING – Technology failing is not an excuse to turn in your
  5. assignment late. START EARLY and if there are problems we will have time to fix them before the deadline.
  6.  
  7. Directions:
  8. Complete the following homework assignment using the description given in each section.
  9.  
  10. Purpose:
  11.  
  12. Use an array to gather and perform operations on user input
  13. Use a menu to handle different user input
  14. Learn algorithms for sorting and searching arrays
  15. Learn how to manipulate arrays by going through individual elements
  16.  
  17. Submission information:
  18. Submit this assignment using the BABBAGE SUBMISSION SYSTEM by the deadline – NO LATER! It must be submitted before the due date. Not doing so WILL result in a zero, no exceptions. If you have problems with this contact Alex Lobzhanidze via email: agl7dd@mizzou.edu. DO NOT WAIT TILL THE LAST DAY. Do not submit on Blackboard or by email!
  19.  
  20. submit1050 homework2.c HW2
  21.  
  22. Description:
  23.  
  24. You will have to write a program that will gather input from the user and store it into an array. Your program will then perform various operations on this array, including sorting it and searching for numbers in it. The array should have a constant size by using a #define statement at the top of your program, like so:
  25.  
  26. #define SIZE 5
  27.  
  28. NOTE: TAs may not grade with SIZE = 5. Your code should work even if we change the value of SIZE!
  29.  
  30. Your program should ask for numbers from the user and store them in the array. (Assume they will all be integers.) Afterwards, you should display a menu asking the user what they wish to do. See the example output for details. The menu should repeat until the user chooses to exit the program by entering a sentinel value.
  31.  
  32. Program variables and function prototypes
  33.  
  34. Define a constant in the program SIZE and make it equal to 5. Here are the functions you will need to implement for this homework. For each of these functions, n is the number of elements in the array (do not use SIZE within your functions or you will lose points; only use it in main):
  35. void loadArray(int array[], int n);
  36. - Load the array by asking the user for values.
  37.  
  38. void printArray(int array[], int n);
  39. - Print out the array (see sample output for example).
  40.  
  41. void sortArray(int array[], int n);
  42. - Sort the array using any sorting algorithm (recommended is bubble sort for simplicity). Array should be sorted in ascending order (A[0] <= A[1] <= A[2] …)
  43.  
  44. int searchArray(int array[], int n, int val);
  45. - Search the array for val. Return the index of val if found or -1 if the value is not in the array. Again, any algorithm may be used to search – a linear search is the simplest. If val is in the array multiple times, returning any index is sufficient.
  46.  
  47. void addToArray(int array[], int n, int val);
  48. - Add the number val to every element of the array.
  49.  
  50. void multiplyArray(int array[], int n, int val);
  51. - Multiply every element of the array by val.
  52.  
  53. The program should run in an infinite loop, until the user enters the sentinel value at the menu screen.
  54.  
  55. Example output (bold is user input):
  56.  
  57. [cdh7x9@babbage ~]$ ./a.out
  58. Enter value 0: 4
  59. Enter value 1: 9
  60. Enter value 2: 2
  61. Enter value 3: 6
  62. Enter value 4: 5
  63.  
  64. What would you like to do?
  65. 1) Print the array
  66. 2) Sort the array
  67. 3) Search the array
  68. 4) Add to the array
  69. 5) Multiply the array
  70. 0) Exit
  71.  
  72. Option: 1
  73. Array[0] = 4
  74. Array[1] = 9
  75. Array[2] = 2
  76. Array[3] = 6
  77. Array[4] = 5
  78.  
  79.  
  80. What would you like to do?
  81. 1) Print the array
  82. 2) Sort the array
  83. 3) Search the array
  84. 4) Add to the array
  85. 5) Multiply the array
  86. 0) Exit
  87.  
  88. Option: 2
  89.  
  90. Sorted array:
  91. Array[0] = 2
  92. Array[1] = 4
  93. Array[2] = 5
  94. Array[3] = 6
  95. Array[4] = 9
  96.  
  97. What would you like to do?
  98. 1) Print the array
  99. 2) Sort the array
  100. 3) Search the array
  101. 4) Add to the array
  102. 5) Multiply the array
  103. 0) Exit
  104.  
  105. Option: 3
  106. Enter a number to search for: 4
  107. The value 4 was found at index 1.
  108.  
  109. What would you like to do?
  110. 1) Print the array
  111. 2) Sort the array
  112. 3) Search the array
  113. 4) Add to the array
  114. 5) Multiply the array
  115. 0) Exit
  116.  
  117. Option: 4
  118. Enter a number to add to each element of the array: 3
  119.  
  120. Modified array:
  121. Array[0] = 5
  122. Array[1] = 7
  123. Array[2] = 8
  124. Array[3] = 9
  125. Array[4] = 12
  126.  
  127. What would you like to do?
  128. 1) Print the array
  129. 2) Sort the array
  130. 3) Search the array
  131. 4) Add to the array
  132. 5) Multiply the array
  133. 0) Exit
  134.  
  135. Option: 5
  136. Enter a number to multiply each element of the array by: 2
  137.  
  138. Modified array:
  139. Array[0] = 10
  140. Array[1] = 14
  141. Array[2] = 16
  142. Array[3] = 18
  143. Array[4] = 24
  144.  
  145. What would you like to do?
  146. 1) Print the array
  147. 2) Sort the array
  148. 3) Search the array
  149. 4) Add to the array
  150. 5) Multiply the array
  151. 0) Exit
  152.  
  153. Option: 3
  154. Enter a number to search for: -1
  155. The value -1 was not found in the array.
  156.  
  157. What would you like to do?
  158. 1) Print the array
  159. 2) Sort the array
  160. 3) Search the array
  161. 4) Add to the array
  162. 5) Multiply the array
  163. 0) Exit
  164. Option: 0
  165.  
  166. Ending program.
  167. Guidelines for Grading Homework 2
  168. 50 Points Possible
  169.  
  170. General
  171. If your program does not compile, or produce any input/output (I/O) because most of the source code is commented out then your homework will receive a grade of NO MORE THAN 15 OUT OF 50.
  172.  
  173. 10 points – For the sortArray function
  174. 10 points – For the searchArray function
  175. 10 points – For the addToArray and multiplyArray functions
  176. 5 points – While loop with switch-case statement properly implemented
  177. 5 points – For the loadArray and printArray functions
  178. 5 points – Code is properly commented, both at the top and in the code itself (every function should have a brief description of its inputs and outputs as well as what it does conceptually)
  179. 5 points – Code follows good style and practices, such as indentation and consistent curly braces
  180.  
  181. Warning – if you make a global array to avoid passing it between your functions, you will automatically lose 25 points from your grade. This is in addition to any other points you may lose for comments or functions not working properly, etc. Do not make your array global!
  182.  
  183. Example of commenting functions
  184.  
  185. // Pow function – calculates the value of base raised to exp
  186. // Inputs – base: an integer that is the base of the exponent
  187. // exp: an integer that is the power of the exponent
  188. // Output – an int that is (base ** exp)
  189. int pow(int base, int exp)
  190. {
  191. // Code goes here..
  192. }
  193.  
  194. Bonus
  195.  
  196. You must implement a new function according to the following prototype:
  197.  
  198. int binarySearch(int array[], int n, int val);
  199.  
  200. This function should search through the array using the binary search algorithm. If you don’t remember this from CS1040 you can find it easily using Google. Note that the array must be sorted to use binary search. To be safe, you should sort it BEFORE attempting to search it, just in case it’s unsorted. You may implement this function either iteratively or recursively; using recursion is the recommended approach.
  201.  
  202. In addition, you must submit your homework early, by 11:59 pm on March 2nd, to be eligible for the extra credit. Please include in your header a comment stating that you submitted early, to make it easier for TAs to see when grading.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement