Eric Roberts Handout #12A
CS106A September 29, 1999
Solutions to Section #1
Problem 1. Karel at helper hours
/*
* Procedure: MoveToNextColumn
* ---------------------------
* This procedure moves Karel from the base of one column
* to the base of the next.
*/
procedure MoveToNextColumn()
{
TurnRight();
iterate (4) {
Move();
}
TurnRight();
}
/*
* Procedure: HelpStudent
* ----------------------
* This procedure instructs Karel to fill the hole directly
* below its current position. Note that this procedure is
* identical to FillHole from the text.
*/
procedure HelpStudent()
{
TurnRight();
Move();
if (NoBeepersPresent()) {
PutBeeper();
}
TurnAround();
Move();
TurnRight();
}
Problem 2. Converting Fahrenheit to Celsius
/*
* File: tempconv.c
* ----------------
* This program converts a temperature value given in degrees * Fahrenheit into its equivalent in degrees Celsius.
*/
#include
#include "genlib.h"
#include "simpio.h"
main()
{
double fahrenheit, celsius;
printf("Program to convert Fahrenheit to Celsius.\n");
printf("Fahrenheit temperature? ");
fahrenheit = GetReal();
celsius = (5.0 / 9.0) * (fahrenheit - 32);
printf("Celsius equivalent: %g\n", celsius);
}