Programming Logic and Design, Second Edition, Introductory



Chapter 1

Answers to Review Questions

1. c

2. b

3. b

4. d

5. a

6. c

7. a

8. c

9. b

10. d

11. a

12. d

13. b

14. c

15. a

16. c

17. a

18. d

19. b

20. c

Answers to Exercises

Match the definitions with the appropriate term.

1. Computer system equipment - d. hardware

2. Another word for programs - e. software

3. Language rules - b. syntax

4. Order of instructions - c. logic

5. Language translator - a. compiler

In your own words, describe the steps to writing a computer program.

Answer should describe the six steps in the programming process:

Understand the Problem

Plan the Logic

Code the Program

Translate the Program into Machine Language

Test the Program

Put the Program into Production

3. Would this set of data be suitable and sufficient to use to test each of the following programs? Explain why or why not.

a. A program that prints a list of Psychology majors.

Yes, the program can select and print the two records whose major field contains "Psychology".

b. A program that prints a list of Art majors.

No, there are no records whose major field contains "Art". A test would not determine if this field would process correctly.

c. A program that prints a list of students on academic probation - those with a grade point average under 2.0.

No, there are no records whose grade point average field contains a value under 2.0. A test would not determine if this field would process correctly.

d. A program that prints a list of students on the dean's list.

