CS 1044
Spring 1999
#1 page 54 no. 2 - see page 24 for description of Syntax TemplatesRobbie the Robot is touring New Orleans and has discovered that an excellent oyster po-boy can be found at the Acme Oyster House in the French Quarter as shown below on 2nd Avenue at point Y. Currently he is standing a point X on A Street. Some of the street segments are one-way as indicated, but the two segments with ?-marks are both either 1-way east or 1-way west, depending upon traffic conditions and do not change during the course of a single trip from X to Y or from Y to X.#2 page 54 no. 4
#3 page 55 no. 5
#4 Robbie the Robot:

Robbie also has 9 friends with him at point X, and his job is to get a po-boy for each of his friends and return to point X to dine with them. He can only carry one meal at a time, which is of no consequence, since Robbie is, of course, only a robot. However, Robbie is to travel the shortest distance possible, considering the state of the one-way streets as he travels. On one trip he may be able to move east on 2nd Avenue, while on another he may have to take another route.
Your job is to present Robbie's task as a formal algorithm.
A formal algorithm is written ALGORITHM (Name) {commands}, and in an algorithm we write lots of comments (lines beginning with an !) so that humans can figure out what is going on at any point in the algorighm. For example, if Robbie wants to take a walk around Acme from point X one could write,
ALGORITHM (Circle Walk)
! This algorithm describes what Robbie does to take a walk around his favorite
! restaurant at point Y, starting with a southward move from point X.
! Move down to 1st Avenue.
{SOUTH;
! Can't go up C Street since it's one-way in the wrong direction.
REPEAT (3)
{EAST;
};
! Now continue north, then west, and then south back to the starting
! point.
REPEAT (2)
{NORTH;
};
REPEAT (3)
{WEST;
};
REPEAT (2)
{SOUTH;
};
}
Notice the consistent use of indentation, punctuation, blank lines, comments,
and other documentation to make the formal algorithm into an unambiguous
statement of what Robbie does to accomplish the assigned task.
//******************************************************************
// Mileage program
// This program computes miles per gallon given four amounts
// for gallons used, and starting and ending mileage
//
// <TYPE YOUR NAME HERE>
//******************************************************************
#include <fstream.h>
const float AMT1 = 11.7; // Number of gallons for fillup 1
const float AMT2 = 14.3; // Number of gallons for fillup 2
const float AMT3 = 12.2; // Number of gallons for fillup 3
const float AMT4 = 8.5; // Number of gallons for fillup 4
const float START_MILES = 67308.0; // Starting mileage
const float END_MILES = 68750.5; // Ending mileage
int main()
{
float mpg; // Computed miles per gallon
ofstream outputFile; // File containing output data
outputFile.open("hw1.out");
mpg = (END_MILES - START_MILES) / (AMT1 + AMT2 + AMT3 + AMT4);
outputFile << "For the gallon amounts " << endl;
outputFile << AMT1 << ' ' << AMT2 << ' '
<< AMT3 << ' ' << AMT4 << endl;
outputFile << "and a starting mileage of " << START_MILES << endl;
outputFile << "and an ending mileage of " << END_MILES << endl;
outputFile << "the mileage per gallon is " << mpg << endl;
outputFile.close();
return 0;
}
The output from this program is:For the gallon amounts 11.7 14.3 12.2 8.5 and a starting mileage of 67308 and an ending mileage of 68750.5 the mileage per gallon is 30.8887