9341882

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

PT1420

Unit 5 Assignment 1 Homework

Short Answers 1-7 Page 158

1. Explain what is meant by the term “conditionally exectuted.”

It is when an action is executed only when a certain condition is true.

2. You need to test a condition and then execute one set of statements if the condition is
true. If the condition is false, you need to execute a different set of statements. What
Structure will you use? Dual alternative decision structure.

3. If you need to test the value of a variable and use that value to determine which
statement or set of statements to execute, which structure would be the most
straightforward to use?

Case Structure

4. Briefly describe how the AND operator works.

The AND operator creates a compound Boolean expression that is only true when
when both subexpressions are true.

5. Briefly describe how the OR operator works.

The OR operator creates a compound Boolean expression that is true when either
of the subexpressions is true.

6. When determining whether a number is inside a range, which logical operator is it best
to use?

The AND operator

7. What is a flag and how does it work?

A flag signals when some condition exists in the program. If it’s set to false, then
the condition does not exist. If true then it does exist.
PT1420
Unit 5 Assignment 1 Homework
PT1420
Unit 5 Assignment 1 Homework

Algorithm Workbench Review Questions 1-5 from page 158-159

1. Design an If-Then statement (or a flowchart with a single alternative decision structure)
that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is
greater than 100

IF x > 100 then

Set y=20

Set z=40

End If

2. Design an If-Then statement (or a flowchart with a single alternative decision structure)
that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less
than 10.

IF a < 10

Set b=0

Set c=1

End IF

3. Design an If-Then-Else statement (or a flowchart with a dual alternative decision


structure) that assigns 0 to the variable b if the variable a is less than 10. Otherwise it
should assign 99 to the variable b.

IF a < 10 then

Set b=0

Else if a =>10 then

Set b=99

End IF

4. The following pseudocode contains several nested If-Then-Else statements.


Unfortunately, it was written without proper alignment and indentation. Rewrite the code
and use the proper conventions of alignment and indentation.

Display “Enter your Test Score”

Input Score

IF score < 60 then


PT1420
Unit 5 Assignment 1 Homework

Display “Your grade is F.”

Else IF score < 70 Then

Display “Your grade is D.”

Else IF score < 80 then

Display “Your grade is C.”

Else IF score < 90 then

Display “Your grade is B.”

Else

Display “Your grade is A.”

End IF

5. Design nested decision structures that performs the following: If amount1 is greater than
10 and amount2 is less than 100, display the greater of amount1 and amount2

IF amount1 >10 and amount2 <100 then

IF amount1 > amount2 then

Display amount1

Else

Display amount2

End IF

(this does not account for if the numbers are == to eachother)


PT1420
Unit 5 Assignment 1 Homework

Programming exercises 1-3 from page 159

1. Roman Numerals

Design a program that prompts the user to enter a number within the range of 1 through
10. The program should display the Roman numeral version of that number. If the
number is outside the range of 1 through 10, the program should display an error
message.

Declare int Number = 0

Display “Enter a number 1-10”

Set Number

Select Number

Case 1

Display “The Roman numeral for 1 is I”

Case 2

Display “The Roman numeral for 2 is II”

Case 3

Display “The Roman numeral for 3 is III”

Case 4

Display “The Roman numeral for 4 is IV”

Case 5

Display “The Roman numeral for 5 is V”

Case 6

Display “The Roman numeral for 6 is VI”

Case 7

Display “The Roman numeral for 7 is VII”

Case 8

Display “The Roman numeral for 8 is VIII”

Case 9
PT1420
Unit 5 Assignment 1 Homework

Display “The Roman numeral for 9 is IX”

Case 10

Display “The Roman numeral for 10 is X”

Default :

Display “You have not entered a number 1 through 10”

End select
PT1420
Unit 5 Assignment 1 Homework

2. Areas of Rectangles

The area of a rectangle is the rectangle’s length times its width. Design a program that
asks for the length and width of two rectangles. The program should tell the user which
rectangle has the greater area, or if the areas are the same.

Declare real length1 = 0

Declare real height1 = 0

Declare real length2 = 0

Declare real height2 = 0

Declare real Rec1 = 0

Declare real Rec2 = 0

Display “What is the width of Rectangle 1?”

Input length1

Display “What is the height of Rectangle 1?”

Input height1

Display “What is the width of Rectangle 2?”

Input length2

Display “What is the height of Rectangle 2?”

Input height2

Set Rec1 = length1 * height1

Set Rec2 = length2 * height2

Display Rec1

Display Rec2

If Rec1 > Rec2 then


PT1420
Unit 5 Assignment 1 Homework

Display “The area of Rectangle 1 is larger than that of Rectangle 2”

Else if Rec1 < Rec2 then

Display “The area of Rectangle 2 is larger than that of Rectangle 1”

Else if Rec1 == Rec2 then

Display “The area for both rectangles is the same”

End If
PT1420
Unit 5 Assignment 1 Homework

3. Mass and weight

Scientists measure an object’s mass in kilograms and its weight in Newtons. If you know
the amount of mass of an object, you can calculate its weight, in Newtons, with the
following formula: Weight = Mass x 9.8

Design a program that asks the user to enter an object’s mass, and then calculates its
weight. If the object weighs more than 1000 Newtons, display a message indicating that
it is too heavy. If the object weighs less than 10 Newtons, display a message indicating
that it is too light.

Declare real mass = 0

Declare real newt = 0

Display “Please enter the mass of the object”

Input mass

Set newt = mass * 9.8

If newt > 1000 then

Display “The object is too heavy”

Else if newt < 10 then

Display “The object is too light”

Else

Display “The object weighs” newt “newtons”

End If
PT1420
Unit 5 Assignment 1 Homework

You might also like