This assignment consists of writing several functions that manipulate arrays or
ID: 3863550 • Letter: T
Question
This assignment consists 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 and returns are described - read carefully. 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.
FillArray
Write a function called FillArray that takes in four parameters:
an integer array
size of the array
a min value (int)
a max value (int)
This function should fill the arrays contents with random values within the range min-->max (inclusive of min and max) This function should not return any value. For instance:
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:
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:
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:
Largest Even Number
Write a function called MaxEven that takes in two parameters:
an integer array
the size of the array
The function should look at the array and determine the maximum (highest) even number that is in the array. (You may assume that there is at least one even number in the array). The function should return the max even value it finds as an integer.
Note that none of the five functions you'll write above do any keyboard input or screen output. You're also not permitted to create any other arrays in your code besides the original one in main().
Part 2: Test your Functions with your main() function
To help you test and get you started, I've provided you with a STARTER FILE which you SHOULD use to start. Fill in your code into this starter file. The starter file already contains the PrintArray function that we looked at in lecture class. You can use this in your main function which will test your array functions, as well as anywhere else you need a PrintArray functionality.
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:
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.
Prompt the user the first time and after every user selection to enter their next menu selection with the following:
The first choice should just invoke the two already given function: PrintArray.
The Q option should exit the menu loop and allow the program to end. Make sure to 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):
F: The option F will call the FillArray function. You may assume this option is selected first by the user to populate the array with data before any other options are selected. When the user selects this option, prompt the user for the minimum random value and the maximum random value. You may assume they give legitimate minimum and maximum values. Then call your FillArray function passing in these user entered min and max values to fill up your array with data.
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
X: Call the MaxEven function and print out a message indicating whether that value was found or not based on the function's return value. Example:
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).
This is what my code looks like so far, so the code should elaborate on this. Function prototypes should go above the Main function.
This is a sample run of the function:
#include kiostream using namespace std; void Print Array const int arr[] const int size) Add your own function prototypes here int main We'll set the test size to 15 Use this constant in your calls instead of the literal 15 Then by changing this line, you can easily test arrays of different sizes. const int SIZE 15 Declare your array of size SIZE*/ Loop that presents user with menu options and calls appropriate Array functions*/ Note: you can assume the user will call Fill Array function first so that the array has data in it to begin return 0 Add in the definitions of your own 5 functions HERE Definition of PrintArray below DO NOT CHANGE Print Array Function //This function prints the contents of any interger array of any size seperated by commas void PrintArray const int arr[], const int size) cout InThe array: In for (int 0; i size-1; i for loop prints each item (not last due to comma handling cout arr[i] cout arrtsize-11 )In"; prints last item sans commanExplanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
char Menu();
void FillArray (int arr[], int size, int min, int max);
void Delete (int arr[], int size, int index);
void Reverse(int arr[], int size);
int Insert(int arr[], int size, int val, int index);
void PrintArray (const int arr[], const int size);
// MAIN FUNCTION
int main()
{
const int max_size = 6;
int size = 5;
int arr[size] = {};
srand(time(NULL));
char choice;
do {
choice = Menu();
switch( choice ) {
case 'F':
int min_range;
int max_range;
cout<< " Enter the minimum range of values: ";
cin >> min_range;
cout<<" Enter the maximum range of values: ";
cin>> max_range;
cout<<" Array is Filled! ";
FillArray(arr, size, min_range, max_range);
break;
case 'D':
int indx;
cout<<" Enter the index at which you want to delete the value: ";
cin>>indx;
if(indx < size)
Delete(arr, size, indx);
break;
case 'I':
int pos, val;
int new_size;
cout << " Enter the index at which you want to insert the new value: ";
cin >> pos;
if(pos>max_size-1)
cout<<" This is out of range";
else{
cout<<" Enter the value to be inserted: ";
cin>>val;
new_size = Insert(arr, size, val, pos);
size = new_size;
}
break;
case 'P':
cout<<" Printing the Array! ";
PrintArray(arr, size);
break;
case 'R':
cout<<" Reversing the Array! ";
Reverse(arr, size);
break;
case 'M':
Menu();
break;
}
}while(choice != 'Q');
cout<<" Thanks for using this Program!";
return 0;
}
char Menu(){
// system("cls");
cout<<" **Given Features** ";
cout<<"P Print the array contents ";
cout<<" **Function Tests** ";
cout<<"F Fill the Array with random contents ";
cout<<"I Insert ";
cout<<"D Delete ";
cout<<"R Reverse ";
cout<<"X Max Even Value ";
cout<<"M Print this Menu ";
cout<<"Q Quit this Program ";
cout<<"Enter a code : ";
char code = toupper(getche());
return code;
}
void FillArray (int arr[], int size, int min, int max){
int base = 0;
if (min < 0)
base = max - min;
else
base = max + min;
for(int i = 0; i < size; i++){
arr[i] = rand() % base + (min);
}
}
void Reverse(int arr[], int size)
{
int temp;
for (int i = 0; i < size/2; ++i)
{
temp = arr[i];
arr[i] = arr[size-i-1];
arr[size-i-1] = temp;
}
cout<<" New List is.. ";
PrintArray(arr, size);
}
void Delete (int arr[], int size, int index){
for(int i = index; i < size-1; i++)
arr[i] = arr[i+1];
arr[size-1] = 0;
cout << " The new List is : ";
PrintArray(arr, size);
}
int Insert(int arr[], int size, int val, int index){
index--;
for(int i = (size-1); i>=index; i--)
arr[i+1] = arr[i];
arr[index] = val;
size = size + 1;
cout << " The new List is : ";
PrintArray(arr, size);
return size;
}
void PrintArray (const int arr[], const int size){
cout << " The array: { ";
for(int i = 0; i < size-1; i++)
cout << arr[i] << ", ";
cout << arr[size-1] << " } ";
}
I apologise for not commenting and not be able to explain the code. Hope it helps.