0% found this document useful (0 votes)
38 views

Lesson 2 - Repetition: Goal(s)

The document is an introduction to computer science lesson on repetition using loops. It discusses writing loops for a guessing number problem and following a loop design strategy. It provides an example problem of generating a random number between 0-100 and having the user guess it, telling them if each guess is too low or too high. The key points, assessment questions, and programming exercises are about writing loops to simulate problems involving repetition.

Uploaded by

Bell Chan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lesson 2 - Repetition: Goal(s)

The document is an introduction to computer science lesson on repetition using loops. It discusses writing loops for a guessing number problem and following a loop design strategy. It provides an example problem of generating a random number between 0-100 and having the user guess it, telling them if each guess is too low or too high. The key points, assessment questions, and programming exercises are about writing loops to simulate problems involving repetition.

Uploaded by

Bell Chan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Introduction to Computer Science 11

Lesson 2 – Repetition
Goal(s):
To write loops for the guessing number problem
To follow the loop design strategy to develop loops
To control a loop with the user confirmation or a sentinel value

Minds On…

What output is displayed by the cout in the following code snippet.

Action …
Key Point
A while loop executes statements repeatedly while the condition is true. Problem

Write a program that randomly generates an integer between 0 and 100, inclusive.
The program prompts the user to enter a number continuously until the number
matches the randomly generated number. For each user input, the program tells the
user whether the input is too low or too high, so the user can make the next guess
intelligently. Here is a sample run:
1
Introduction to Computer Science 11

Here is the algorithm for solving the problem.


1. Generate a random number between 0 and 100 inclusive.
2. Prompt the user to guess the magic number generated in step 1.
3. If the user guess is < magic number, display ‘your guess is too low’.
4. If the user guess guess is > magic number, display ‘your guess is too high’.

5. If the user guess = magic number, display ‘Yes, You got it’
1
Introduction to Computer Science 11

Assessment
1. How many times will the following code print "Welcome to C++"?
Introduction to Computer Science 11
1

2. What is the output of the following code?

3. What is the output of the following code?

Programming Exercises
4. (Simulation: Even or Odd) Write a program that generates a random integer
one
hundred thousand times (100 000) and displays the number of even and odd
integers.

5. (Occurrence of max numbers) Write a program that reads integers, finds the
largest
of them, and counts its occurrences. Assume that the input ends with number
0. Suppose that you entered 3 5 2 5 5 5 0; the program finds that the largest is
5 and the occurrence count for 5 is 4.
1
Introduction to Computer Science 11

(Hint : Maintain two variables, max and count. max stores the current max
number, and count stores its occurrences. Initially, assign the first number to max
and 1 to count. Compare each subsequent number with max. If the number is
greater than max, assign it to max and reset count to 1. If the number is equal to
max, increment count by 1.)

6. (Simulation: clock countdown) Write a program that prompts the user to enter
the
number of seconds, displays a message at every second, and
terminates when the
time expires. Here is a sample run:

You might also like