CPT-168 - Homework #10 - John Vanassen

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A control structure is a logical design that controls the order in which a set of statements executes or loops. Examples include for, while and do while loops in C++.

4.1 What is a control structure?

You use the If-Then-Else statement to write a dual alternative decision structure. (See 4.9. Again, same exact question.)

4.14 What statement do you use in pseudocode to write a dual alternative decision structure?

If number == 1 Then Display "One" Else If number == 2 Then Display "Two" Else If number == 3 Then Display "Three" Else Display "Unknown" End If End If End If

4.16 Convert the following pseudocode to an If-Then-Else If statement:

The case structure compares the value of the testExpression (usually a variable, but can be an expression) with the values that follow each Case statement from top to bottom. When it finds a Case value that matches it branches to that Case statement, executes the statements for that Case and then exits the case structure. If it doesn't find a matching Case it branches to the Default statement and executes the statements for the Default Case.

4.19 What does the case structure test, in order to determine which set of statements to execute?

A decision structure is a control structure that allows a program to perform actions only under certain conditions. The condition is tested (ex. someNumber <= 2 or color == "blue"), and performs an action or actions according to whether the condition (or conditions) evaluate(s) to TRUE or FALSE (Boolean). Example: http://tinypic.com/r/30aca47/9

4.2 What is a decision structure?

You can use nested decision structures to achieve the same results. Ex. If testExpression == oneValue Then Display "You chose item 1." Set someVar = 20 Else If testExpression == twoValue Then Display "You chose item 2." Set someVar = 30 Else If testExpression == threeValue Then Display "You Chose item2." Set someVar = 40 Else Display "Invalid choice." End If End If End If

4.20 You need to write a multiple alternative decision structure, but the language you are using will not allow you to perform the test you need in a Select Case statement. What can you do to achieve the same results?

If y==20 Then Set x = 0 End If

4.6 Write a pseudocode If-Then statement that assigns 0 to x if y is equal to 20.

https://www.scribd.com/document/318833572/CPT168-HW-10-Answer-Key

Answer key as a word doc - https://www.scribd.com/document/318833572/CPT168-HW-10-Answer-Key

In an If-Then-Else statement, the statements between Else and End If would execute only if the condition for the If portion did not evaluate as true. Examples: Set yourNumber = 33 If yourNumber > 34 Then Display "Your number is greater than 34." Else Display "Your number is not greater than 34." End If In the above pseudocode example, the value of yourNumber is set to 33. The If condition evaluates as False, therefore the Else statement would execute. Set name = "Fred" If Not (name == "Fred") Display "Hey, you're not Fred!" Else Display "Hi Fred!" End If In the second example above, name is set to Fred, therefore the If condition, Not (name == "Fred") --the same as name != "Fred" -- evaluates to False, so the Else statement would execute and display "Hi Fred!"

4.10 When you write an If-Then-Else statement, under what circumstances do the statements that appear between Else and End If execute?

If "z" < "a" Then Display "z is less than a." Else Display "z is not less than a." End If The program would display z is not less than a. The computer evaluates the condition above using ASCII value of the character z (122, or for you 01111010) to the ASCII value of the character a (97, or in binary 01100001). 122 < 97 evaluates to False.

4.11 If the following pseudocode were an actual program, what would it display?

Using relational operators you can test whether a value or expression is greater than another, whether a value or expression is less than another, whether a value or expression is greater than OR equal to another, whether a value or expression is less than OR equal to another, whether a value or expression is equal to another, whether a value or expression is NOT equal to another.

4.5 What types of relationships between values can you test with relational operators?

If sales >= 10000 Then Set commission = 0.2 End If

4.7 Write a pseudocode If-Then statement that assigns 0.2 to commission if sales is greater than or equal to 10,000.

A dual alternative decision structure has two possible paths of execution, one if the condition evaluates to true, and another if the condition evaluates to false. The dual alternative decision structure executes one statement (or set of statements) no matter what the Boolean value of the evaluation is. An example of this is the piece of flowchart shown in the answer to question 4.2.

