Copy of CS1121 - Lab 2 Worksheet
Copy of CS1121 - Lab 2 Worksheet
Jeremiah Branch
Adam Pawlikowski
Ean Bingaman
Odd or Even?:
1. Explain how we use the modulo (or remainder) operation to represent divisibility in
programming.
● Modulus represents divisibility by 2, if the remainder is greater than 0, the
number is odd, and not divisible by 2. If the remainder is 0, then the number is
even, and divisible by 2.
2. Revise the decision question in the flowchart to a function that uses modulo.
● We can replace the “Is number divisible by two” question with “Is the
modulo/remainder equal to 0”, if yes, the number is even. If not, the number is
odd.
3. Convert the flowchart to pseudocode form, using your answer from part 2.
1. begin
2. display “Enter a number”
3. set number to USER_INPUT
4. if number mod 2 = 0
5. display “the number is even”
6. else
7. display “the number is odd”
8. end
Guess My Number:
● The user would provide an integer guess to guess what number the program
chose.
● The program is expected to check if the user input is equal to the chosen number
(specifically, if it is higher or lower than the selected value)
● It compares the user input to the selected value using comparison operations,
and displays an answer depending on whether the guess is too high or too low
4. When does the program end? And how (if at all) is the final result shared with the user?
● It ends when the user guesses the right number, and displays confirmation that
the user is correct along with the correct number.
Critical Thinking:
1. Do you think, based on the text “between 1 and 10”, that 1 and 10 should be included as
part of the numbers that can potentially be selected, or not? What language may more
clearly convey whether the ends should or should not be included.
● 1 and 10 should be included as part of the numbers that can be potentially used.
Using greater/less than operators will convey whether the endpoints should be
included.
2. The program needs to keep track of some information for this task. What value is the
program getting prior to user input? Should this value ever change? Why or why not?
● The value the program is getting is a randomly generated number between the
specified range for the user to guess. This value should remain constant/final
because the user is trying to guess that number; it would defeat the purpose of
the program if the number we were trying to guess was constantly changing
3. Is there a value that should change as the program runs? Why or why not?
● The guess that the user enters should change each time they get it incorrect.
This way, the user can guess again and the program can check if that guess is
correct or wrong.
4. Control Flow describes the paths that programs can take. Does this program require
decisions (if), steps to be repeated (loops), or both? How do you know?
● The program will utilize a while loop that continues until the user input equals the
selected number. An if statement will be used to check if the guess is correct
each time.
5. Conditionals are either TRUE or FALSE. In this program, two conditions exist that can
give user feedback on incorrect inputs. What two conditions dictate to the user the
number is TOO HIGH or TOO LOW?
● If the user input is less than the selected number (i.e., 4 < 6) then the program
will output “TOO LOW”
● If the user input to greater than the selected number (ie., 7>6) then the program
will output “TOO HIGH”
6. There is a specific circumstance in which the program can end. What condition must be
fulfilled for it to happen?
● Finally if the input is exactly the number then the program will output
“CONGRATULATIONS” then end
Positive Thinking:
● Count the number of positive numbers and check if any are negative.
● Checks if they are negative, and if they are positive it increments the counter
4. When does the program end? And how (if at all) is the final result shared with the user?
● The program ends when the user enters a negative number, and the program
counts and displays how many positive numbers there are
Pseudocode:
1. Write out the pseudocode for level 2.
1. begin
2. display “enter a positive number”
3. set input to USER_INPUT
4. set count to 0
5. while (input > 0)
6. count + 1
7. display “enter a positive number”
8. set input to USER_INPUT
9. end while
10. display count // only indented further because of the 10)
11. end
Critical Thinking:
1. Give an example of an assumption you need to make based on the problem description.
(Hint: Is there a certain value a user might input where behavior isn’t defined?)
● We are assuming that the user is putting in numbers and not letters.
2. Does this algorithm require control flow? How do you know? What indicated it to you?
● Yes, it does, because a sequence of events needs to be followed for the program
to run correctly. For example, after collecting user input, the program THEN
checks if the number is correct.
3. There is a specific condition that allows the program to terminate. What is it?
● Yes, when the user inputs a number lower than 1 the program terminates and
displays the count
4. What is the term for what we might use to maintain and “count” the number of positives?
(Hint: What lets us store, modify, and access important values in programs?)
● We would use a “counting” integer variable that increments by one each time a
positive integer is entered