Write a program driver that uses the class Statistics to gather statistical data
ID: 3803128 • Letter: W
Question
Write a program driver that uses the class Statistics to gather statistical data on a set of random numbers between 0 and max using dynamic arrays. The main program should ask the user how many numbers should be in the set and the value of max. Using the class, the program should display the random numbers, then calculate and display the average and median of the set. (See getRandomNumbers() p.650 for an example on random numbers.) You will design and implement the class Statistics that gathers statistical data on an array of integers. An instance of your class will dynamically allocate and deallocate an array of random integers between 0 and max. The class must include the data member numbers that is a pointer to an array of integers (dynamic array)and the data member length that holds the number of elements in the dynamic array. Your class must include the appropriate constructors, destructor, and the following member functions: displayNumbers displays the numbers in the array getAverage returns the average of the numbers in the array getMedian returns the median value of the array. See Chapter 10 programming challenge 6 for a discussion of the term median. Taking the median will require a sorted array. You will need to dynamically allocate a temporary array to hold the sorted values. Do not change the contents of the numbers array. Keep in mind you will be implementing member functions and that your numbers array is a data member. To successfully complete your assignment work on it in two parts: Part I begin by using static arrays in your class. Get your average and median functions working properly. Do not submit this as your solution. See class example Statistics_staticarrayclass.cpp Part II next modify your class to use dynamic arrays. Submit this as your solution. Hint: See class Squares p.657 for an example. Run the program with the following data sets: array length = 100, max value = 99 array length = 9, max value = 99
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Statistics {
public :
int *pointer = NULL;
public :
Statistics(){
}
~Statistics(){
delete []pointer;
}
void displayName(int dataLength){
cout << " Item you have entered are " << endl;
for(int indx = 0; indx < dataLength ; indx++){
cout << "item is " << *(pointer + indx) << endl;
}
}
int getAverage(int dataLength){
int average = 0;
for(int indx = 0; indx < dataLength ; indx++){
average = average + *(pointer + indx);
}
return average/dataLength;
}
int getMedian(int dataLength){
double median = 0;
int temp[dataLength];
for(int indx = 0; indx < dataLength ; indx++){
temp[indx] = *(pointer + indx);
}
// Sorting of array
for (int i = 0; i < dataLength - 1; i++) {
int min = temp[i];
int minIndex = i;
for (int j = i + 1; j < dataLength; j++) {
if (min > temp[j]) {
min = temp[j];
minIndex = j;
}
}
if (minIndex != i) {
temp[minIndex] = temp[i];
temp[i] = min;
}
}
for(int indx = 0; indx < dataLength ; indx++){
cout << temp[indx] << endl;
}
int medianPoint = 0;
if(dataLength%2 == 0){
medianPoint = dataLength/2;
median = temp[medianPoint];
}else{
medianPoint = dataLength/2;
median = (temp[medianPoint] + temp[medianPoint+1])/2;
}
return median;
}
};
int main()
{
int input = -1;
int max = -1 ;
Statistics stats;
cout << " Enter the value that how many numbers should be in the set " << endl;
cin >> input;
stats.pointer = new int[input];
int temp;
cout << "Enter max value " << endl;
cin >> max;
for(int indx = 0; indx < input ; indx++){
temp = rand()%max;
*(stats.pointer + indx) = temp;
}
stats.displayName(input);
double average = stats.getAverage(input);
cout << "Average : " << average << endl;
double median = stats.getMedian(input);
cout << " Median : " << median << endl;
return 0;
}
Output
-----------
Enter the value that how many numbers should be in the set
4
Enter max value
89
Item you have entered are
item is 37
item is 33
item is 41
item is 28
Average : 34
28
33
37
41
Median : 37
Description
----------------
As mentioned three method are introduced in Statistics class
1. displayName()
2. getAverage()
3. getMedian()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.