Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CS 1050 Homework Assignment 2 – Array Operations –SPRING 2012
- Due date: Sunday, March 4th by 8 PM
- WARNING – Technology failing is not an excuse to turn in your
- assignment late. START EARLY and if there are problems we will have time to fix them before the deadline.
- Directions:
- Complete the following homework assignment using the description given in each section.
- Purpose:
- Use an array to gather and perform operations on user input
- Use a menu to handle different user input
- Learn algorithms for sorting and searching arrays
- Learn how to manipulate arrays by going through individual elements
- Submission information:
- 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!
- submit1050 homework2.c HW2
- Description:
- 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:
- #define SIZE 5
- NOTE: TAs may not grade with SIZE = 5. Your code should work even if we change the value of SIZE!
- 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.
- Program variables and function prototypes
- 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):
- void loadArray(int array[], int n);
- - Load the array by asking the user for values.
- void printArray(int array[], int n);
- - Print out the array (see sample output for example).
- void sortArray(int array[], int n);
- - 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] …)
- int searchArray(int array[], int n, int val);
- - 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.
- void addToArray(int array[], int n, int val);
- - Add the number val to every element of the array.
- void multiplyArray(int array[], int n, int val);
- - Multiply every element of the array by val.
- The program should run in an infinite loop, until the user enters the sentinel value at the menu screen.
- Example output (bold is user input):
- [cdh7x9@babbage ~]$ ./a.out
- Enter value 0: 4
- Enter value 1: 9
- Enter value 2: 2
- Enter value 3: 6
- Enter value 4: 5
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 1
- Array[0] = 4
- Array[1] = 9
- Array[2] = 2
- Array[3] = 6
- Array[4] = 5
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 2
- Sorted array:
- Array[0] = 2
- Array[1] = 4
- Array[2] = 5
- Array[3] = 6
- Array[4] = 9
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 3
- Enter a number to search for: 4
- The value 4 was found at index 1.
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 4
- Enter a number to add to each element of the array: 3
- Modified array:
- Array[0] = 5
- Array[1] = 7
- Array[2] = 8
- Array[3] = 9
- Array[4] = 12
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 5
- Enter a number to multiply each element of the array by: 2
- Modified array:
- Array[0] = 10
- Array[1] = 14
- Array[2] = 16
- Array[3] = 18
- Array[4] = 24
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 3
- Enter a number to search for: -1
- The value -1 was not found in the array.
- What would you like to do?
- 1) Print the array
- 2) Sort the array
- 3) Search the array
- 4) Add to the array
- 5) Multiply the array
- 0) Exit
- Option: 0
- Ending program.
- Guidelines for Grading Homework 2
- 50 Points Possible
- General
- 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.
- 10 points – For the sortArray function
- 10 points – For the searchArray function
- 10 points – For the addToArray and multiplyArray functions
- 5 points – While loop with switch-case statement properly implemented
- 5 points – For the loadArray and printArray functions
- 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)
- 5 points – Code follows good style and practices, such as indentation and consistent curly braces
- 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!
- Example of commenting functions
- // Pow function – calculates the value of base raised to exp
- // Inputs – base: an integer that is the base of the exponent
- // exp: an integer that is the power of the exponent
- // Output – an int that is (base ** exp)
- int pow(int base, int exp)
- {
- // Code goes here..
- }
- Bonus
- You must implement a new function according to the following prototype:
- int binarySearch(int array[], int n, int val);
- 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.
- 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