****NOTE: CANNOT BE TOO COMPLICATED, NOTHING MORE ADVANCED THAN FOR LOOPS, WHILE LOOPS, ETC ************
First, write a function deleteFromArray which receives three arguments in its parameters: an array of integers; an offset indicating a position in the array; and an integer containing the size of the array. The function then deletes the value at the position indicated by the offset (by shifting all values to the right of the deleted value one offset to the left), and finally returns the new size of the array.
Next, write a program to use this function. Particularly, the program first sets, declares, and initializes an array as follows:
int myValues[10] = {0,1,1,2,3,5,8,13,21,34};
int arraySize = 10;
Next, the program repeatedly displays the array to the user, asking whether they wish for a something to be deleted. If the user responds in affirmative, the program asks them for the offset in the array of the value to be deleted. It then calls your deleteFromArray function to carry out the actual deletion, receiving the new size of the array from the function's return value. If the user asks to delete from an offset that goes beyond the end of the array, an appropriate message is displayed stating that the deletion cannot be carried out. The program ends the first time that the user chooses not to delete a value from the array.
Your main function may also call other functions to carry out some of its work. In other words, you may choose to encapsulate parts of the program's tasks in additional function, such as a function that displays the array on the console, and so on.
Here is an example execution of the required program (input typed by the user is in green). The formatting of your program's output should match this example.
Array: {0,1,1,2,3,5,8,13,21,34}
Want to delete something? (y/n) y
Offset to delete? 3
Array: {0,1,1,3,5,8,13,21,34}
Want to delete something? (y/n) y
Offset to delete? 7
Array: {0,1,1,3,5,8,13,34}
Want to delete something? (y/n) y
Offset to delete? 9
Sorry, can't delete that.
Want to delete something? (y/n) n
Goodbye!
Carefully note the following in the example output above: The user first asks for deletion at offset 3, which refers to the value 2 in the array. Every value to the right of the 2 in the original array must be consequently copied one position to the left. For example, value 8 was at offset 6 before the first deletion (we deleted value 2, remember?), but appears at offset 5 (one left of its previous location) after the deletion. Also note that 10 elements were displayed before the first deletion, but 9 elements are displayed after it. To get this right, you need to keep track of the number of elements in the array, as well as the array itself. Also note that when the user tries to delete the item at offset 9 after the two deletions, when there were only 8 valid elements remaining in the array, the program declines to carry out the deletion.
Now, in a separate int main, write a program that asks the user for a positive integer N, and then uses that integer to compute and display the following four sums and products:
- The sum of integers 1 ... N inclusive.
- The sum of the powers of 2: 20 + 21 + 22 + ... + 2(N-1)
- The sum of the inverse of the powers of 2: 1/20 + 1/21 + 1/22 + ... + 1/2(N-1)
- The product of the integers 1 ... N inclusive
Here is an example execution of the program with input 6 (the user inputs 6 in response to the request "Input N: ", but the program displays everything else). Note that your output should be formatted exactly as shown in the example below:
Input N: 6
1 + 2 + 3 + 4 + 5 + 6 = 21
1 + 2 + 4 + 8 + 16 + 32 = 63
1 + 0.5 + 0.25 + 0.125 + 0.0625 + 0.03125 = 1.96875
1 * 2 * 3 * 4 * 5 * 6 = 720
Make sure you test your program! Your program should work for any positive integer (i.e., greater than 0)!
Hint: Do this question in stages. Get the first sum working perfectly (tested and everything!) before going on to the second. You will cause yourself a lot of grief if you try to do this one all at once. And you can use the ideas you develop from earlier parts to solve later parts.