cs31 lecture 2

# 8 a few observations x an algorithm is a high level

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

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 C++ x Each verb in an instruction can be elaborated in an Each algorithm algorithm x Only keywords and arithmetic operators never need to be Only detailed 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 Hypothesis: tank than it achieved since the last refuel. tank milesToRefuel = (c – g) * (m / g) milesToRefuel 10 Second problem: compute the miles to refuel x Step 2: write the algorithm Algorithm ComputeMilesLeft: IInput: nput: • • • 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.

Ask a homework question - tutors are online