Extended Prelude to Programming

Third Edition

Solutions to Selected Programming Problems

for the Instructor

Chapter 1

Problem 2

Declare Price, Tip As Real

Write “Enter the price of the meal.”

Input Price

Set Tip = 0.15 * Price

Write “Price of the meal: ”, Price

Write “Amount of the tip: ”, Tip

Problem 3

Declare C, F As Real

Write “Enter a temperature in degrees Celsius.”

Input C

Set F = (9 / 5) * C + 32

Write “Temperature in degrees Celsius: ”, C

Write “Temperature in degrees Fahrenheit: ”, F

Problem 4

Declare AtBats, Hits As Integer

Declare Average As Real

Write “Enter the number of at bats.”

Input AtBats

Write “Enter the number of at hits.”

Input Hits

Set Average = Hits / AtBats

Write “At bats: ”, AtBats

Write “Hits: ”, Hits

Write “Batting average: ”, Average

Problem 5

Declare P, R, T, I, FV As Real

Write “Enter the amount to be invested.”

Input P

Write “Enter the rate of interest as a decimal.”

Input R

Write “Enter the term of the investment (in years).”

Input T

Set I = P * R * T

Set FV = P + I

Write “The amount invested: ”, P, “ dollars”

Write “The rate of interest: ”, R * 100, “%”

Write “The term of the investment: ”, T, “ years”

Write “The future value: ”, FV, “ dollars”

Problem 6

Declare FirstName, LastName, FullName As String

Declare MiddleInitial As Character

Write “Enter the first name.”

Input FirstName

Write “Enter the middle initial.”

Input MiddleInitial

Write “Enter the last name.”

Input LastName

Set FullName = FirstName + “ ” + MiddleInitial + “. ” + LastName

Write “The full name is”

Write FullName

Chapter 2

Problem 1

Main module

Declare Sales, PercentRate, Commission As Real

Write “Commission Calculator”

Call Input Data module

Call Process Data module

Call Output Results module

End Program

Input Data module

Write “Enter the sales total for the month.”

Input Sales

Write “Enter the commission rate as a percentage.”

Input PercentRate

Process Data module

Declare Rate As Real

Set Rate = PercentRate / 100

Set Commission = Sales * Rate

Output Results module

Write “Sales for the month: ”, Sales, “ dollars”

Write “Commission rate: ”, PercentRate, “%”

Write “Commission for the month: ”, Commission, “ dollars”

Problem 2

Main module

Declare Name As String

Declare Pounds, Ounces As Integer

Declare Price, Unit Price As Real

Write “Unit Price Conversion Program”

Call Input Data module

Call Process Data module

Call Output Results module

End Program

Input Data module

Write “Enter the name of the item.”

Input Name

Write “Enter the price of the item.”

Input Price

Write “Enter the weight in pounds, ounces.”

Input Pounds, Ounces

Process Data module

Declare TotalOunces As Integer

Set TotalOunces = 16 * Pounds + Ounces

Set UnitPrice = Price / TotalOunces

Output Results module

Write “Item: ”, Name

Write “Price: \$”, Price

Write “Weight: ”, Pounds, “ pounds, ”, Ounces, “ounces”

Write “Unit price: ”, UnitPrice

Problem 4

Main module

Declare ID As String

Declare PayRate, RegularHours, OvertimeHours As Real

Declare GrossPay, Deductions, NetPay As Real

Write “Employee Pay Program”

Call Input Data module

Call Process Data module

Call Output Results module

End Program

Input Data module

Write “Enter the employee’s ID number.”

Input ID

Write “Enter the employee’s rate of pay.”

Input PayRate

Write “Enter the hours worked: regular, overtime”

Input RegularHours, OvertimeHours

Write “Enter the employee’s deductions.”

Input Deductions

Process Data module

Set GrossPay = PayRate * (RegularHours + 1.5 * OvertimeHours)

Set NetPay = GrossPay - Deductions

Output Results module

Write “Employee ID and pay rate: ”, ID, “ \$”, PayRate, “ per hour”

Write “Hours worked (regular/overtime): ”, RegularHours, “/”, OvertimeHours

Write “Gross pay: \$”, GrossPay

Write “Deductions: \$”, Deductions

Write “Net pay: \$”, NetPay

Problem 6

Main module

Declare LoanAmount, PercentRate, MonthlyPayment As Real

Declare NumberOfPayments As Integer

Write “Monthly Payment Calculator”

Call Input Data module

Call Process Data module

Call Output Results module

End Program

Input Data module

Write “Input the amount of the loan.”

Input LoanAmount

Write “Enter the rate of interest as a percentage.”

Input PercentRate

Write “Enter the number of payments to be made.”

Input NumberOfPayments

Process Data module

Declare Rate, Power As Real

Set Rate = PercentRate / 100