4.8 How does a dual alternative decision structure work?

You would use the If-Then-Else statement. If someInteger < 10 Then Display "That's too low!" Else Display "That's too high!" End If

4.9 What statement do you use in pseudocode to write a dual alternative decision structure?

A Boolean expression is an expression that can be evaluated to True or False using a relational operator (>, <, >=, <=, ==, !=)

4.4 What is a Boolean expression?

Declare String s1 = "New York" Declare String s2 = "Boston" If s1 > s2 Then Display s2 Display s1 Else Display s1 Display s2 End If Boston New York So in short, the computer starts on the left and compares the first character of each string. If they're equal it moves on to the next character in each string. As soon as it gets to a character comparison that is not equal it determines greater than/less than using the ASCII values for the two characters and does not compare any further. If one string runs out of characters before another during the comparison/evaluation (so up until then the strings have been equal) the shorter one is determined to be the lesser value. Ex. "Georgetown > George" The computer would First compare the ASCII values for G and G, then for e and e, followed by o and o, r and r, g and g, e and e, and finally t and (oops, out of characters), therefore "Georgetown" > "George" would evaluate True. In the code for the question, the computer would compare the ASCII value for B (66) to the ASCII value for N (78) and determine that "Boston" < "New York". Because of the set order of the ASCII code values we (people, that is, rather than computers) can also easily look at certain string comparisons and know which is the higher value. A-Z: Capital letters all have a lower ASCII value than lowercase letters. Starting at A, each following letter has a higher value than the previous one. So A < B which is < C which is < D, etc. The same goes for a-z (lowercase) a < b < c, d etc. Any capital letter has a lower ASCII value than any lowercase letter. A number (talking about string comparisons by character, so 0-9) has a lower value than any alphabetic character (referring only to the American alphabet/ASCII code)

4.12 If the following pseudocode were an actual program, what would it display?

A dual alternative decision structure has two possible paths of execution. One path is taken if the condition evaluates True, the other if the condition evaluates False. See the answer to 4.8. It's the same question!

4.13 How does a dual alternative decision structure work?

In an If-Then-Else statement, the statements between Else and End If would execute only if the condition for the If portion did not evaluate as true. See the answer to 4.10 for examples.

4.15 When you write an If-Then-Else statement, under what circumstances to the statements that appear between the Else clause and the End If clause execute?

A multiple alternative decision structure allows you to test the value of a variable or expression and then use that value to determine which statement or statements to execute. The case or switch structure is a multiple alternative decision structure. Example: https://postimg.org/image/xsgcke9pt/ The flowchart snippet above would work well for a program in which the user was presented with a menu and prompted to enter a menu choice: 1. Compute perimeter 2. Compute area 3. Compute diameter Enter 1, 2, or 3: The Default pathway could be a message displaying "Invalid input" or a statement to exit the program.

4.17 What is a multiple alternative decision structure?

Select (testExpression) Case oneValue Display "You chose item 1." Set someVar = 20 Case twoValue Display "You chose item 2." Set someVar = 30 Case threeValue Display "You Chose item2." Set someVar = 40 Default: Display "Invalid choice." End Select

4.18 How do you write a multiple alternative decision structure in pseudocode?

A single alternative decision structure is one which provides only one alternative path of execution. For example: If color == "blue" Then // color being a string variable in this case Display "blue" End If In the above case either the condition is True (the value of the variable color IS "blue") and the program displays "blue". Otherwise (if the condition is not true) the decision structure is exited with no action taken. It can also work the opposite way with the single path of execution being performed if the condition evaluates to False, otherwise (if it evaluates to true) the decision structure would be exited with no action taken.

4.3 What is a single alternative decision structure?


Ensembles d'études connexes

Social Studies Weekly Quarter 3, Week 20

View Set

Chapter 15 - Stockholders Equity

View Set

CRC Foundations - Clinical Research Coordinator (CRC) Responsibilities

View Set

Themes and Resolution in Romeo and Juliet, Part 8

View Set

Module 3 - Aqueous Solutions and Reactions in Water

View Set