1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
/* prototype for template functions (global) *****************/
// STUDENT CODE BEGINS
template < class T >
void showValues( T values[], int size );
template < class T >
void doubleArray( T values[], int size );
template < class T >
void sortArray( T values[], int size );
// STUDENT CODE ENDS (template prototypes)
/* application entry point ***********************************/
int main()
{
/* declarations section ************************************/
// Function prototypes (other than templates)
void showValues( int [], int );
void doubleArray( int [], int );
void sortArray( int [], int );
// declare arrays and data here
// use a size declarator
// STUDENT CODE BEGINS
const int SIZE = 7;
int values[SIZE] = {6,2,3,5,9,2,1};
unsigned short int uvalues[SIZE] = {250,100,34000,5000,298,12,25123};
long double lvalues[SIZE] = {1.432,12.5432,1.422,3.1234,3.111,4.3321,5};
// STUDENT CODE ENDS
string programLoop = "Y";
int menuChoice;
/* statements section **************************************/
do
{
// menu
cout << "0 - exit\n";
cout << "Enter your choice: ";
cin >> menuChoice;
// do it! - expand 'switch' as you complete each method
// STUDENT CODE BEGINS
switch (menuChoice)
{
case 0:
programLoop = "N";
break;
case 1:
showValues( values, SIZE ); // show int array values
break;
case 2:
showValues( uvalues, SIZE ); // show unsigned short int values
break;
case 3:
showValues( lvalues, SIZE ); // show long double values
break;
case 4:
doubleArray( values, SIZE ); // double the int array values
break;
case 5:
doubleArray( uvalues, SIZE ); // double the unsigned short int values
break;
case 6:
doubleArray( lvalues, SIZE ); // double the long double values
break;
case 7:
sortArray( values, SIZE ); // sort int values
break;
case 8:
sortArray( uvalues, SIZE ); // sort unsigned short values
break;
case 9:
sortArray( lvalues, SIZE ); // sort long double values
break;
default:
cout << "Invalid selection - try again\n\n";
}
// STUDENT CODE ENDS
} while (programLoop == "Y");
/* termination section *************************************/
cin.get();
return 0;
}
/**************************************************************
Function definition: doubleArray
Parameter(s): numbers[] : T, size : int
Returns: void
This function doubles the value of each element
in the array passed into nums. The value passed
into size is the number of elements in the array.
**************************************************************/
// STUDENT CODE BEGINS
template < class T >
void doubleArray( T values[], int SIZE)
{ for (int index = 0; index < SIZE; index++)
{
values[index] *= 2;
}
}
// STUDENT CODE ENDS
/**************************************************************
Function definition: showValues
Parameter(s): numbers[] : T, size : int
Returns: void
This function accepts an array of integers and
the array's size as its arguments. The contents
of the array are displayed.
**************************************************************/
// STUDENT CODE BEGINS
template < class T >
void showValues( T values[], int SIZE )
{
for ( int index = 0; index < SIZE; index++)
{
cout << right << setw(10) << values[index];
}
cout << endl;
}
// STUDENT CODE ENDS
/**************************************************************
Function definition: sortArray
Parameter(s): numbers[] : T, size : int
Returns: void
Sort type: selection sort
Order: ascending or descending, your choice
**************************************************************/
// STUDENT CODE BEGINS
template < class T >
void sortArray( T values[], int SIZE )
{
/***Local Identifiers***/
bool swap; // true = swap took place
T temp; // holds value for swapping
do
{
swap = false;
for ( int index = 0; index < ( SIZE - 1 ); index++ )
{
if ( values[index] > values[index + 1] )
{
temp = values[index];
values[index] = values[index + 1];
values[index + 1] = temp;
swap = true;
}
}
} while (swap);
}
// CODE ENDS
|