.. _lab9-cpp-review: Lab9: Review Exercise ####################### .. include:: /references.inc 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 Your job ######## 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: .. literalinclude:: code/main.cpp :language: c :linenos: Add Another Function ******************** Go online and get the current purchase price for each car. Then set up a function that calculates the total cost of owning this car over that eight year period. (Obviously, we are ignoring maintenance costs here). This function has the following form: .. code-block:: c double cost_per_mile(double purchase, double fuel, double miles); All you need to do in this function is calculate the cost per mile driven for a vehicle. Use this function to add output lines in your main function showing which vehicle would be a smarter choice. (Of course, this assumes that this is your only concern in buying a car. If you hate fossil fuel, well, your choice is your own. YMMV!)