I had a midterm problem similar to this last year.
The question was
3) Write a function using the following structure and prototype.
struct stat
{
float avg;
float median;
float *mode;
int nModes;
};
stat *avgMedMode(int *,int);
The function takes in an integer array and the size of the array.
Then returns a pointer to a structure containing the average, median
and mode. I will input a small array to test this out so ask for
how many inputs to fill the array, then the values to place into the
array. Make sure you delete the dynamic array creation for the mode
when you exit the problem.
My solution (as a module of the entire midterm program) is as follows:
// Frank C. Jamison
// April 08, 2006
// CIS 17A - Section 43052
// Borland C++ Builder Enterprise Suite Version 6.0
// Purpose: Midterm Examination Program 3
// Midterm Examination
#include <iostream>
using namespace std;
// Structure Declarations - Midterm Program 3
struct stats {
float avg;
float median;
int *mode;
int nmodes;
};
// Function Declarations - Midterm Program 3
stats *avgMedMode(int *, int);
int *createArray(int);
void displayStats(stats);
int findHighFrequency(int*, int);
int findNumModes(int *, int, int);
float getArrayAverage(int *, int);
void getArrayElements(int *, int);
float getArrayMedian(int *, int);
int getModeArray(int *, int, int *, int);
void programHeader3();
void sortArrayElements(int *, int);
// Function Declarations - Multiple Files
void clear();
bool validLoNum(int, int);
void wait();
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 *
//*****************************************************************************
void problem3() {
// Variable Definition
int numElements;
// Display program header
programHeader3();
// Get and validate number of integers to calculate
do {
cout << "\n\tNumber of integers: ";
cin >> numElements;
} while (!validLoNum(numElements, 1));
// Create dynamic array to hold integers
int *intArray = createArray(numElements);
// Get integer array elements
getArrayElements(intArray, numElements);
// Dynamically allocate memory for stats structure and set structure
// variables
stats *arrayStats = avgMedMode(intArray, numElements);
// Display Integer Array Statistics
displayStats(*arrayStats);
// Free dynamically allocated memory
delete [] arrayStats->mode;
delete [] intArray;
delete arrayStats;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Create Structure and Set Variables *
//*****************************************************************************
stats *avgMedMode(int *arrayPtr, int numElements) {
// Variable Definition
int highFrequency;
// Dymanically create stats structure
stats *statStruct = new stats;
// Sort survey results in ascending order
sortArrayElements(arrayPtr, numElements);
// Find highest integer frequency
highFrequency = findHighFrequency(arrayPtr, numElements);
// Find the number of modes in the array
statStruct->nmodes = findNumModes(arrayPtr, numElements, highFrequency);
// Dynamically create new array to hold mode of integer array
int *modeArray = createArray(statStruct->nmodes);
// Add modes to mode array
*modeArray = getModeArray(modeArray, highFrequency, arrayPtr, numElements);
// Set structure pointer to mode array
statStruct->mode = modeArray;
// Find the median value of the integer array
statStruct->median = getArrayMedian(arrayPtr, numElements);
// Find the average value of the integer array
statStruct->avg = getArrayAverage(arrayPtr, numElements);
return statStruct;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Dynamically Allocate Memory for Integer *
// Array *
//*****************************************************************************
int *createArray(int numElements) {
// Dynamically allocate memory for integer array
int *array = new int[numElements];
// Return pointer to dynamically allocated memory for integer array
return array;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Display Integer Array Statistics *
//*****************************************************************************
void displayStats(stats arrayStruct) {
// Display program header
programHeader3();
// Display integer array statistics
cout << "\n\tInteger Array Statistics\n";
cout << "\n\tArray Average: " << arrayStruct.avg << endl;
cout << "\n\tArray Median: " << arrayStruct.median << endl;
// If the array mode is set to -1, report that there is no mode
if (*arrayStruct.mode == -1)
cout << "\n\tArray Mode: None";
// If the array mode is not set to -1, display the array mode
else {
cout << "\n\tArray Mode: ";
for (int i = 0; i < arrayStruct.nmodes; i++)
cout << arrayStruct.mode[i] << " ";
}
// Wait for user to hit [Enter] to continue
wait();
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Find Highest Integer Frequency *
//*****************************************************************************
int findHighFrequency(int* arrayPtr, int numElements) {
// Local Variable Definitions
int currentHigh = 1,
frequency = 1,
highFrequency = 1;
// Check the frequency of each element in the array
for (int i=1; i < numElements; i++) {
// If the current element has the same value as previous element,
// add 1 to frequency
if (arrayPtr[i] == arrayPtr[i - 1])
frequency += 1;
// If the current element has a different value than the previous,
// set the value of currentHigh to frequency and reset frequency to 1
else {
currentHigh = frequency;
frequency = 1;
// If value of currentHigh is greater highFrequency, set value of
// highFrequency to currentHigh
if (currentHigh > highFrequency)
highFrequency = currentHigh;
}
// If all elements in the array have the same value, set value of
// highFrequency to frequency
if (frequency > highFrequency)
highFrequency = frequency;
}
// Return the highest integer frequency
return highFrequency;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Find Number of Modes in Integer Array * *
//*****************************************************************************
int findNumModes(int *arrayPtr, int numElements, int highFrequency) {
// Variable Definitions
int frequency = 1,
numModes = 0;
// Check the frequency of each element in the array
for (int i=1; i < numElements; i++) {
// If the current element has same value as previous element,
// add 1 to frequency
if (arrayPtr[i] == arrayPtr[i-1]) {
frequency += 1;
// If frequency has the same value as highFrequency, add 1 to
// numModes
if (frequency == highFrequency)
numModes += 1;
}
// If current element has a different value than the previous
// element, reset frequency to 1
else
frequency = 1;
}
// If there is only one element in the array, set numModes to 1
if (numModes == 0)
numModes = 1;
// Return the number of modes in the array
return numModes;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Get the Average Value of the Integer Array * *
//*****************************************************************************
float getArrayAverage(int *arrayPtr, int numElements) {
// Local Variable Definitions
float average = 0.0;
// Get the sum of all array elements
for (int i = 0; i < numElements; i++)
average += arrayPtr[i];
// Divide the sum of the array elements by the number of array elements
average = average / numElements;
// Return the average value of the integer array
return average;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Get Elements for Integer Array Pointed To *
//*****************************************************************************
void getArrayElements(int *arrayPtr, int numElememts) {
// Display program header
programHeader3();
// Get and validate integer for each array element
for (int i = 0; i < numElememts; i++) {
do {
cout << "\n\tEnter an integer for Element " << (i + 1) << ": ";
cin >> arrayPtr[i];
} while (!validLoNum(arrayPtr[i], 0));
}
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Get the Median Value of the Integer Array * *
//*****************************************************************************
float getArrayMedian(int* arrayPtr, int numElements) {
// Variable Definitions
int middle,
midpoint1,
midpoint2;
float median;
// If there are an even number of elements, calculate the average of
// the two middle elements
if (numElements %2 == 0) {
midpoint1 = (numElements / 2);
midpoint2 = midpoint1 - 1;
median = (arrayPtr[midpoint1] + arrayPtr[midpoint2]) / 2.0;
}
// If there is an odd number of elements, get the middle element
else {
middle = (numElements / 2);
median = arrayPtr[middle];
}
// Return the median value
return median;
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Get the Modes of the Integer Array *
//*****************************************************************************
int getModeArray(int *modeArrayPtr, int highFrequency, int* arrayPtr,
int numElements) {
// Variable Definitions
int frequency = 1,
modeElement = 0;
// If the integer array has no mode, return -1
if (highFrequency == 1)
return -1;
// If the integer array has a mode, add mode values to the mode array
else {
// Get mode elements from the integer array
for (int i=1; i < numElements; i++) {
// If the value in the current array element is the same as the
// previous element, add 1 to frequency
if (arrayPtr[i] == arrayPtr[i-1]) {
frequency += 1;
// If the current frequency is the same as the high frequency,
// add the value in the current element to the mode array
if (frequency == highFrequency) {
modeArrayPtr[modeElement] = arrayPtr[i];
modeElement += 1;
}
}
// If the value in the current array element is not the same as
// the previous element, reset the frequency counter to 1
else
frequency = 1;
}
// Return the mode array
return *modeArrayPtr;
}
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Display Program Header *
//*****************************************************************************
void programHeader3() {
// Clear the screen
system("cls");
// Display the program screen header
cout << "\n\n\tMIDTERM PROGRAM 3 - Average, Median, and Mode\n\n";
}
//*****************************************************************************
// FUNCTION: MIDTERM PROBLEM 3 - Sort Elements of Integer Array Pointed To in *
// Ascending Order *
//*****************************************************************************
void sortArrayElements(int *arrayPtr, int numElememts) {
// Local Variable Definitions
int minIndex,
minValue,
startScan;
// Sort array elements in ascending order
for (startScan = 0; startScan < (numElememts - 1); startScan++) {
// Initialize variables
minIndex = startScan;
minValue = arrayPtr[startScan];
// Check next element in the array for a smaller number
for (int index = (startScan + 1); index < numElememts; index++) {
// If the next element is smaller, store its value and index
// number in minValue and minIndex variables
if (arrayPtr[index] < minValue) {
minValue = arrayPtr[index];
minIndex = index;
}
}
// Copy current element to next element and replace current element
// with stored smaller value
arrayPtr[minIndex] = arrayPtr[startScan];
arrayPtr[startScan] = minValue;
}
}