Friday, October 06, 2006

 

Solutions to Chapter 5, p. 92, #1, 2, 4, 5, 6, 8

Note: These are not the ONLY way of solving the problems. You may have found a way that is different but just as good.






% The "05-01 Solution" program
const factor := 2.54
% number of cm in 1 inch
var len : real
put "Enter the length of the desk in inches: " ..
get len
put "The length of the desk in centimeters is: ", len * factor, " cm."





% The "05-02 Solution" program
const currentYear := 2006
var birthYear : int
put "Enter the year you were born (e.g. 1981):" ..
get birthYear
put "In ", currentYear," you will be ", currentYear - birthYear, " years old"



OR - GET THE USER TO ENTER THE CURRENT YEAR, AS BELOW



var birthYear, thisYear : int
put "Enter the year you were born (e.g. 1981):" ..
get birthYear
put "Enter the current year :"
get thisYear
put "In ", thisYear," you will be ", thisYear - birthYear, " years old."






% The "05-04" Solution" program
var number1, number2, number3, number4 : int
put "Enter 4 numbers"
% Note that all 4 numbers can be entered on one line
get number1, number2, number3, number4
put "Here are the four numbers that you entered: " ..
put number1, ",", number2, ",", number3, ",", number4
% commas in quotation marks, and commas separating items






% The "05-05" Solution" program
const GREETING:= "Hello, "
const PUNCTUATION := "!"
var name : string
put "Enter your name, please"
get name
put GREETING,name,PUNCTUATION






% The "05-06 Solution" program
var number1, number2, number3, product: int
put "Enter 3 numbers"
get number1, number2, number3
product:= number1 * number2 * number3
% the variable product is not necessary but it
% shortens the multiplication expressions below
put "The product of ", number1, " * ", number2, " * ", number3, " = ", answer
put ""
put "The square of ", product, " is ", product**2






% The "Ch05_08" Program
% The two inputs show two methods
var firstName, lastName : string
put "Enter a first name: "
get firstName
% input one string per line
put "Enter a last name: "
get lastName
put lastName, ", ",firstName
put "Enter a first and last name: "
get firstName, lastName
% input two strings per line
put lastName, ", ",firstName









<< Home

This page is powered by Blogger. Isn't yours?