Set Power = (1 + Rate) ^ NumberOfPayments

Set MonthlyPayment = LoanAmount * Rate* Power / (Power - 1)

Output Results module

Write “Amount of loan: \$”, LoanAmount

Write “Rate of interest: ”, PercentRate, “%”

Write “Number of payments: ”, NumberOfPayments

Write “Monthly payment: \$”, MonthlyPayment

Chapter 3

Problem 1

Main module

Declare Number As Real

Call Input Number module

Call Display Message module

End Program

Input Number module

Write “Enter a number.”

Input Number

Display Message module

If Number > 0 Then

Write “Positive”

End If

If Number < 0 Then

Write “Negative”

End If

If Number = 0 Then

Write “Neither Positive or Negative”

End If

Problem 2

Main module

Declare Num1, Num2 As Real

Declare Choice As Integer

Write “Basic Calculator”

Call Input Data module

Call Input Choice module

Select Case Of Choice

Case 0:

Write “Goodbye”

Case 1:

Call Display Sum module

Case 2:

Call Display Difference module

Case 3:

Call Display Product module

Case 4:

Call Display Quotient module

End Case

End Program

Input Data module

Write “Enter two numbers.”

Input Num1, Num2

Input Choice module

Write “0 - Quit program”

Write “1 - Display sum”

Write “2 - Display difference”

Write “3 - Display product”

Write “4 - Display quotient”

Input Choice

Display Sum module

Declare Sum As Real

Set Sum = Num1 + Num2

Write “The sum of ”, Num1, “ and ”, Num2, “ is:”

Write Sum

Display Difference module

Declare Difference As Real

Set Difference = Num1 - Num2

Write “The difference of ”, Num1, “ and ”, Num2, “ is:”

Write Difference

Display Product module

Declare Product As Real

Set Product = Num1 * Num2

Write “The product of ”, Num1, “ and ”, Num2, “ is:”

Write Product

Display Quotient module

Declare Quotient As Real

If Num2 > 0 Then

Set Quotient = Num1 / Num2

Write “The quotient of ”, Num1, “ and ”, Num2, “ is:”

Write Quotient

Else

Write “No quotient -- Divisor is 0"

End If

Problem 4

Main module

Declare X, Area As Real

Declare Choice As Integer

Write “Area Calculator”

Call Input Data module

Call Input Choice module

Select Case Of Choice

Case 0:

Write “Goodbye”

Case 1:

Call Square module

Case 2:

Call Circle module

Case 3:

Call Triangle module

End Case

End Program

Input Data module

Write “Enter a positive number for”

Write “the side of a square or equilateral triangle”

Write “or the radius of a circle.”

Input X

Input Choice module

Write “0 - Quit program”

Write “1 - Compute area of square”

Write “2 - Compute area of circle”

Write “3 - Compute area of equilateral triangle”

Input Choice

Square module

Set Area = X ^ 2

Write “The area of a square with side ”, X

Write “ is: ”, Area

Circle module

Set Area = 3.14 * X ^ 2

Write “The area of a circle with radius ”, X

Write “ is: ”, Area

Triangle module

Set Area = (Sqrt(3) / 4) * X ^ 2

Write “The area of an equilateral triangle with side ”, X

Write “ is: ”, Area

Problem 6

Main module

Declare Income, Tax As Real

Call Input Income module

Call Compute Tax module

Call Display Tax module

End Program

Input Income module

Input Income

Compute Tax module

If (Income >= 0) And (Income < 50000) Then

Set Tax = 0.05 * Income

End If

If (Income >= 50000) And (Income < 100000) Then

Set Tax = 2500 + 0.07 * Income

End If

If Income >= 100000 Then

Set Tax = 6000 + 0.09 * Income

End If

Display Tax module

Write “Income: ”, Income

Write “Tax: ”, Tax

Problem 7

[See Chapter 4, Problem 7 solution]

Problem 8

[See Chapter 4, Problem 8 solution]

Chapter 4

Problem 1

Main module

Declare MySquare As Real

Write “Table of Squares”

Call Input Number module

Call Display Table module

End Program

Input Number module

Write “Enter a positive number.”

Write “The program will display the squares of”

Write “the numbers from 1 to this number and the sum of the squares.”

Input MySquare

While MySquare <= 0

Write “Enter a positive number.”

Input MySquare

End While

Display Table module

Declare K As Integer

Declare Sum As Real

Write “Number, Square”

For K = 1 Step 1 To MySquare

Write K, K ^ 2

Sum = Sum + K^2

End For

Write “The sum of the squares is “ , Sum

Problem 2

Main module

Declare Sum As Real

Declare Counter As Integer

Write “Average Age Calculator”

Call Process Ages module

Call Display Average module

End Program

Process Ages module

Declare Age As Real

Set Counter = 0

Set Sum = 0

Write “Enter an age. Enter 0 when done.”

