# Assignment #6 - Array Practice

Due: Thurs, July 22

## Objective

To gain practice with arrays and common array algorithms, as well as the use of array parameters in functions.

This assignment will consist of writing several functions that manipulate arrays or access data from arrays, as well as a test program that will allow interactive testing of the array functions.

### Part 1: Functions

Write the following functions. Each one takes in an integer array as a parameter, and other necessary parameters are returns are described. Make sure the parameters are in the order specified. Make sure to use the const qualifier on the array parameter on any function where it is appropriate. A sample CALL is given for each function.
• #### Insert

Write a function called Insert that takes in four parameters:
• an integer array
• the size of the array
• the new value to be inserted into the array
• the index at which to insert the new value
This function should insert a a new value into the array, at the specified index. Note that this means that other values have to "move" to make room. The last value in the array will just disappear from the array. If the index is out of bounds for the array, abort the function with no change made to the array. This function does not return a value. Sample call:
```  // Suppose the array "list" is {2, 4, 6, 8, 10, 12}

Insert(list, 6, 100, 3);	// insert the value 100 at index 3.

// "list" is now {2, 4, 6, 100, 8, 10}
```
• #### Delete

Write a function called Delete that takes in three parameters:
• an integer array
• the size of the array
• the index of the item to delete
This function should delete the value at the given index. The remaining items in the array will need to shift over to fill the "empty" slot. The last item in the array (now vacant) should be set to 0. If the given index is out of bounds for the array, abort the function without deleting anything. This function does not return a value. Sample call:
```  // Suppose the array "list" is {2, 4, 6, 8, 10, 12}

Delete(list, 6, 2);	// delete the value at index 2.

// "list" is now {2, 4, 8, 10, 12, 0}
```
• #### Reverse

Write a function called Reverse that takes in two parameters:
• an integer array
• the size of the array
This function should reverse the order of the array's contents. No returned value. Sample call:
```  // Suppose the array "list" is {2, 4, 6, 8, 10, 12}

Reverse(list, 6);	// Reverse the array "list"

// "list" is now {12, 10, 8, 6, 4, 2}
```
• #### HowMany

Write a function called HowMany that takes in three parameters:
• an integer array
• the size of the array
• an integer value to check for in the array
This function should count the number of occurences of the given value in the array (i.e. how many times this value appears), and then return the count. Sample call:
```  // Suppose "list" is {2, 5, 5, 10, 5, 10, 3, 4, 5, 9, -1, 6}

HowMany(list, 12, 5)	// returns how many times the value 5 appears
//  in array "list".  This call returns 4
//  because the value 5 appears 4 times.
```
• #### SumOdds

Write a function called SumOdds that takes in three parameters:
• an integer array
• size of the array
• how many odd numbers to add (N, in the description below)
This function should compute and return the sum of the first N odd values in the array. (If there are fewer than N odd numbers, then you'll just add them all (i.e. all of the odd numbers)). Sample calls:
```  // suppose "list" is {1, 3, 4, 6, 9, 10, 12, 0, 5, 11, -3}

SumOdds(list, 11, 4);	// this call should return the value 18
SumOdds(list, 11, 8);	// this call should return the value 26
//   (there are fewer than 8 odd numbers)
```
Note that none of these functions do any keyboard input or screen output.

### Part 2: Test Program

To help you test and get you started, I've provided you with a starter file, which you can download from this link. The starter file already contains the PrintArray function that we looked at in lecture class, as well as a function called FillArray that allows the user to enter values into an array from the keyboard. You may use these functions in writing the following test program.

Write a main() function that creates an array of size SIZE (a constant given in the starter file). Since this is a constant, it can be changed for testing different sizes of arrays easily. Use this constant whenever referring to the array's size from main() (instead of using a hard-coded literal value).

Then, the program should go into a menu loop, presenting the user with the following menu the first time:

```	** Given features **
P	Print the array contents
F	Fill the array (user entry)

** Function Tests **
I	Insert
D	Delete
R	Reverse
H	HowMany
S	SumOdds

Q	Quit this program
```
• The menu only needs to be printed explicitly the first time. Then, only re-print the menu if the user selects the M menu option.
• When you finish with one menu option, re-prompt the user to enter a new menu selection with the following:
```Enter your menu selection:
```
• The first two menu choices should just invoke the given functions: PrintArray and FillArray. The first will print the array contents, and the second will populate the array with user-entered values.
• The Q option should exit the menu loop and allow the program to end. Print out the final array before quitting the program
• The 5 menu options under **Function Tests** will test your functions. Some of them call for extra user input. The options should behave as follows (make sure to do any user input in the order specified):
• I: Prompt the user to type the value to be inserted, then the index to insert at. Call the Insert function appropriately, then print out the contents of the array
• D: Prompt the user to type the index to be deleted. Call the Delete function appropriately, then print out the contents of the array.
• R: No extra input required. Just call the Reverse function appropriately, then print out the array
• S: Prompt the user to enter how many odd numbers to add. Whenever the value entered is larger than the array's size (or less than 0), print an error message and make the user re-enter. Once a valid value is entered, call the SumOdds function and print out a message showing the result. Example:
`The sum of the first 5 odd numbers is:  18`
• H: Prompt the user to enter a value to search for. Then call the HowMany function and print out a message indicating how many times this value was found in the array. Example:
`The value 6 appears in the list 3 times`
Hint: A good way to implement a menu loop is a switch statement (for processing a menu selection) inside of a do-while loop (for repeating the menu selection process).

### General Requirements

• No global variables
• The required array tasks must be performed with the functions specified (not just with a single main() routine). Note that your 5 functions should not have any cout/cin statements in them.
• You may not change the two provided functions in any way (PrintArray and FillArray)
• You will need the iostream library. You may also use iomanip and/or cctype if you like. (No other libraries).
• As always, your source code must be readable and appropriately documented

To see an example of the menu program running interactively, you can run my version of the executable by logging onto the machine:
```  program.cs.fsu.edu
```
and at the prompt, type in the command:
```  ~myers/c++prog/menu6
```

### Submitting:

Program submissions should be done through the submission web page, linked from the main course web site. Submit the file:
``` hw6.cpp
```