Tutorial Set 1
Tutorial Set 1
Tutorial Set 1
COURSE : ALGORITHM
CHAPTER 1
Question 4: Algorithm Workbench
1. Draw a flowchart or write pseudocode to represent the logic of a program that allows
the user to enter a value. The program divides the value by 2 and outputs the result.
2. Draw a flowchart or write pseudocode to represent the logic of a program that allows
the user to enter a value for one edge of a cube. The program calculates the surface
area of one side of the cube, the surface area of the cube, and its volume. The
program outputs all the results.
3. Draw a flowchart or write pseudocode to represent the logic of a program that allows
the user to enter two values. The program outputs the product of the two values.
4. a. Draw a flowchart or write pseudocode to represent the logic of a program that
allows the user to enter values for the width and length of a room’s floor in feet. The
program outputs the area of the floor in square feet.
b. Modify the program that computes floor area to compute and output the number of
6-inch square tiles needed to tile the floor.
5. a. Draw a flowchart or write pseudocode to represent the logic of a program that
allows the user to enter values for the width and length of a wall in feet. The program
outputs the area of the wall in square feet.
b. Modify the program that computes wall area to allow the user to enter the price of a
gallon of paint. Assume that a gallon of paint covers 350 square feet of a wall. The
program outputs the number of gallons needed and the cost of the job. (For this
exercise, assume that you do not need to account for windows or doors, and that you
can purchase partial gallons of paint.)
c. Modify the program that computes paint cost to allow the user to enter the number
of doorways that do not have to be painted. Assume that each doorway is 14 square
feet. Output the number of gallons needed and the cost of the job.
6. Research current rates of monetary exchange. Draw a flowchart or write pseudocode
to represent the logic of a program that allows the user to enter a number of dollars
and convert it to Euros and Japanese yen.
7. Draw a flowchart or write pseudocode to represent the logic of a program that allows
the user to enter values for a salesperson’s base salary, total sales, and commission
rate. The program computes and outputs the salesperson’s pay by adding the base
salary to the product of the total sales and commission rate.
8. A consignment shop accepts a product for sale and sets an initial price. Each month
that the item doesn’t sell, the price is reduced by 20 percent. When the item sells, the
item’s owner receives 60 percent of the sale price, and the shop gets 40 percent. Draw
a flowchart or write pseudocode to represent the logic of a program that allows the
user to enter an original product price. The output is the sale price, the owner’s cut,
and the shop’s cut each month for the first three months the item is on sale.
9. A mobile phone app allows a user to press a button that starts a timer that counts
seconds. When the user presses the button again, the timer stops. Draw a flowchart or
write pseudocode that accepts the elapsed time in seconds and displays the value in
minutes and seconds. For example, if the elapsed time was 130 seconds, the output
would be 2 minutes and 10 seconds.
10. Explain why each of the following names does or does not seem like a good variable
name to you.
a. d
b. dsctamt
c. discountAmount
d. discount Amount
e. discount
f. discountAmountForEachNewCustomer
g. discountYear2015
h. 2015Discountyear
11. If productCost and productPrice are numeric variables, and productName is a string
variable, which of the following statements are valid assignments? If a statement is
not valid, explain why not.
a. productCost = 100
b. productPrice = productCost
c. productPrice = productName
d. productPrice = "24.95"
e. 15.67 = productCost
f. productCost = $1,345.52
g. productCost = productPrice - 10
h. productName = "mouse pad"
i. productCost + 20 = productPrice
j. productName = 3-inch nails
k. productName = 43
l. productName = "44"
m. "99" = productName
n. productName = brush
o. battery = productName
p. productPrice = productPrice
q. productName = productCost
12. Assume that income = 8 and expense = 6. What is the value of each of the following
expressions?
a. income + expense * 2
b. income + 4 – expense / 2
c. (income + expense) * 2
d. income – 3 * 2 + expense
e. 4 * ((income – expense) + 2) + 10
13. Design an algorithm that prompts the user to enter his or her height and stores the
user’s input in a variable named height.
14. Design an algorithm that prompts the user to enter his or her favorite color and stores
the user’s input in a variable named color.
15. Write assignment statements that perform the following operations with the variables
a and b.
a. Adds 2 to a and stores the result in b
b. Multiplies b times 4 and stores the result in a
c. Divides a by 3.14 and stores the result in b
d. Subtracts 8 from b and stores the result in a
16. Assume the variables result, x, y, and z are all integers, and that x = 4, y = 8, and z =
2. What value will be stored in result in each of the following statements?
a. Set result = x + y
b. Set result = z * 2
c. Set result = y / x
d. Set result = y – z
17. Write a pseudocode statement that declares the variable cost so it can hold real
numbers.
18. Write a pseudocode statement that declares the variable total so it can hold integers.
Initialize the variable with the value 0.
19. Write a pseudocode statement that assigns the value 27 to the variable count.
20. Write a pseudocode statement that assigns the sum of 10 and 14 to the variable total.
21. Write a pseudocode statement that subtracts the variable downPayment from the
variable total and assigns the result to the variable due.
22. Write a pseudocode statement that multiplies the variable subtotal by 0.15 and
assigns the result to the variable totalFee.
23. If the following pseudocode were an actual program, what would it display?
Algorithm compute1;
VAR a, b, c, result: Integer;
Begin
| a5;
| b2;
| c3;
| resulta+b*c;
| Write(result);
End
24. If the following pseudocode were an actual program, what would it display?
Algorithm compute2;
VAR num: Integer;
Begin
| num99;
| num5;
| Write(num);
End
1. If the following pseudocode were an actual program, why would it not display the output that the
programmer expects?
Algorithm debug1;
VAR favoriteFood: String;
Begin
| Write(“What is the name of your favorite food?”);
| Read(favoriteFood);
| Write(“your favorite food is”);
| Write(“favoriteFood”);
End
For each of the following Boolean expressions, decide whether the statement is true, false, or illegal.
a) numberRed = numberBlue?
b) numberBlue > numberGreen?
c) numberGreen < numberRed?
d) numberBlue = wordBlue?
e) numberGreen = "Green"?
f) wordRed = "Red"?
g) wordBlue = "Blue"?
h) numberRed <= numberGreen?
i) numberBlue >= 200?
j) numberGreen >= numberRed + numberBlue?
k) numberRed > numberBlue AND numberBlue < numberGreen?
l) numberRed = 100 OR numberRed > numberBlue?
m) numberGreen < 10 OR numberBlue > 10?
n) numberBlue = 30 AND numberGreen = 300 OR numberRed = 200?
2) Chocolate Delights Candy Company manufactures several types of candy. Design a flowchart or pseudocode for
the following:
a) A pseudocode or a flowchart that accepts a candy name (for example, “chocolate-covered blueberries”), price
per pound, and number of pounds sold in the average month, and displays the item’s data only if it is a best-
selling item. Best-selling items are those that sell more than 2000 pounds per month.
b) A pseudocode or a flowchart that accepts candy data continuously until sentinel value is entered and displays
a list of high-priced, best-selling items. Best-selling items are defined in Exercise 2a. High-priced items are
those that sell for $10 per pound or more.
3) Pastoral College is a small college in the Midwest. Design a flowchart or pseudocode for the following:
a) A program that accepts a student’s data as follows: ID number, fi rst and last name, major field of study, and
grade point average. Display a student’s data if the student’s grade point average is below 2.0.
b) A program that continuously accepts students’ data until a sentinel value is entered and displays a list of all
students whose grade point averages are below 2.0.
c) A program for the Literary Honor Society that continuously reads student data and displays every student who
is an English major with a grade point average of 3.5 or higher.
4) The Summerville Telephone Company charges 10 cents per minute for all calls outside the customer’s area code
that last over 20 minutes. All other calls are 13 cents per minute. Design a flowchart or pseudocode for the
following:
a) A program that accepts the following data about one phone call: customer area code (three digits), customer
phone number (seven digits), called area code (three digits), called number (seven digits), and call time in
minutes (four digits). Display the calling number, called number, and price for the call.
b) A program that continuously accepts data about phone calls until a sentinel value is entered, and displays all
the details only about calls that cost over $10.
c) A program that continuously accepts data about phone calls until a sentinel value is entered, and displays
details only about calls placed from the 212 area code to the 704 area code that last over 20 minutes.
d) A program that prompts the user for a three-digit area code from which to select phone calls. Then the
program continuously accepts phone call data until a sentinel value is entered, and displays data only for
phone calls placed to or from the specified area code.
5) Diana Lee, a supervisor in a manufacturing company, wants to know which employees have increased their
production this year over last year. These employees will receive certificates of commendation and bonuses.
Design a flowchart or pseudocode for the following:
a) A program that continuously accepts each worker’s first and last names, this year’s number of units produced,
and last year’s number of units produced. Display each employee with a message indicating whether the
employee’s production has increased over last year’s production.
b) A program that accepts each worker’s data and displays the name and a bonus amount. The bonuses will be
distributed as follows:
If this year’s production is greater than last year’s production and this year’s production is:
LOOPING
1) What is output by each of the pseudocode segments in Figure 5-22?
2) Design the logic for a program that outputs every number from 1 through 10.
3) Design the logic for a program that outputs every number from 1 through 10 along with its square and
cube.
4) Design the logic for a program that outputs every even number from 2 through 30.
5) Design the logic for a program that outputs numbers in reverse order from 10 down to 1.
6) a. The No Interest Credit Company provides zero-interest loans to customers. Design an application that
gets customer account data, including an account number, customer name, and balance due. Output the
account number and name, then output the customer’s projected balance each month for the next 10
months. Assume that there is no finance charge on this account, that the customer makes no new
purchases, and that the customer pays off the balance with equal monthly payments, which are 10
percent of the original bill.
b. Modify the No Interest Credit Company application so it executes continuously for any number of
customers until a sentinel value is supplied for the account number.
7) a. The Some Interest Credit Company provides loans to customers at 1.5 percent interest per month.
Design an application that gets customer account data, including an account number, customer name,
and balance due. Output the account number and name; then output the customer’s projected balance
each month for the next 10 months. Assume that when the balance reaches $10 or less, the customer can
pay off the account. At the beginning of every month, 1.5 percent interest is added to the balance, and
then the customer makes a payment equal to 5 percent of the current balance. Assume the customer
makes no new purchases.
b. Modify the Some Interest Credit Company application so it executes continuously for any number of
customers until a sentinel value is supplied for the account number.
8) Secondhand Rose Resale Shop is having a seven-day sale during which the price of any unsold item
drops 10 percent each day. For example, an item that costs $10.00 on the first day costs 10 percent less,
or $9.00, on the second day. On the third day, the same item is 10 percent less than $9.00, or $8.10.
Design an application that allows a user to input a price until an appropriate sentinel value is entered.
Output is the price of each item on each day, one through seven.
9) Mr. Roper owns 20 apartment buildings. Each building contains 15 units that he rents for $800 per
month each. Design the application that would output 12 payment coupons for each of the 15 apartments
in each of the 20 buildings. Each coupon should contain the building number (1 through 20), the
apartment number (1 through 15), the month (1 through 12), and the amount of rent due.
10) a. Design a program for the Hollywood Movie Rating Guide, in which users continuously enter a value
from 0 to 4 that indicates the number of stars they are awarding to the Guide’s featured movie of the
week. The program executes continuously until a user enters a negative number to quit. If a user enters a
star value that does not fall in the correct range, reprompt the user continuously until a correct value is
entered. At the end of the program, display the average star rating for the movie.
b. Modify the movie-rating program so that a user gets three tries to enter a valid rating. After three incorrect
entries, the program issues an appropriate message and continues with a new user.
c. Modify the movie-rating program so that the user is prompted continuously for a movie title until
“ZZZZZ” is entered. Th en, for each movie, continuously accept starrating values until a negative number is
entered. Display the average rating for each movie.
CHAPTER 3 ARRAYS
1) a. Design the logic for a program that allows a user to enter 10 numbers, then displays them in the
reverse order of their entry.
b. Modify the reverse-display program so that the user can enter up to 10 numbers until a sentinel value is
entered.
2) a. Design the logic for a program that allows a user to enter 10 numbers, then displays each number and
its difference from the numeric average of the numbers entered.
b. Modify the program in Exercise 2a so that the user can enter up to 10 numbers until a sentinel value is
entered
3) a. The city of Cary is holding a special census. The city has collected data on cards that each hold the
voting district and age of a citizen. The districts are numbered 1 through 22, and residents’ ages range
from 0 through 105. Design a program that allows a clerk to go through the cards, entering the district
for each citizen until an appropriate sentinel value is entered. The output is a list of all 22 districts and
the number of residents in each.
b. Modify Exercise 3a so the clerk enters both the district and age on each card. Produce a count of the
number of residents in each of the 22 districts and a count of residents in each of the following age groups:
under 18, 18 through 30, 31 through 45, 46 through 64, and 65 and older.