Lab9: Review Exercise¶

It is time for a review exercise that should help you settle into the swing of writing simple programs that solve some kind of problem. Let’s consider comparing the gas cost for two possible vehicles you might consider buying, a Toyota Corolla, or a Toyota Prius.

The review project¶

As a review of the material we have covered so far in the course, I want you to study the program presented below and identify where in that program the concepts we have studied are demonstrated. The list that follows was taken from the lecture notes, so you should be familiar with them. Here is the list of concepts we have studied (in no particular order):

1. Defining the problem we want to solve
2. Decomposing the problem into manageable chinks
3. Input-process-output (IPO) design process
4. Basic program layout (standard includes, the namespace line, main function)
5. Simple input and output statements
6. Basic data types we can use
7. Standard structures (sequence, decision, loop)
8. Basic structured statements (assignment, if-then-else, for loop, while loop)
9. Program style
10. Variable declarations (naming conventions)
11. Constants
12. Scope of variables (where can you reference a variable name in your program)
13. Using standard C++ functions like sin()
14. How math works (difference in math for integers and floating point numbers)
15. Operator precedence (multiply and divide happened before add or subtract)
16. Assignment statements
17. Functions (void and value returning) setup and use
18. Function parameters
19. Logical expressions (operators, AND, OR operations)
20. Adding up a bunch of numbers in a loop
21. Counted loops

This assignment will be pretty simple. Fire up a editing program of some sort (Word will do, so will the editor in CLion) and write up a summary of the concepts you see demonstrated on each line in the program (except for those with just a closing curly bracket, or a blank line). If several lines in a row are similar (like they all declare variables) you may group those lines together in your writeup. Just indicate all the lines that your writeup covers.

In your writeup, indicate (briefly) what the line (or group of lines) is doing. It may be defining a variable of a certain type, checking the value to see if it is too big, whatever makes sense. The goal here is to make sure you understand exactly what the program is doing and why each statement is there.

Create a paragraph per line (or group of lines) to make it readable. You can number the paragraphs using the line numbers you will see in the listing below. Put a blank line between each paragraph.

Think about what you see in this program. You should have used all the concepts in the above list somewhere in your writeup, see if you can make that happen.

Modify the Code¶

The program shown makes an assumption that gas will rise in price smoothly (linearly) over the number of years we define. Since that may not be realistic, let’s try to modify the gasPrice function so it works this way:

Gas will rise in price from the defined initial value to the defined final value over the course of 4 years, then remain fixed at that higher value for the following years.

To make this change, you will need to use another if-then-else statement to see if the day number is after 4 years. If not, we use a formula similar to the one shown currently in the listing. If so, we return the higher price.

Make changes to the function and rerun the analysis to figure out how expensive it will be to drive the Prius and the Corolla for the eight years we looked at in the lecture. Will the Prius be a better buy in this case, or a worse buy?

Here is the program listing, with line numbers for easy reference:

 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 #include #include using namespace std; const int MILES_PER_YEAR = 21000; const double CITY_PERCENT = 45.0; const double HIGHWAY_PERCENT = 55.0; const double CITY_MPG = 51.0; const double HIGHWAY_MPG = 45.0; const double USABLE_GAS = 9.0; const double INITIAL_PRICE = 3.359; const double FINAL_PRICE = 6.00; const int NUM_YEARS = 8; double gasPrice(int day); int main(int argc, char * argv[]) { cout << "Driving the Toyota Prius" << endl; double daily_miles = MILES_PER_YEAR / 365.0; double daily_city_miles = daily_miles * CITY_PERCENT/100.0; double daily_highway_miles = daily_miles*HIGHWAY_PERCENT/100.0; double daily_gas_consumed = daily_highway_miles / HIGHWAY_MPG + daily_city_miles / CITY_MPG; double gas_in_tank = USABLE_GAS; double price; double amount_purchased; double gallons_purchased; double total_gas_purchases = 0; for(int day = 0;day < 365*8; day++) { cout << "Driving summary for day " << day << endl; cout << " highway miles: " << daily_highway_miles << endl; cout << " city miles : " << daily_city_miles << endl; cout << " gas consumed : " << daily_gas_consumed << endl; gas_in_tank = gas_in_tank - daily_gas_consumed; cout << " gas in tank : " << gas_in_tank << endl; if (gas_in_tank < 0.0) { cout << " BUY GAS" << endl; gallons_purchased = USABLE_GAS - gas_in_tank; price = gasPrice(day); cout << " price today is : " << price << endl; cout << " Gallons purchased: " << gallons_purchased << endl; cout << " fillup cost : " << gallons_purchased * price << endl; total_gas_purchases = total_gas_purchases + gallons_purchased * price; cout << " total gas cost : " << total_gas_purchases << endl; gas_in_tank = USABLE_GAS; } } return EXIT_SUCCESS; } double gasPrice(int day) { double dailyIncrease = (FINAL_PRICE - INITIAL_PRICE) / (NUM_YEARS * 365); double currentPrice = INITIAL_PRICE + day * dailyIncrease; return currentPrice; }