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
|
/*Adam Spaay
CIS 330 C++ Programming
Median Function
4/5/2012*/
// Write a function that accepts as arguements the following: A) an array of intergers
// and B) an interger that indicates the number of elements in the array.
// Modify the program to allow the used to input up to 10 intergers and the used input in an array.
// Validation is not required.
// If the used inputs -1 exit the input loop.
// Find the median value after the user has input their interger values.
// Display all intergers input by the user and the median value.
// Demonstrate "pointer prowess"
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//function prototypes:
void arrayDisplay(int*, int, int*);
void clearBoard(int*, int&, int);
void medianDisplay(int*, int);
int main()
{
//Local variables
const int ARRAYSIZE = 10;
int *input;
int *pointer;
int userInput = 0;
int incrCounter = 0;
int displayCounter = 0;
input = new int[ARRAYSIZE];
//Set pointer equal to array
pointer = input;
pointer+=incrCounter;
do
{
if (incrCounter < ARRAYSIZE)
{
cout << "Enter a number into the array, or enter -1 to quit." <<incrCounter + 1 << "of" << ARRAYSIZE;
cin >> userInput;
if (incrCounter < 10)
{
*pointer = userInput;
incrCounter++;
pointer++;
}
else
{
cin.ignore();
cout << "The array cannot hold more than 10 values.";
cin.get();
}
}
} while (userInput != -1);
//Displaying the inputs
input = pointer;
arrayDisplay(input, incrCounter, pointer);
cin.ignore();
cin.get();
//Displaying the median
medianDisplay(input, incrCounter);
//Clearing the array
clearBoard(input, incrCounter, ARRAYSIZE);
cin.ignore();
cout << "The array has been cleared.";
cin.get();
return 0;
}
void arrayDisplay(int input[], int incrCounter, int *pointer)
{
int displayCounter = 0;
pointer = input;
cout << "The number of elements currently in the array is: "<< incrCounter;
for (displayCounter = 0; displayCounter < incrCounter; displayCounter++)
{
cout << pointer << " ";
pointer++;
}
return;
}
void medianDisplay (int input[], int incrCounter)
{
double middleInt;
double arrayAvg;
double determinateDecimal;
int medianInt;
middleInt = incrCounter / 2;
determinateDecimal = middleInt- static_cast <int> (middleInt);
medianInt = static_cast<int>(middleInt);
if (determinateDecimal > 0)
{
cout << "The median is " << input[medianInt] << endl;
}
else
{
arrayAvg = (input[medianInt] + input[medianInt + 1]) / 2.0;
cout << "The median is " << arrayAvg << endl;
}
return;
}
void clearBoard(int input[], int &incrCounter, int ARRAYSIZE)
{
delete [] input;
input = 0;
incrCounter = 0;
input = new int[ARRAYSIZE];
return;
}
|