I need to convert this program from Function Decomposition to OOP. If you don't have the time, I don't blame you. if you do, I owe you big time. If someone could explain to a dummy exactly what OOP is supposed to do even better.
I am a total noob. Any help would be appreciated. here is my program in functional decompostion:
Here is my pathetic attempt at trying to make it OOP. I am still trying to learn it. Took me long enough to get Function Decomposition.
I am a total noob. Any help would be appreciated. here is my program in functional decompostion:
Code:
#include <iostream> //Prototyping and global declarations
using namespace std ;
void display(float[], int) ;
int average(float, int, float[]) ;
void search(float[], int) ;
void sort(int, int, int, int, float[], int) ;
int largest(int, float[]) ;
int smallest(float[]) ;
void load(float[], int&) ;
void Get_Choice(int&) ;
void Process_Choice(int, int, int&, int, int, int, int, int) ;
//Purpose: This fucntion is used to inform the user of his or her choices
// and alllow them to slect a choice.
//Pre: Function is waiting for user input
//Post: Function processes users input and then waits for further commands
void Get_Choice(int& choice)
{
cout << endl ;
cout << endl ;
cout << "Welcome to Oliver's Number Array Program " << endl ;
cout << endl ;
cout << "Hello, Please choose one of the following choices" << endl ;
cout << endl ;
cout << "Choice 1 load the array from keyboard " << endl ;
cout << endl ;
cout << "Choice 2 display the array " << endl ;
cout << endl ;
cout << "Choice 3 find the average of the array " << endl ;
cout << endl ;
cout << "Choice 4 search the array " << endl ;
cout << endl ;
cout << "Choice 5 sort the array" << endl ;
cout << endl ;
cout << "Choice 6 find the largest value " << endl ;
cout << endl ;
cout << "Choice 7 find the smallest value " << endl ;
cout << endl ;
cout << "Choice 8 quit " << endl ;
cin >> choice ;
cout << endl ;
cout << endl ;
cout << endl ;
}
//Purpose: This function process the choice inputted from Get_Choice function
//Pre: Function is waiting for choice selection
//Post: Function process choice and calls the correct function and then waits
// for further user input.
void Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex)
{
switch(choice)
{
case 1: load(array, count);
break ;
case 2: display(array, count) ;
break ;
case 3: average(sum, count, array) ;
cout << endl ;
cout << "The average of the array is " << average(sum, count, array) << endl ;;
break ;
case 4: search(array, count) ;
break ;
case 5: sort(arrayLength, passCount, searchIndex, minIndex, array, count) ;
break ;
case 6: largest(count, array) ;
break ;
case 7: smallest(array) ;
break ;
}
}
//Purpose: This is the main where we call Get_Choice and Process_Choice
void main()
{
float array[50] ; //Declaration and instatiation of variables
int count = 0 ;
float sum = 0 ;
int arrayLength = 0 ;
int passCount = 0 ;
int searchIndex = 0 ;
int minIndex = 0 ;
int choice ;
do
{
Get_Choice(choice) ;
Process_Choice(choice, array, count, sum, arrayLength, passCount, searchIndex, minIndex) ;
}
while(choice != 8) ;
}
//Purpose: This function loads the values inputted into the array
//Pre: Waiting to be called to fill array
//Post: Array is filled with values
void load(float array[], int&count)
{
cout << endl ;
cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
while(cin)
{
cin >> array[count] ;
count++ ;
}
cin.clear() ;
count-- ;
}
//Purpose: This function is called to display the array
//Pre: Waiting to be called to read and display the array
//Post: Function reads and displays array
void display(float array[], int count)
{
cout << endl ;
cout << "The array will now be displayed to you " << endl ;
cout << "Index" << " " << "ArrayElement" << endl ;
for (int L = 0 ; L < count ; 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 compute average
//Post: Average of the array is calculated and sent back to main
int average(float sum, int count, float array[])
{
if (count > 0)
{
for (int L = 0 ; L < count ; L++)
sum = sum + array[L] ;
}
else
cout << "No Average" << endl ;
return sum / count ;
}
//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 array
void search(float array[], int count)
{
cout << endl ;
cout << "You will now be given an opportunity to search the Array" << endl ;
float target ;
int t = 0 ;
cout << "Enter a number to search for " ;
cin >> target ;
while(t < count && array[t] != target)
t++ ;
if (t != count)
cout << target << " is in the array " << endl ;
else
cout << target << " is not in the array" << endl ;
}
//Purpose: This is a selection sort used to sort the array in ascending order
//Pre: Waiting to be called to sort array in asecending order
//Post: Array is sorted in ascending order
void sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count)
{
cout << endl ;
cout << "The Array will now be sorted" << endl ;
float temp ;
arrayLength = count ;
for (passCount = 0 ; passCount < arrayLength ; passCount++)
{
minIndex = passCount ;
for (searchIndex = passCount + 1 ; searchIndex < arrayLength ; searchIndex++)
{
if (array[searchIndex] < (array[minIndex]))
minIndex = searchIndex ;
}
temp = array[minIndex] ;
array[minIndex] = array[passCount] ;
array[passCount] = temp ;
}
}
//Purpose: This function finds the largest value in a sorted array
//Pre: Waiting to be called to display the largest value in array
//Post: Largest value in array is found and displayed to user
int largest(int count, float array[])
{
cout << endl ;
cout << "The largest value in the array is" << endl ;
cout << array[count-1] << endl ;
return array[count-1] ;
}
//Purpose: This function inds the smallest value in a sorted array
//Pre: Waiting to be called to display the smallest value in array
//Post: Smallest value is found and displayed to user.
int smallest(float array[])
{
cout << endl ;
cout << "The smallest value in the array is" << endl ;
cout << array[0] << endl ;
return array[0] ;
}
Here is my pathetic attempt at trying to make it OOP. I am still trying to learn it. Took me long enough to get Function Decomposition.
Code:
#include <iostream> //Prototyping and global declarations
using namespace std ;
enum RelationType {LESS, EQUAL, GREATER} ;
class Numbers
{
public:
void display(float array[], int count) ;
int average(float sum, int count, float array[]) ;
void search(float array[], int count) ;
void sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count) ;
int largest(int count, float array[]) ;
int smallest(float array[]) ;
void load(float array[], int& count) ;
void Get_Choice(int& choice);
void Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex) ;
private:
float array[50] ;
int count ;
} ;
Code:
#include <iostream> //Prototyping and global declarations
#include "2num.h"
using namespace std ;
//Purpose: This fucntion is used to inform the user of his or her choices
// and alllow them to slect a choice.
//Pre: Function is waiting for user input
//Post: Function processes users input and then waits for further commands
void Numbers::Get_Choice(int& choice)
{
cout << endl ;
cout << endl ;
cout << "Welcome to Oliver's Number Array Program " << endl ;
cout << endl ;
cout << "Hello, Please choose one of the following choices" << endl ;
cout << endl ;
cout << "Choice 1 load the array from keyboard " << endl ;
cout << endl ;
cout << "Choice 2 display the array " << endl ;
cout << endl ;
cout << "Choice 3 find the average of the array " << endl ;
cout << endl ;
cout << "Choice 4 search the array " << endl ;
cout << endl ;
cout << "Choice 5 sort the array" << endl ;
cout << endl ;
cout << "Choice 6 find the largest value " << endl ;
cout << endl ;
cout << "Choice 7 find the smallest value " << endl ;
cout << endl ;
cout << "Choice 8 quit " << endl ;
cin >> choice ;
cout << endl ;
cout << endl ;
cout << endl ;
}
//Purpose: This function process the choice inputted from Get_Choice function
//Pre: Function is waiting for choice selection
//Post: Function process choice and calls the correct function and then waits
// for further user input.
void Numbers::Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex)
{
switch(choice)
{
case 1: load(array, count);
break ;
case 2: display(array, count) ;
break ;
case 3: average(sum, count, array) ;
cout << endl ;
cout << "The average of the array is " << average(sum, count, array) << endl ;;
break ;
case 4: search(array, count) ;
break ;
case 5: sort(arrayLength, passCount, searchIndex, minIndex, array, count) ;
break ;
case 6: largest(count, array) ;
break ;
case 7: smallest(array) ;
break ;
}
}
//Purpose: This is the main where we call Get_Choice and Process_Choice
void main()
{
float array[50] ; //Declaration and instatiation of variables
int count = 0 ;
float sum = 0 ;
int arrayLength = 0 ;
int passCount = 0 ;
int searchIndex = 0 ;
int minIndex = 0 ;
int choice ;
do
{
Numbers::Get_Choice(choice) ;
Numbers::Process_Choice(choice, array, count, sum, arrayLength, passCount, searchIndex, minIndex) ;
}
while(choice != 8) ;
}
//Purpose: This function loads the values inputted into the array
//Pre: Waiting to be called to fill array
//Post: Array is filled with values
void Numbers::load(float array[], int&count)
{
cout << endl ;
cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
while(cin)
{
cin >> array[count] ;
count++ ;
}
cin.clear() ;
count-- ;
}
//Purpose: This function is called to display the array
//Pre: Waiting to be called to read and display the array
//Post: Function reads and displays array
void Numbers::display(float array[], int count)
{
cout << endl ;
cout << "The array will now be displayed to you " << endl ;
cout << "Index" << " " << "ArrayElement" << endl ;
for (int L = 0 ; L < count ; 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 compute average
//Post: Average of the array is calculated and sent back to main
int Numbers::average(float sum, int count, float array[])
{
if (count > 0)
{
for (int L = 0 ; L < count ; L++)
sum = sum + array[L] ;
}
else
cout << "No Average" << endl ;
return sum / count ;
}
//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 array
void Numbers::search(float array[], int count)
{
cout << endl ;
cout << "You will now be given an opportunity to search the Array" << endl ;
float target ;
int t = 0 ;
cout << "Enter a number to search for " ;
cin >> target ;
while(t < count && array[t] != target)
t++ ;
if (t != count)
cout << target << " is in the array " << endl ;
else
cout << target << " is not in the array" << endl ;
}
//Purpose: This is a selection sort used to sort the array in ascending order
//Pre: Waiting to be called to sort array in asecending order
//Post: Array is sorted in ascending order
void Numbers::sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count)
{
cout << endl ;
cout << "The Array will now be sorted" << endl ;
float temp ;
arrayLength = count ;
for (passCount = 0 ; passCount < arrayLength ; passCount++)
{
minIndex = passCount ;
for (searchIndex = passCount + 1 ; searchIndex < arrayLength ; searchIndex++)
{
if (array[searchIndex] < (array[minIndex]))
minIndex = searchIndex ;
}
temp = array[minIndex] ;
array[minIndex] = array[passCount] ;
array[passCount] = temp ;
}
}
//Purpose: This function finds the largest value in a sorted array
//Pre: Waiting to be called to display the largest value in array
//Post: Largest value in array is found and displayed to user
int Numbers::largest(int count, float array[])
{
cout << endl ;
cout << "The largest value in the array is" << endl ;
cout << array[count-1] << endl ;
return array[count-1] ;
}
//Purpose: This function inds the smallest value in a sorted array
//Pre: Waiting to be called to display the smallest value in array
//Post: Smallest value is found and displayed to user.
int Numbers::smallest(float array[])
{
cout << endl ;
cout << "The smallest value in the array is" << endl ;
cout << array[0] << endl ;
return array[0] ;
}