#include <iostream> //This is our prototyping and global
using namespace std ; //declarations
void display(int[], int) ;
int average(int, int, int[]) ;
void search(int[], int) ;
void sort(int, int, int, int, int[], int) ;
int largest(int, int[]) ;
int smallest(int[]) ;
int main() //This is the main function where all
{ //other functions are called to.
int array[50] ;
int actsize = 0 ; //Declartion and instatiation of
int sum = 0 ; //variables
int arrayLength = 0 ;
int passactsize = 0 ;
int searchIndex = 0 ;
int minIndex = 0 ;
int choice ;
cout << "Please choose one of the following" << endl ; //These are the Menu
//options
cout << "Option 1 input from keyboard" << endl ;
cout << "Option 2 quit " << endl ;
cin >> choice;
if(choice = 1) //If the user choses choice 1 we enter the if
{ //statement
switch(choice)
{case 1:
cout << "Welcome to Oliver's Number Array Program" << endl ; //Welcoming statements
cout << "Please follow the directions carefully" << endl ;
cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
//Prompting user for keyboard input
while(cin) //Takes the input and puts into an array
{
cin >> array[actsize] ;
actsize++ ;
}
cin.clear() ;
actsize--; //Subtract element so that end of file is not displayed in array.
cout << "The Array will now be displayed to you" << endl ; //Array will now be displayed
display(array, actsize) ; //Display array Function
cout << "The average of the Array will now be computed" << endl ;
cout << "The average is "; //Average of array will be found
if (actsize > 0)
cout << average(sum, actsize, array) << endl ; //Calls the average function
else
cout << "zero" << endl ; //Output if there is no average
cout << "You will now be given an opportunity to search the Array" << endl ;
//User is told they may search the array
search(array, actsize) ; //Calls search function
cout << "The Array will now be sorted" << endl ; //User is told the array will be sorted
//using a selection sort from the book
sort(arrayLength, passactsize, searchIndex, minIndex, array, actsize) ; //Calls the sort function
cout << endl; //Some space to make it look nicer
cout << endl;
cout << endl;
cout << endl;
cout << "The Array will now be displayed to you" << endl ; //Tells user that Array will be displayed
display(array, actsize) ; //Calling display function
cout << "The largest value in the array is" << endl ; //Tells user that the largest value will be
//Displayed
largest(actsize, array) ; //Calling largest function
cout << array[actsize-1] << endl ;
cout << "The smallest value in the array is" << endl ; //Tells user that the largest value will be
//displayed
smallest(array) ; //Calling smallest function
cout << array[0] << endl ;
}}
else
cout << endl; //if choice 2, then the main is not entered
}
//Purpose: This function is used to dispaly the array to the user
//Pre: Waiting to be called to read the array
//Post: Function reads and displays the array
void display(int array[], int actsize)
{
cout << "Index" << " " << "Arrayelement" << endl ;
for (int L = 0 ; L < actsize ; L++)
cout << L << "." << " " << array[L] << endl ;
}
//Purpose: This function calculates the average of the array
//Pre: Waiting to be called to read values from array and take average
//Post: Average of the array is calculated and sent back to the main
int average(int sum, int actsize, int array[])
{
sum = 0 ;
for (int L = 0 ; L < actsize ; L++)
sum = sum + array[L] ;
return sum / actsize ;
}
//Purpose: To give the user an opportunity to search the array
//Pre: Waiting to be called to read and compare values of the array
//Post: Array is searched and values are compared to see if the target is in the Array
void search(int array[], int actsize)
{
float target ;
int t = 0 ;
cout << "Enter a number to search for ";
cin >> target ;
while(t < actsize && array[t] != target)
t++ ;
if (t != actsize)
cout << target << "is in the array" << endl ;
else
cout << target << "is not in the array" << endl ;
}
//Purpose: This is a selection sort from the book to sort the array in ascending order
//Pre: Waiting to be called to sort the array in ascending order
//Post: Array is now sorted in ascending order
void sort(int arrayLength , int passactsize, int searchIndex, int minIndex, int array[], int actsize)
{
int temp ;
arrayLength = actsize ;
for (passactsize = 0 ; passactsize < arrayLength ; passactsize++)
{
minIndex = passactsize ;
for (searchIndex = passactsize +1 ; searchIndex < arrayLength; searchIndex++)
{
if (array[searchIndex] < (array[minIndex]))
minIndex = searchIndex ;
}
temp = array[minIndex] ;
array[minIndex] = array[passactsize] ;
array[passactsize] = temp ;
}
}
//Purpose: Find the largest value in a sorted array
//Pre: Waiting to be called to display the largest value in array
//Post: The largest value is found and sent back to main
int largest(int actsize, int array[])
{
return array[actsize-1] ;
}
//Purpose: Find the smallest value in a sorted array
//Pre: Waiting to be called to display the smallest value in array
//Post: The smallest value is found and sent back to main
int smallest(int array[])
{
return array[0] ;
}