Overview
In this assignment, you will analyze three types of household utility expenses (gas, electricity and water) over 12 months.
You will read the expenses from a file into 3 one-dimensional arrays of type double to store the three utility expenses for 12 months. You will need to declare a symbolic constant that represents the size of a single array:
const int NUM_MON = 12; //number of months;
You need to do the following analysis on the data you read in:
1. For each type of utility expenses, you need to calculate and print out:
the sum over 12 months
the average of 12 months
the highest cost
the lowest cost
2. For each month, you need to calculate and print out the sum of 3 expenses of each month; Then sort the sum of monthly expenses in ascending order; print out the sorted monthly expense; print out the highest monthly expense, the lowest monthly expense, and the average monthly expense.
3. Numbers should be printed such that there are 2 digits after the decimal point
Input
Your program will read from a file by explicitly opening the file.
The file's format is as follows: Each line is either a string or a double. It starts with a string that describes the type of utility of the next 12 lines (doubles) that are the expenses from month 1 to month 12. Then (at line 14), it is again a string that describes the type of next 12 numbers. Since there are 3 types of expenses, the file has a total of 39 lines:
gas
207.14 <-- expense for month 1
177.34 <-- expense for month 2
...
170.90 <-- expense for month 12
electricity
70.34
60.45
...
50.90
water
40.56
42.67
...
49.34
You can get the file "expenses.txt" from here
How to open and read from a file
1. Declare #include <fstream>.
2. In your buildArrays() function, declare an input file stream variable:
ifstream inFile;
3. "Open" the file (connect the program to the file). Also check to make sure that the file was opened successfully:
inFile.open("expenses.txt");
if (inFile.fail())
{
cout << "Unable to open expenses.txt file\n";
exit(1);
}
4. To read a series of strings and expenses from the file until the end of file is reached, use your input file stream variable instead of cin with the following loop:
read a string into some temporary string variable. (You don't need to use it in this assignment.)
use a for loop to read the 12 doubles into one of the dimensional arrays:
inFile >> gas[month]; // this reads a value into one spot of the array
Repeat the above process 2 more times: once for the electricity array and once for the water array
Remember to use the defined constant for the total number of months.
5. After you have read the last value from the input file and reached end of file, you should "close" the file:
inFile.close();
Functions to write and use
Write and use the following functions for this program:
void buildArrays( double gas[], double electricity[], double water[] )
This function will be called to load all the data into the three one-dimensional arrays. Use the logic presented in step 4 above.
void printUtilityStat(string caption, double array[], int size)
This function will be called 3 times for three utilities in the program. It in turn calls the statistical functions (mean, sum, high, low) to print out the statistics.
void getSumArray( double gas[], double electricity[], double water[], double sums[] )
This function takes in four one-dimensional arrays. This function takes each of the utility values for a specific month and adds them up. The resulting sum is then stored in the corresponding location of the sums array.
For example, sums[0] is equal to the sum of gas[0], electricity[0], and water[0].
void printArray(string caption, double array[], int size)
This function should print the current contents of a single array, with a caption (title) which is supplied as the first argument, and another array of int. When printing the numbers, print one line for the caption. Then print out the contents of the array one entry per line. Add a '$' before prinitng each amount.
This function will be called twice in main: once to show the contents of the sums array before it is sorted, and once to show the contents after it has been sorted.
double mean(double array[], int size)
This function returns the mean (average) of the numbers in the array passed to it. This function (and the three functions below) will be used for calculating the means for each utility and for monthly total. Note that this function can call the sum() below to avoid duplicated logic.
double sum(double array[], int size)
This function returns the sum of the numbers in the array passed to it.
double low(double array[], int size)
This function returns the smal