Question: Submit your source code files (all .h and .cpp files), make sure your name is on each file you submit. Make sure and use three
Submit your source code files (all .h and .cpp files), make sure your name is on each file you submit.
Make sure and use three separate files for your object oriented programs (this means for each program you need to submit three files on Canvas (two .cpp files and 1 .h file) plus UML diagrams. Do not use inline functions. IF YOU VIOLATE THESE RULES points off. NO GLOBAL VARIABLES no exceptions. Programs with global variables will not be accepted (global constants are fine) Also, do NOT DYNAMICALLY allocate the array, period! Do NOT use getline to read numbers from the file read the numbers directly from the fileProgram 1: Extend Stats class and Rainfall Statistics Based on Chapter 9 lecture and struct (20 pts)Take program #2 of lab 8 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 parameterExtend 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 valuesof 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 Statsclass 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.
Here is the code that needs to be extended:
HEADER FILE:
#ifndef STATS_H
#define STATS_H
class Stats
{
private:
double rainFall[12];
public:
Stats();
void setValues(int, double);
void printValues();
double getTotal();
double getAverage();
double getLargest();
double getSmallest();
};
RAINFALL.CPP FILE
#include
using namespace std;
Stats::Stats() { int i;
for (i = 0; i<12; i++) { rainFall[i] = 0.0; } }
void Stats::setValues(int pos, double val) {
if (val < 0) { val = 0; } rainFall[pos] = val; }
void Stats::printValues() { cout << " Data: \t ";
for (int i = 0; i<12; i++) {
cout << " " << rainFall[i]; }
cout << " "; }
double Stats::getTotal() { double total = 0;
for (int i = 0; i<12; i++) {
total += rainFall[i]; }
return total; }
double Stats::getAverage() { return (getTotal() / (double)12.0); }
double Stats::getLargest() {
double highest = rainFall[0];
for (int i = 0; i<12; i++) { if (rainFall[i] > highest) {
highest = rainFall[i]; } }
return highest; }
double Stats::getSmallest() {
double smallest = rainFall[0];
for (int i = 0; i<12; i++) {
if (rainFall[i] < smallest) {
smallest = rainFall[i]; } }
return smallest; }
MAIN CPP FILE
#include
using namespace std;
int main() { double val; Stats rainfall; int i;
for (i = 0; i<12; i++) {
cout << " Enter month " << (i + 1) << " data: "; cin >> val;
rainfall.setValues(i, val); }
rainfall.printValues();
cout << " Total rainfall: " << rainfall.getTotal(); cout << " Average rainfall: " << rainfall.getAverage(); cout << " Largest rainfall: " << rainfall.getLargest(); cout << " Smallest rainfall: " << rainfall.getSmallest();
cout << " "; return 0; system("pause"); }
i need three separate files header file, cpp file and a main cpp file. i would really appreciate anyone who can help. i have no clue how to extend this program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