Input Age

While Age > 0

Set Counter = Counter + 1

Set Sum = Sum + Age

End While

Display Average module

Declare Average As Real

If Counter > 0 Then

Set Average = Sum / Counter

Write “The average age is ”, Average, “ years.”

Else

Write “No ages were entered.”

End If

Problem 3

Main module

Declare N, NFactorial As Integer

Write “Factorial Calculator”

Call Input Number module

Call Compute Factorial module

Call Display Result module

End Program

Input Number module

Declare Num As Real

Repeat

Write “Enter a positive integer.”

Input Num

Until (Num > 0) And (Int(Num) = Num)

Set N = Num

Compute Factorial module

Declare Product, K As Integer

Set Product = 1

For K = 1 Step 1 Until N

Set Product = Product * K

End For

Set NFactorial = Product

Display Result module

Write N, “! = ”, NFactorial

Problem 5

Main module

Declare P As Real

Write “Bacteria Population”

Call Input Initial Number module

Call Display Table module

End Program

Input Initial Number module

Write “Enter the number of bacteria present initially.”

Input P

While P <= 0

Write “This number must be positive!”

Input P

End While

Display Table module

Declare N As Real

Declare T As Integer

Write “Day, Number”

For T = 1 Step 1 To 10

Set N = P * 2 ^ (T / 10)

Write T, N

End For

Problem 7

Main module

Declare Max As Real

Write “Largest Number Finder”

Call Find Maximum module

Call Display Maximum module

End Program

Find Maximum module

Declare Number As Real

Write “Enter a positive number. Enter 0 when done.”

Input Number

Set Max = Number

While Number > 0

If Number > Max Then

Set Max = Number

End If

Write “Enter a positive number. Enter 0 when done.”

Input Number

End While

Display Maximum module

Write “The largest number input is”

Write Maximum

Problem 8

Main module

Declare PositiveSum, NegativeSum As Real

Write “Positive/Negative Sum Calculator”

Call Find Sums module

Call Display Sums module

End Program

Find Sums module

Declare Number As Real

Set PositiveSum = 0

Set NegativeSum = 0

Write “Enter a number. Enter 0 when done.”

Input Number

While Number > 0

If Number > 0 Then

Set PositiveSum = PositiveSum + Number

Else

Set NegativeSum = NegativeSum + Number

End If

Write “Enter a number. Enter 0 when done.”

Input Number

End While

Display Sums module

Write “The sum of the positive numbers is ”, PositiveSum

Write “The sum of the negative numbers is ”, NegativeSum

Chapter 5

Problem 1

Main module

Declare Numbers[100] Of Reals

Declare Max, Min As Real

Declare Count As Integer

Write “Maximum and Minimum Values”

Call Input Numbers module

Call Find MaxMin module

Call Display MaxMin module

End Program

Input Numbers module

Declare Num As Real

Set Count = 0

Write “Enter a number. Enter 0 to quit.”

Input Num

While Num > 0

Set Numbers[Count] = Num

Set Count = Count + 1

Write “Enter a number. Enter 0 to quit.”

Input Num

End While

Find MaxMin module

Declare K As Integer

Set Max = Numbers[0]

Set Min = Numbers[0]

For K = 1 Step 1 To Count - 1

If Numbers[K] > Max Then

Set Max = Numbers[K]

End If

If Numbers[K] < Min Then

Set Min = Numbers[K]

End If

End For

Display MaxMin module

Declare K As Integer

Write “Given the numbers:”

For K = 0 Step 1 To Count - 1

Write Numbers[K]

End For

Write “The largest is ”, Max

Write “The smallest is ”, Min

Problem 2

Main module

Declare X[100] Of Reals

Declare Mean, StandardDeviation As Real

Declare Count As Integer

Write “Mean and Standard Deviation”

Call Input Numbers module

Call Compute Results module

Call Display Results module

End Program

Input Numbers module

Declare Num AS Real

Set Count = 0

Write “Enter a number. Enter 0 to quit.”

Input Num

While Num > 0

Set X[Count] = Num

Set Count = Count + 1

Write “Enter a number. Enter 0 to quit.”

Input Num

End While

Compute Results module

Declare Sum As Real

Declare K As Integer

If Count > 0

Set Sum = 0

For K = 0 Step 1 To Count - 1

Set Sum = Sum + X[K]

End For

Set Mean = Sum / Count

If Count = 1 Then

Set StandardDeviation = 0

Else

Set Sum = 0

For K = 0 Step 1 To Count - 1

Set Sum = Sum + (X[K] - Mean) ^ 2

End For

Set StandardDeviation = Sqrt(Sum / (Count - 1))

End If

End If

Display Results module

If Count = 0 Then

Write “No numbers were input.”

Else

Write “The mean is ”, Mean

Write “The standard deviation is ”, StandardDeviation

