Question: Make sure and use three separate files for your object oriented programs (two .cpp files and 1 .h file) Do not use inline functions NO
Make sure and use three separate files for your object oriented programs (two .cpp files and 1 .h file)
Do not use inline functions
NO GLOBAL VARIABLES(global constants are fine)
So you need to take this program and extend the class named
Stats. Add a struct called All that will have an average, max , and min double values.
It should have added to the Stats class:
?
A readValuesFromFile function that takes a filename as a parameter, opens the file for input, zeros the array, and reads all the values the file has into the array
?
A getAll function that makes a temporary variable, temp , of type All , sets its average to getAvg, max to getLargest , and min to getSmallest and returns temp(not as a reference parameter but a return type)
Have another function that is not part of the class called:
A writeStatsToFile function that takes a filename as a parameter and an All parameter, opens the file for output, and sends statistics report to the file using the All
parameter
Extend the class in Stats.h and put the All struct in Stats.h also. The class implementation file (.cpp) will contain the function definitions and the additions (dont forget to include the Stats.h file). Name the class implementation file Stats.cpp
Next create a program that uses the Stats class to read rainfall data from a file ("rainfall.dat" is a good name) and report annual rainfall statistics.
An example of "rainfall.dat" file is:
12.4
15.9
19.2
23
5.3
1.2
0.2
3.7
7.3
9
11.1
14.6
The program should first create a Stats object named rainfall and call its readValuesFromFile member function to set each of the 12 monthly rainfall totals from the file. It should then use the getAll function to set variable values of All data type, then print the statistics using that variable. Then call writeStatsToFile, using a different filename (suggested "rainstats.dat") and the All variable of values, to print the stats report to the disk. Note that the printing of the rainfall report should be done in the
client program file not in the Stats class though sending the report to disk is done with the Stats class.
The program utilizing the extended stats class should be in a separate .cpp file. Name this program
rainfallextend.cpp
This is the code from the original assignment
main.cpp
#include
#include"Stats.h"
using namespace std;
int main()
{
Stats stats;
double rainfallAmount;
cout << "Enter the rainfall data of the 12 months below. " << endl;
cout << "----------------------------------------------" << endl;
for (int i = 0; i < 12; i++)
{
cin >> rainfallAmount;
stats.setValue(i, rainfallAmount);
}
double total = stats.getTotal();
double maxRainfall = stats.getLargest();
double minRainfall = stats.getSmallest();
double AvgRainfall = stats.getAvg();
cout << endl;
cout << "#1: Rainfall Data: ";
stats.printValues();
cout << "#2: Total Rainfall: " << total << " units" << endl;
cout << "#3: Maximum Rainfall: " << maxRainfall << " units" << endl;
cout << "#4: Minimum Rainfall: " << minRainfall << " units" << endl;
cout << "#5: Average Rainfall: " << AvgRainfall << " units" << endl;
system("pause");
}
Stats.cpp
#include
#include"Stats.h"
using namespace std;
Stats::Stats()
{
for (int i = 0; i < 12; i++)
array[i] = 0.0;
}
// function to setValue into the array at a given index
void Stats::setValue(int index, double value)
{
// checking whether a valid index was
// provided or not
if (index >= 0 && index < 12)
array[index] = value;
}
// function to print values of the array
void Stats::printValues()
{
for (int i = 0; i < 12; i++)
cout << array[i] << " ";
cout << endl << endl;
}
// function to return total values
double Stats::getTotal()
{
double sum = 0;
for (int i = 0; i < 12; i++)
sum += array[i];
return sum;
}
// function to return average
double Stats::getAvg()
{
double total = getTotal();
return total / 12;
}
// function to return the largest of the array
double Stats::getLargest()
{
double largest = array[0];
for (int i = 0; i < 12; i++)
{
if (array[i] > largest)
largest = array[i];
}
return largest;
}
// function to return smallest value in array
double Stats::getSmallest()
{
double smallest = array[0];
for (int i = 0; i < 12; i++)
{
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
Stats.h
#ifndef STATS_H
#define STATS_H
class Stats {
private:
double array[12];
public:
Stats();
void setValue(int index, double value);
void printValues();
double getTotal();
double getAvg();
double getLargest();
double getSmallest();
};
//#include"Stats.cpp"
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
