Some Background The EPA provides a nice web site where you c
Some Background
The EPA provides a nice web site where you can make such calculations (https://www3.epa.gov/carbon-footprint-calculator/ ) as well as an Excel spreadsheet to explain the values. We are just going to look at the calculations for automobile gasses. Here are the facts:
The average car emits 19.6 lbs_CO2/gallon_gas
That emission, when accounting for other greenhouse gasses (carbon monoxide, nitrous
oxide) increases the amount by a factor 100/98.65 . That is, the amount of CO2 times this ratio is the total amount of greenhouse gas emitted
Program Specifications
Your program will do the following:
Take as input two floating point values (in this order, as indicated):
the mpg, the number of miles per gallon for your car
the miles driven per year
Print the following three results on a single line, each value separated from the other by a
single space. The precision is to two decimal point of accuracy (see note 1 below)
the lbs of greenhouse gas emitted in one year
the change in lbs of greenhouse gas emitted if the mileage went up by 1 mpg
the change in lbs of greenhouse gas emitted if the mileage went up by 5 mpg
Deliverables
proj01.cpp -- your source code solution (remember to include your section, the date, project number and comments).
Please be sure to use the specified file name, i.e. “proj01.cpp”
Save a copy of your file in your CSE account disk space (H drive on CSE computers).
You will electronically submit a copy of the file using the \"handin\" program:
http://www.cse.msu.edu/handin/webclient
Assignment Notes:
1. We might as well try to make the output somewhat readable. You can look this up in the text or on the internet, but you can use the following modifiers that affect how numbers print.
a. cout << fixed Elements will be printed as floating point numbers (ex 123.456)
cout << scientific Elements will be printed in scientific notation (ex 1.23456 x 102)
cout << setprecision(2) Floating point numbers will have 2 values after the decimal point and will be rounded, (123.46)
Thus cout << fixed << setprecision(2) << 123.4567 << endl; will print 123.46
The following statement will read two variables off of the same, space separated line. It is an example of chaining input:
...
double d1, d2;
...
There are three pairs of input and output files provided for you to check yourself. The pairs
are inX.txt and outX.txt, where X is some integer number, and the number indicates the pair. Thus if you compile your code, using the default executable name a.out, then
./a.out < int1.txt should produce the output found in out1.txt . Look at lab00 for examples of redirected input.
You do not have to check for bad input values. In general, we will explicitly indicate the errors we are looking for, but for now we are not checking for input errors.
If you read off of a single line for the input, you can do a handy trick off the command line. You can redirect a file (with that single line of input) to your main program. Thus in the terminal, in the directory where your program proj01 resides, you can do:
This will automatically feed the input to the cin statement and produce the output. Makes it
easier to test things (and to grade them too!!).
You can check your calculations on http://www.wolframalpha.com/ or using python or some
other approach that you are comfortable with.
Getting Started
Create a new directory for your project
If you are in a CSE lab or on x2go, select the H: drive as the location to store your file.
Save the name of the project: proj01.cpp
Prompt for some of the values and print them back out, just to check yourself.
When you do a calculation, use the provided table as a way to check yourself.
Use the handin web site to hand in the program (incomplete as this point, but you should
continually hand things in).
Now you can calculate the individual elements of the project. After writing code for each of
the below aspects, compile and print out values to make sure you are getting the correct
values. Do it INCREMENTALLY, that is write and check various calculations
Now you enter a cycle of edit-run to incrementally develop your program.
Solution
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double mpg;
double miles_per_year;
// lbs per gallon obtained by multiplying co2 factor
double lbs_per_gallon = 19.6*(100/98.65);
// reading value of miles per gallon and miles driven in a year
cin >> mpg >> miles_per_year;
// computing gallon with given mpg
double gallons = miles_per_year/mpg;
// computing gallon if mpg is up by 1
double gallon1 = miles_per_year/(mpg+1);
// computing gallon if mpg is up by 5
double gallon2 = miles_per_year/(mpg+5);
// computing weight
double green_house_lbs = gallons*lbs_per_gallon;
// computing weight if mpg is up by 1
double green_house_lbs1 = gallon1*lbs_per_gallon;
// computing weight if mpg is up by 5
double green_house_lbs2 = gallon2*lbs_per_gallon;
// printing result with 2 precision digit and a space
cout << fixed << setprecision(2) << green_house_lbs << \" \";
cout << fixed << setprecision(2) << green_house_lbs1 << \" \";
cout << fixed << setprecision(2) << green_house_lbs2 <<endl;
return 0;
}
/*
Sample run
$ ./a.out
5 30
119.21 99.34 59.60
*/


