0% found this document useful (0 votes)
11 views8 pages

Coding - Part 3 - CYU

Uploaded by

Dhaval Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Coding - Part 3 - CYU

Uploaded by

Dhaval Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CODING – PART 3

BIG IDEAS:
• Loops provide an efficient way to perform repeated calculations
• for loops repeat a specified number of times
• while loops repeat as long as a specified condition is met

LEARNING GOALS AND SKILL DEVELOPMENT:


You know you have met the goals for this lesson when you can:

ANCHOR SKILL BUILDING


LEARNING GOALS
QUESTIONS QUESTIONS
Predict the output of a program involving a for loop 1 1
EMERGING

Predict the output of a program involving a while loop 1

Explain the purpose of incrementing a variable in a loop 1

ANCHOR SKILL BUILDING


LEARNING GOALS
QUESTIONS QUESTIONS
Differentiate between a for loop and a while loop 2 2 3 4 5
EVOLVING

Write programs involving for loops and while loops 2, 4

Write and use a program involving a loop to solve a problem 4

ANCHOR SKILL BUILDING


LEARNING GOALS
QUESTIONS QUESTIONS
Write and use a program involving a loop to solve a problem for
7 6 7
which an algebraic model is not provided
EXTENDING

Coding – Part 3
BUILD YOUR SKILLS
1. Consider the two programs shown below.

PROGRAM A PROGRAM B
Start loop Set x = 0 and y = 0
Use 5 consecutive integers for
x, starting at 0. Start loop
Repeat as long as y ≤ 20.
Set y = 2*x + 7
Set y = 2*x + 7
Print y
Print y
End loop
Set x = x + 1

End loop

a) How many values will be printed when Program A is run?


b) In Program B, what is the purpose of the “Set x = x + 1” step?
c) Write the output of each program.
d) What would happen if the first block of Program B was left out? Explain.
e) What would happen if the “Set x = x + 1” step of Program B was omitted? Explain.

2. a) Briefly explain the difference between a for loop and a while loop.

b) Use Python to create each program in question #1.

Coding – Part 3
3. Consider the Python program shown below.

a) What is the purpose of this program?


b) What is the purpose of the variable x?
c) Is line #2 necessary for the program to run? Explain.
d) Explain what is happening in line #7.
e) What would happen if line #7 was omitted from the program? Explain.

4. The height of a ball (in metres), t seconds after it is thrown, is given by the equation
h = −4.9t 2 + 22.3t + 2.1 .

a) Create a Python program that uses a for loop to determine the


height of the ball every second for 0 ≤ t ≤ 5 . Be sure to display
the time values alongside the corresponding height values.
Display all height values rounded to one decimal place.
b) Is the relationship between time and height linear or non-linear?
c) Using a while loop, modify your program from part (a) to print
the height of the ball every 0.5 seconds for 0 ≤ t ≤ 5 .

d) Modify your program from part (c) to print the height of the ball every 0.1 seconds, from
t = 0 until the time the ball hits the ground. Round the height values to two decimal places.

e) Using the output of your program from part (d), state the maximum height of the ball and
the time it takes to reach that height.

Coding – Part 3
5. Suppose we wish to create a Python program to solve equations of the form ax + b = c , where a, b
and c are known values.

a) Write a program that prompts the user to enter values for a, b and c in the above equation
and then solves for x.
b) For each of the following equations, state the values that would be entered for a, b and c in
your program and then use your program to solve the equation.
i) 4 x + 3 = 15 ii) −9.2 x + 1285.3 = −1861.1
iii) x − 14 = −38 iv) −32.5 x = −825.5
c) Will your program work for all values of a, b and c? Explain.

6. The following blocks represent the steps of a program that averages five numbers entered by the
user.

Read number from user End loop Set total = 0

Start loop Set average = total / 5 Set total = total + number


Repeat 5 times. Print average

a) Order the blocks in the correct sequence.


b) Create the program in Python and use it to calculate the average of the first five natural
numbers.
c) Modify your program from part (b) such that the user first specifies how many numbers will
be averaged.

1 1 1
7. Consider the sum 1 + + + + ...
2 4 8

a) Create a Python program to evaluate the above sum for the desired
number of terms (for example, the sum of the first 20 terms).
b) As more terms are added, what happens to the value of the sum?
1 1 1
c) Does the sum 1 + + + + ... behave similarly? Explain.
2 3 4

Coding – Part 3
CHECK YOUR UNDERSTANDING
1. a) 5 b) Increase the value of x by 1 for the next iteration of the loop.
c) PROGRAM A: PROGRAM B:

d) The program would return an error since a value of y is needed in the condition of the loop
and a value of x is needed for the calculation within the loop.
e) The program would loop indefinitely, printing an output of 7 for each iteration of the loop. The
value of x would never change and, therefore, the value of y would become fixed at 7 after it

ANSWERS
is first calculated in the loop. The value of y would never reach 20 to stop the loop.

2. a) With a for loop, the desired number of iterations is already known (for example, repeat the
loop 20 times). With a while loop, the number of iterations depends on some other criteria (for
example, repeat the loop as many times as needed until a calculated value reaches 0).
b) PROGRAM A: PROGRAM B:

3. a) The program adds ten numbers that are entered by the user.
b) x is a “dummy variable” that is only used to define the loop. x starts with a value of 0 and
increases by 1 with each iteration of the loop. The loop stops when x reaches 10 (it doesn’t
actually run for an x-value of 10 though).
c) Yes. In order to use the sum variable in the calculation in line #7, it must already be defined.
The initial value of sum should be 0 so that it does not affect the sum of the user-entered
values.
d) In line #7, the value of sum is updated by adding the number entered by the user in the current
iteration of the loop. As a result, the sum variable is a running total of all the numbers entered
by the user.
e) The value of sum would not be updated and the program would ultimately output the original
value of sum, which is 0.

Coding – Part 3
4. a)

b) non-linear (first differences are not equal)


c) d)

Note: t is rounded to one decimal place to improve


the appearance of the program’s output.
ANSWERS

e) The maximum height of the ball is approximately 27.47 m, which occurs after
approximately 2.3 seconds.

5. a)

b) i) a = 4, b = 3, c = 15 and x = 3

ii) a = −9.2, b = 1285.3, c = −1861.1 and x = 342

iii) a = 1, b = −14, c = −38 and x = −24

iv) a = −32.5, b = 0, c = −825.5 and x = 25.4

c) If the program involves division by a, an a-value of 0 cannot be used, since division by 0 is


undefined.

Coding – Part 3
6. a) Set total = 0 b)

Start loop
Repeat 5 times.

Read number from user

Set total = total + number


The average of the first five natural numbers is 3.
End loop

Set average = total / 5

Print average

ANSWERS
c)

Coding – Part 3
7. a)

b) As more terms are added, the value of the sum approaches 2.


c) No. As more terms are added, the value of the sum approaches infinity. A possible Python
program for investigating the sum is shown below.
ANSWERS
ANSWERS

Coding – Part 3

You might also like