Reputation: 79
Programming Student here...trying to work on a project but I'm stuck.
The project is trying to find the miles per gallon per trip then at the end outputting total miles and total gallons used and averaging miles per gallon
How do I loop back up to the first question after the first set of questions has been asked.
Also how will I average the trips...will I have to have a variable for each of the trips? I'm stuck, any help would be great!
Upvotes: 0
Views: 1233
Reputation: 70993
You will have to tell us the type of data you are given.
As per your last question: remember that an average can be calculated in real time by either storing the sum and the number of data points (two numbers), or the current average and the number of data points (again, two numbers).
For instance:
class Averager {
double avg;
int n;
public:
Averager() : avg(0), n(0) {}
void addPoint(double v) {
avg = (n * avg + v) / (n + 1);
n++;
}
double average() const { return avg; }
};
Upvotes: 2