Chapter 6- Programming Exercises

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

1. Payroll Program with Input validation
REQUIREMENTS
This program will calculate payroll for hours worked. The program prompts the user to enter an employee’s hourly pay rate and the number of hours worked. It will Validate the user’s input so that only pay rates in the range of $7.50 through $18.25 and hours in the range of 0 through 40 are accepted. The program will display the employee’s gross pay.

Pseudo Code
Module main()
Declare Real payRate = 0.0
Declare Integer hoursWorked = 0
Declare Real grossPay = 0.0
Display “Enter pay Rate (range of $7.50 through $18.25): $”
Input payRate
Display “Enter hours worked (range of 0 through 40):”
Input hoursWorked
While isValidRate(payRate) == false OR isValidHours(hoursWorked) == false
Display “Error: payRate or hours out of range!”
Display “Enter pay Rate (range of $7.50 through $18.25): $”
Input payRate
Display “Enter hours worked (range of 0 through 40):”
Input hoursWorked
End While
Set grossPay = getPay(payRate, hoursWorked)
Display “Your grossPay is $”, grossPay
End Module

Function Boolean isValidRate(Real pRate)
Declare Boolean status = false
If pRate >= 7.50 AND pRate <= 18.25 Set status = true End If Return status End Function Function Boolean isValidHours(Integer pHours) Declare Boolean status = false If pHours >= 0 AND pHours <= 40 Set status = true End If Return status End Function Function Real getPay(Real pRate, Integer pHours) Return pRate * pHours End Function 2. Theater Seating Revenue with Input Validation
REQUIREMENTS
This program asks for the number of tickets sold in each section and then displays the amount of income generated from ticket sales.
The program will validate the numbers that are entered for each section. The theater seating and pricing requiremetns are as follows:
A dramatic theater has three seating sections, and it charges the following prices for tickets in each section:
section A seats cost $20 each
section B seats cost $15 each
section C seats cost $10 each
The theater has,
300 seats in section A
500 seats in section B
200 seats in section C

Pseudo Code
Module main()
Declare Integer soldTicketA = 0
Declare Integer soldTicketB = 0
Declare Integer soldTicketC = 0
Declare Real totalSales = 0.0
Constant Integer maxTicketA = 300
Constant Integer maxTicketB = 500
Constant Integer maxTicketC = 200
Constant Real costA = 20.00
Constant Real costB = 15.00
Constant Real costC = 10.00

Display “Enter total ticket sales for section A:”
Input soldTicketA
Display “Enter total ticket sales for section B:”
Input soldTicketB
Display “Enter total ticket sales for section C:”
Input soldTicketC
While isValid(soldTicketA,soldTicketB,soldTicketC) == false
Display “ERROR: tickets for a section is out of range! Capacity for section A=”, maxTicketA, ” section B=”, maxTicketB, ” section C=”, maxTicketC
Display “Enter total ticket sales for section A:”
Input soldTicketA
Display “Enter total ticket sales for section B:”
Input soldTicketB
Display “Enter total ticket sales for section C:”
Input soldTicketC
End While
Set totalSales = getSales(soldTicketA,soldTicketB,soldTicketC)
Display “Your total Sales is $”, totalSales
End Module

Function Boolean isValid(Integer soldA, Integer soldB, Integer soldC)
Declare Boolean status = false
If soldA >= 0 AND soldA <= maxTicketA AND soldB >= 0 AND soldB <= maxTicketB AND soldC >= 0 AND soldC <= maxTicketC Set status = true End If Return status End Function Function Real getSales(Integer soldA, Integer soldB, Integer soldC) Return costA * soldA + costB * soldB + costC * soldC End Function

Leave a Reply

Your email address will not be published. Required fields are marked *