Objectives Review Void Value Returning Functions Review Passing Parameters Functions Refer Q43817823
Objectives:
- Review void and value returningfunctions.
- Review passing parameters to functions by referenceand by value.
- Review passing arrays to functions.
- Review iterating through an array to output its elements.
- Learning to search an array for a specified item.
- Understanding and implementing the SelectionSort algorithm.
Instructions:
The main function, showMenu function, and loadArr function arewritten for you. Please don’t changethem. Reading the code, you’ll notice the followingfunctions and TODO comments. Please implement thefunctions according to the TODO comments written in the givencode.
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <cmath>
using namespace std;
const int SIZE = 10;
// Function Prototypes
void showMenu();
void loadArr(int arr[], int size);
void fillArr(int arr[], int size, int fillValue);
void printArr(int arr[], int size);
void selectionSort(int arr[], int size);
bool linearSearch(int arr[], int size, int searchTerm);
int getMin(int arr[], int size);
int getMax(int arr[], int size);
int getSum(int arr[], int size);
void getRangeAndAverage(int arr[], int size, int &range, double&average);
int main()
{
int arr[SIZE];
int userMenuChoice;
int searchTerm;
int fillValue;
int range = 0;
double average = 0.0;
srand(static_cast<unsignedint>(time(NULL)));
loadArr(arr, SIZE);
while (true)
{
showMenu();
cin >> userMenuChoice;
switch (userMenuChoice)
{
case 1:
loadArr(arr,SIZE);
break;
case 2:
cout <<“Please enter a single value to fill every array position: “;
cin >>fillValue;
fillArr(arr,SIZE, fillValue);
break;
case 3:
printArr(arr,SIZE);
break;
case 4:
selectionSort(arr, SIZE);
break;
case 5:
cout <<“Please enter an integer to search for: “;
cin >>searchTerm;
if(linearSearch(arr, SIZE, searchTerm))
cout << searchTerm << ” wasfound!n”;
else
cout << searchTerm << ” was NOTfound!n”;
break;
case 6:
cout <<“The minimum array elements is: “
<< getMin(arr, SIZE) << endl;
break;
case 7:
cout <<“The maximum array elements is: “
<< getMax(arr, SIZE) << endl;
break;
case 8:
cout <<“The sum of the current array elements is: “
<< getSum(arr, SIZE) << endl;
break;
case 9:
getRangeAndAverage(arr, SIZE, range, average);
cout <<“The range of the values in the array is: ” << range <<“.n”;
cout <<“The average of the values in the array is ” << average<< “.n”;
break;
case -1:
cout <<“Thank you for using our CSC 2111 program.n”;
return 0;
default:
cout <<userMenuChoice << ” is not a menu choice. Please tryagain.nn”;
}
}
}
// Function do display the menu
void showMenu()
{
cout << endl;
cout << ” 1. Load New Random Values” <<endl;
cout << ” 2. Fill All Positions with a SingleValue” << endl;
cout << ” 3. Print the Array” <<endl;
cout << ” 4. Sort the Array in Ascending Order”<< endl;
cout << ” 5. Search the Array” <<endl;
cout << ” 6. Find the Minimum Value” <<endl;
cout << ” 7. Find the Maximum Value” <<endl;
cout << ” 8. Calculate the Sum” <<endl;
cout << ” 9. Calculate the Range and Average”<< endl;
cout << “-1. Exit the Program” << endl<< endl;
cout << “Please make your selection: “<< endl;
}
// Function to load the array with random numbers between 1 and100
void loadArr(int arr[], int size)
{
for (int i = 0; i < size; i++)
arr[i] = (rand() % 100) + 1;
}
// Function to fill the array with a value passed in theparameter
void fillArr(int arr[], int size, int fillValue)
{
// TODO: Loop through the array and set each elementwith
// the value fillValue.
}
// Function to print each element of the array
void printArr(int arr[], int size)
{
// TODO: Loop through the array and output eachelement
// to screen.
}
// Function to sort the array using selection sort
void selectionSort(int arr[], int size)
{
// TODO: Sort the values in the array using theselection
// sort algorithm. Feel free to implement thisfunction
// using the example code in the slides.
}
// Function to search the array using a search term passed inthe parameter
bool linearSearch(int arr[], int size, int searchTerm)
{
// TODO: Loop through the array checking each value tosee
// whether it matches the searchTerm passed in theparameter.
// This is a bool returning function, so if a match isfound,
// return true. If no match is found after checkingevery
// element, return false.
}
// Function to return the array’s minimum value
int getMin(int arr[], int size)
{
// TODO: Loop through the array and determine andRETURN
// the minimum value. Feel free to use theexample
// code from the slides when implementing thisfunction.
}
// Function to return the array’s maximum value
int getMax(int arr[], int size)
{
// TODO: Loop through the array and determine andRETURN
// the maximum value. This function is very similarto
// the getMin function (except we’re returning themaximum
// value instead of the minimum value).
}
// Function to return the sum of all the array’s values
int getSum(int arr[], int size)
{
// TODO: Loop through the array and calculate thesum
// of all the elements. After the loop, RETURNthat
// sum.
}
// Function to place the array’s range and average value bysetting
// the parameters passed by reference
void getRangeAndAverage(int arr[], int size, int &range, double&average)
{
// This function performs two tasks. It calculates therange of all
// the values in the array, and it calculates theaverage of all the
// values in the array.
// Because we’re interested in “returning” twovalues to the calling
// function, we aren’t using a return statement.Instead, we’re
// using references passes. Notice the & symbolbefore the parameter
// name. Because we’re using reference passes, we canchange the
// value of those parameters here in the function, andthose changes will
// be reflected in main().
// TODO: Set the range. The range is calculated asthe difference between
// the highest value and the lowest value. Feel freeto use the getMax and
// getMin functions that you’ve alreadyimplemented.
// TODO: Set the average. The average is calculatedas the sum of all the
// values (feel free to use getSum) divided by thenumber of values in the
// list. When performing a division operation, it’simportant to check that
// we’re not attempting to divide by zero.
}
Expert Answer
Answer to Objectives: Review void and value returning functions. Review passing parameters to functions by reference and by value….
OR