This preview shows page 1. Sign up to view the full content.
Unformatted text preview: /else, etc.)
8 A few observations
x An algorithm is a high-level description of a procedure
x But it does not contain all the info required to implement a
But C++ program out of it
x Each verb in an instruction can be elaborated in an
x Only keywords and arithmetic operators never need to be
detailed 9 Second problem: compute the miles to refuel
x Problem statement (informal) Compute the number of miles remaining before refueling x Step 1: formulate precisely the problem m: the number of miles done sine last refueling g: the number of gallons of gas consumed since last refueling c: the capacity of the gas tank (in gallons) Hypothesis: the car will have the same mpg for the rest of the
tank than it achieved since the last refuel.
tank milesToRefuel = (c – g) * (m / g)
10 Second problem: compute the miles to refuel
x Step 2: write the algorithm Algorithm ComputeMilesLeft:
• m: number of miles since last refuel
g: number of gallons consumed since last refuel
c: the capacity of the gas tank (in gallons) Output: • milesToRefuel: the number of miles before refuel is need...
View Full Document
This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.
- Fall '00