Trying to find the average array

Write your question here.
Hello folks, I am not able to find the average arrays from my .txt file. Is there any solution to this?
Thank you for your time!

This is whats in the .txt file.

01-03-1994:0.992
01-10-1994:0.995
01-17-1994:1.001
01-24-1994:0.999
01-31-1994:1.005
02-07-1994:1.007
02-14-1994:1.016
02-21-1994:1.009
02-28-1994:1.004
03-07-1994:1.007
03-14-1994:1.005
03-21-1994:1.007
03-28-1994:1.012
04-04-1994:1.011
04-11-1994:1.028
04-18-1994:1.033
04-25-1994:1.037
05-02-1994:1.04
05-09-1994:1.045
05-16-1994:1.046
05-23-1994:1.05
05-30-1994:1.056
06-06-1994:1.065
06-13-1994:1.073
06-20-1994:1.079
06-27-1994:1.095
07-04-1994:1.097
07-11-1994:1.103
07-18-1994:1.109

My code.

/*In the student sample programs for this book, you will find a text file named 1994_Weekly_Gas_Averages.txtPreview the document.
The file contains the average gas price for each week in the year 1994 (There are 52 lines in the file.
Line 1 contains the average price for week 1, line 2 contains the average price for week 2, and so forth.)
Write a program that reads the gas prices from the file into an array or a vector. The program should do the following:
Display the lowest average price of the year, along with the week number for that price, and the name of the month in which it occurred.
Display the highest average price of the year, along with the week number for that price, and the name of the month in which it occurred.
Display the average gas price for each month. (To get the average price for a given month, calculate the average of the average weekly prices for that month.)*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <algorithm>


using namespace std;



int main()
{
vector<string> datesVec;
string dates;


string monthName[2] = { "Janurary", "December"};

double avg;
int sumTotal = 0;



ifstream inFile;

inFile.open("weeklyGas.txt");
while (inFile >> dates)
{
datesVec.push_back(dates);
cout << dates << endl;
}
inFile.close();



cout << "lowest and highest of gas prices. " << endl;
cout << *min_element(datesVec.begin(), datesVec.end()) << ": " << monthName[0] << endl;
cout << *max_element(datesVec.begin(), datesVec.end()) << ": " << monthName[1] << endl;


return 0;
}
at the same time you read it in, total it up in a variable. after that, divide by .size() to get the average.

Heh, the good old days. I remember when it first went over a buck and everyone was all fired up.
There's a lot more to this than just finding the average. Let's go through the assignment's details sentence by sentence:

Display the lowest average price of the year, along with the week number for that price, and the name of the month in which it occurred.

So you need to find the minimum value and the week number that it occurs. So it might be best to read all the data. You could put it into a vector or array - that way the week number is just index+1

"and the name of the month". That means you must parse the date field and you need a function or array to convert a month number to its name. Jumping ahead, you only need to remember the month number. You can throw away the rest of the date.

Display the highest average price of the year, along with the week number for that price, and the name of the month in which it occurred.

That's almost identical to the requirement above.

Display the average gas price for each month. (To get the average price for a given month, calculate the average of the average weekly prices for that month.)

So you need another pass through the data to compute the monthly average.

May I have an example? I am still confused as to find the average gas price per month. Sorry for the inconvenience!
To print the monthly average, you go through the data accumulating statistics. When you see a new month, you print the data for the previous month:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct Prices {
    int monthNo;
    double price;
    bool read(istream &is);
};

void showMonthlyAverages(vector<Prices> &data)
{
    if (data.size() == 0) return;

    int month = data[0].monthNo;
    unsigned count=0;
    double sum = 0.0;
    for (unsigned i=0; i<data.size(); ++i) {
        if (data[i].monthNo != month) {
            // New month. Print the data for the last month
            cout << month << '\t' << sum/count << '\n';
            sum = count = 0;
            month = data[i].monthNo;
        }
        sum += data[i].price;
        ++count;
    }
    // Now print the last one
    cout << month << '\t' << sum/count << '\n';
}

Topic archived. No new replies allowed.