No, we don't know what value grade point average must be to qualify for the dean's list, so this condition cannot be tested. (Students might argue that they used the dean's list standards used by their institution. This can lead to a discussion about making assumptions.)

e. A program that prints a list of students from Wisconsin.

No, there is no state field, so the program would have no way of knowing what state each student is from.

f. A program that prints a list of female students.

No, there would be no way the program could determine the gender of the student based on the data in the record. Humans could look at the current data and guess if a student was male or female, but they might be wrong. (If the file contained first names such as Terry, Chris and Pat, the task would be even more difficult.) If a gender field contained an "F" or "M", then a program could select students based on gender.

4. Suggest a good set of test data to use for a program that gives an employee a $50 bonus check if the employee has produced more than 1,000 items in a week.

Answers may vary, but should include a record where a field called something like itemsProduced is less than 1000, is 1000, and is greater than 1000. The record should also have a way to distinguish one record from another (for example, empName or empNumber). For example:

|empName |itemsProduced |

|Brown |1000 |

|Walters | 999 |

|Porter |1001 |

5. Suggest a good set of test data for a program that computes gross paychecks (that is, before any taxes or other deductions) based on hours worked and rate of pay. The program computes gross as hours times rate, unless the hours are over 40. Then the program computes gross as regular rate of pay for 40 hours, plus one and a half times the rate of pay for the hours over 40.

Answers may vary, but included in the record layout should be a field for hoursWorked and another for rateOfPay. There should be at least one record where hoursWorked is less than 40, one where it is 40, and where it is greater than 40. The record should also have a way to distinguish one record from another (empName or empNumber). For example:

|empName |hoursWorked |rateOfPay |

|Garcia |38 |12.55 |

|Patel |41 |13.50 |

|Martin |40 |23.00 |

6. Suggest a good set of test data for a program that is intended to output a student's grade point average based on letter grades (A, B, C, D, or F) in five courses.

Answers may vary, but included in the record layout should be the five grades that will be submitted for averaging. Answers may vary depending on whether the student will input numeric scores (A=4, B=3, etc.) or input letter grades, with the assumption that the program will convert the letter to a numeric score. There should be a variety of grades, and every grade should be used at least once. The record should also have a way to distinguish one record from another (for example, by studentName or studentNumber). For example:

|stuLastName |grade1 |grade2 |grade3 |grade4 |grade5 |

|Abel |A |A |A |A |A |

|Sager |F |F |F |F |F |

|Thompson |A |B |C |D |F |

|Lee |B |C |A |C |C |

7. Suggest a good set of data for a program for an automobile insurance company that wants to increase its premiums by $50 a month for every ticket a driver receives in a three year period.

Answers may vary, but included in the record layout should be the insured's premium amount and the number of tickets received. There should be at least one record where ticketsIssued is less than 3, one where it is 3, and where it is greater than 3. The record should also have a way to distinguish one record from another (insuredName or policyNumber). For example:

|insuredName |premium |ticketsReceived |

|Crandell |165.99 |0 |

|Byer |237.50 |3 |

|Harris | 89.75 |4 |

|Wu |150.00 |1 |

8. Assume that a grocery store keeps a file for inventory, where each grocery item has its own record. Two fields within each record are the name of the manufacturer and the weight of the item. Name at least six more fields that might be stored for each record. Provide an example of the data for one record. For example, for one product the manufacturer is DelMonte, and the weight is twelve ounces.

Other fields may include:

|itemName |itemNumber |price |cost |packaging |

|quantityOnHand |distributor |department |aisle |isOnSpecial |

Sample record:

itemName: Peaches

itemNumber: 12345

price: 1.99

cost: 1.70

packaging: can

quantityOnHand: 300

distributor: Acme

department: Canned Goods

aisle: 3

isOnSpecial: Y

9. Assume a library keeps a file with data about its collection, one record for each item the library lends out. Name at least eight fields that might be stored for each record. Provide an example of the data for one record.

Fields may include:

|title |author |publisher |year |numPages |

|ISBNcode |category |isOnLoan |dateDue |isOnReserve |

|normalLoanTerm |shelfNumber | | | |

Sample record:

title: The Horse Whisperer

author: Evans

publisher: Dell

year: 1995

numPages: 350

ISBNcode: 0730098712

category: Fiction

isOnLoan: Y

dateDue: 021405

isOnReserve: N

normalLoanTerm: 21

shelfNumber: 216

10. Match the term with the appropriate shape.

1-c, 2 – d, 3 – a, 4 –b, 5 – e

11. Which of the following names seem like good variable names to you? If a name doesn’t seem like a good variable name, why not?

Students can have arguments for or against most of these names. The purpose of the exercise is to provoke thought.

a. c – probably too short to be descriptive

b. cost – good, maybe to short?

c. costAmount – good, maybe redundant?

d. cost amount – invalid, space not allowed

e. cstofdngbsns – valid, but difficult to read?

f. costOfDoingBusinessThisFiscalYear – valid, but cumbersome

g. cost2004 – valid. Does 2004 represent a year, an item number, or something else?

12. If myAge and yourRate are numeric variables, and departmentCode is a character variable, which of the following statements are valid assignments? If a statement is not valid, why not?

a. myAge = 23 valid

b. myAge = yourRate valid

c. myAge = departmentCode invalid, mismatched types

d. myAge = “departmentCode” invalid, mismatched types

e. 42 = myAge invalid, constant on left of equal sign

f. yourRate = 3.5 valid

g. yourRate = myAge valid

h. yourRate = departmentCode invalid, mismatched types

i. 6.91 = yourRate invalid, constant on left of equal sign

j. departmentCode = Personnel invalid, character data needs quotes

k. departmentCode = “Personnel” valid

l. departmentCode = 413 invalid, mismatched types

m. departmentCode = “413” valid

n. departmentCode = myAge invalid, mismatched types

o. departmentCode = yourRate invalid, mismatched types

p. 413 = departmentCode invalid, constant on left of equal sign

q. “413” = departmentCode invalid, constant on left of equal sign

13. a. Draw a flowchart to represent the logic of a program that allows the user to enter a value. The program multiplies the value by 10 and prints out the result.

[pic]

b.Write pseudocode for the same problem.

start

get number

answer = number * 10

print answer

stop

14. a. Draw a flowchart to represent the logic of a program that allows the user to enter a value that represents the radius of a circle. The program calculates the diameter ( by multiplying the radius by 2), and then calculates the circumference (by multiplying the diameter by 3.14). The program prints both the diameter and the circumference.

[pic]

b. Write pseudocode for the same problem.

start

get radius

diameter = radius * 2

circumference = diameter * 3.14

print diameter, circumference

stop

15. a. Draw a flowchart to represent the logic of a program that allows the user to enter two values. The program prints the sum of the two values.

[pic]

b. Write pseudocode for the same problem.

start

get value1

get value2

answer = value1 + value2

print answer

stop

16. a. Draw a flowchart to represent the logic of a program that allows the user to enter three values. The first represents your hourly pay rate, the second represents the number of hours you worked this pay period, and the third represents the percentage of your gross salary that is withheld. The program multiplies your hourly pay rate by the number of hours worked giving the gross pay; then it multiplies the gross pay by the withholding percentage, giving the withholding amount. Finally it subtracts the withholding amount from the gross pay, giving the net pay after taxes. The program prints the net pay.

[pic]

b. Write pseudocode for the same problem.

start

get hourlyRate, hoursWorked, pctWithholding

grossPay = hourlyRate * hoursWorked

withholdingAmt = grossPay * pctWithholding

netPay = grossPay – withholdingAmt

print netPay

stop

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download