Question 1 Write pseudocode for an algorithm that (1) asks the user to input the length of the side of a square, (2) computes the square’s area, and (3) prints the square’s area. Use the following sample session. Sample session: Enter length of side of square in meters: 15 The area of the square is 225 square meters.
Question 2 (a) What is an infinite loop? (b) Given the following pseudocode, circle the statements that are considered to be within the body of the while loop: input time while time is less than 8 print time set time to time _ 1 (c) Suppose the user’s input for time is 3. How many lines of output will the algorithm generate?
Question 3 Trace the following algorithm. The book presents two ways to do tracing—a short form and a long form. To give you a head start, the setup for the short form and also the long form are given below. 1 set y to 0 2 input x 3 while x is not equal to y 4 set y to value of x 5 input x 6 set x to x _ y 7 print “x = ” x 8 print “y = ” y Short-form setup: input x 2 3 4 0
y
Long-form setup: Input-2,3,4,0 input Line # x
Fall 2013
output
y
output
Ms. Gayani Gupta
Page 2 of 2 Handout #5
Computer Science –CS 1050
Question 5 Trace the following algorithm. The lecture handout #4 presents two ways to do tracing—a short form and a long form. To give you a head start, the setup for the short form and also the long form are given below. 1 set num to 2 2 set count to 1 3 while count is less than 5 4 set count to count * num 5 if count / 2 is less than 2 6 print “Hello” 7 else 8 while count is less than 7 9 set count to count _ 1 10 print “The count is” count “.” Short-form setup: num count
output
Long-form setup: Line #
num
count
output
Extension activity Think the similarities and differences between While..End while loop and Repeat..until loop.
Use a suitable example of your choice for your explanation.