PseudoCode Conditional Branches Class Activity ICS B Spring

Pseudo-Code Conditional Branches Class Activity ICS B Spring 2015 Anham Liaqat

Exercise 1 Input Variable: X Pseudocode: Z=1 INPUT X IF X is less than 4 Z=2 END IF OUTPUT Z What is output of above program if input is a) X is 3 b) X is 4 c) X is 2

Exercise 2 Input Variable: X Pseudocode: Z=1 INPUT X IF X is less than 4 Z=2 ELSE Z=4 END IF OUTPUT Z What is output of above program if input is a) X is 1 b) X is 5 c) X is 4

Exercise 3 Input Variable: X Pseudocode: Z=1 INPUT X IF X is less than 4 Z=1 END IF IF X is equal to 3 Z=2 END IF OUTPUT Z What is output of above program if input is a) X is 3 b) X is 4 c) X is 1

Exercise 4 Input Variable: X Pseudocode: Z=1 INPUT X IF X is less than 4 Z=3 IF X is equal to 3 Z=2 END IF OUTPUT Z What is output of above program if input is a) X is 3 b) X is 4 c) X is 2

Exercise 5 Input Variables: X, Y Pseudocode: Z=1 INPUT X INPUT Y IF X is equal to 3 IF Y is greater than X Z=4 ELSE Z=5 END IF OUTPUT Z What is output of above program if input is X is 3 and Y is 4 X is 3 and Y is 2 X is 2 and Y is 3

Input Variable: X Pseudocode: INPUT X Z=1 IF X is less than 4 Z=1 ELSE IF X is equal to 3 Z=2 ELSE IF X is equal to 4 Z=3 ELSE Z=4 END IF OUTPUT Z Exercise 6 1. What is output of this program if input is X is 3 X is 4 X is 5 2. For what input in X, the output variable Z is set to 2?

Exercise 7 Write pseudocode for a program which takes a year as input (e. g. 2014) and determines whether or not it is a leap year. A leap year is a multiple of 4, and if it is a multiple of 100, it must also be a multiple of 400.

Exercise 7 Solution There are more then one solutions. This is only one of them

Exercise 8 Write and run a program that plays the game of “Rock, paper, scissors. ” In this game, two players simultaneously say (or display a hand symbol representing) either “rock, ” “paper, ” or “scissors. ” The winner is the one whose choice dominates the other. . The rules are: paper dominates (wraps) rock, rock dominates (breaks) scissors, and scissors dominate (cut) paper. You can use 1=rock, 2=paper, 3=scissors Sample Input: 1 1 Sample Output: Draw Sample Input: 1 2 Sample Output: 2 nd player wins

Exercise 8 Solution There are more then one solutions. This is only one of them
- Slides: 11