End If

Problem 4

Main module

Declare Prices[100] Of Reals

Declare Median As Real

Declare Count As Integer

Write “Median Home Price”

Call Input Prices module

Call Sort Prices module

Call Compute Median module

Call Display Median module

End Program

Input Prices module

Declare Price As Real

Set Count = 0

Write “Enter the price of a home. Enter 0 to quit.”

Input Price

While Price > 0

Set Prices[Count] = Num

Set Count = Count + 1

Write “Enter the price of a home. Enter 0 to quit.”

Input Price

End While

Sort Prices module

Declare Flag As Integer

Declare Temp As Real

Set Flag = 0

While Flag = 0

Set Flag = 1

For K = 0 Step 1 To Count - 2

If Prices[K] > Prices[K + 1] Then

Set Prices[K] = Temp

Set Prices[K] = Prices[K + 1]

Set Prices[K + 1] = Temp

Set Flag = 0

End If

End For

End While

Compute Median module

If Count/2 > Int(Count/2) Then

Set Median = Prices[(Count + 1)/2]

Else

Set Median = (Prices[Count/2] + Prices[Count/2 + 1]) / 2

End If

Display Median module

Write “The median price of the homes n Botany Bay is”

Write “\$”, Median

Problem 5

Main module

Declare A[4, 4] Of Integers

Declare IsMagicSquare As Integer

Write “Test for a Magic Square”

Call Input Square module

Call Test Square module

Call Display Result module

End Program

Input Square module

Declare I, J As Integer

Write “Enter the rows of a square array; 4 numbers per row.”

For I = 0 Step 1 To 3

Write “Row”, I

For J = 0 Step 1 To 3

Write “Number”, J

Input A[I, J]

End For (I)

End For (J)

Test Square module

Declare Sum, TestSum As Integer

Declare K As Integer

Set IsMagicSquare = 1

Set Sum = A[1, 1] + A[1, 2] + A[1, 3] + A[1, 4]

For K = 2 Step 1 To 4

Set TestSum = A[K, 1] + A[K, 2] + A[K, 3] + A[K, 4]

If Sum > TestSum Then

Set IsMagicSquare = 0

End If

End For

For K = 1 Step 1 To 4

Set TestSum = A[1, K] + A[2, K] + A[3, K] + A[4, K]

If Sum > TestSum Then

Set IsMagicSquare = 0

End If

End For

Set TestSum = A[1, 1] + A[2, 2] + A[3, 3] + A[4, 4]

If Sum > TestSum Then

Set IsMagicSquare = 0

End If

Set TestSum = A[4, 1] + A[3, 2] + A[2, 3] + A[1, 4]

If Sum > TestSum Then

Set IsMagicSquare = 0

End If

Display Result module

If IsMagicSquare = 1 Then

Write “This IS a magic square.”

Else

Write “This is NOT a magic square.”

End If

Problem 6

Declare FirstName, LastName, Phone As String

Declare Customer As String

Declare Found As Integer

Write “Phone Number Finder”

Open “CUSTOMER” For Input As DataFile

Write “Enter customer’s last name.”

Input Customer

Set Found = 0

While Not EOF(DataFile) AND Found = 0

If LastName = Customer Then

Write FirstName, “ ”, LastName, “ ”, Phone

Set Found = 1

End If

End While

If Found = 0 Then

End If

Close DataFile

Problem 8

Main module

Declare NumberTickets As Integer

Declare Tickets[10000] Of Integers

Declare Count As Integer

Call Process User Request module

End Program

Set Count = 0

Set Count = Count + 1

End While

Process User Request module

Declare Driver As String

Declare Found, Index As Integer

Declare Response As Character

Write “Do you want to enter a driver license? (Y/N)”

Input Response

While Response = “Y” Or Response = “y”

Call Find Driver Info module

Call Display Results module

Write “Do you want to enter a driver license? (Y/N)”

Input Response

End While

Write “Enter the driver’s license number.”

Input Driver

Find Driver Info module

Set Index = 0

Set Found = 0

While Found = 0 AND Index < Count

Set Index = Index + 1

Set Found = 1

End If

End While

Display Results module

If Found = 1 Then

Write Names[Index], “ - ”, Licenses[Index]

Write “Number of tickets: ”, Tickets[Index]

Else

End If

Chapter 6

Problem 1

Declare Name As String

Write “This program creates the file GRADES with records:”

Write “ Name, Test 1 Test 2 Test 3”

Write “Enter a student name. Enter ZZZ when done.”

Input Name

While Name > “ZZZ”

Write GradeFile, Name, 0, 0, 0

Write “Enter a student name. Enter ZZZ when done.”

Input Name

End While

Problem 2

Declare Name As String

Declare Test1, Test2, Test3, Sum As Real

Write “This program displays the contents of the file GRADES.”