27
Table of Contents Table of Contents 1 Unit 2: Algorithms and Programming 2 E-safety 3 How technology affects your sleep 3 Decomposition 4 Algorithms 5 Pseudocode 5 Introduction to text-based programming 6 Programming with data 6 Programming constructs 10 Advanced programming constructs 16 Incorporating existing code 18 Number analyser mini project 20 Using feedback 24 Unit 2 summary 26 Student reflection 26 End of unit quiz 27 Activity key Theory activity Interactive activity Lab activity

Unit 2: Algorithms and Programming 2

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Unit 2: Algorithms and Programming 2

Table of Contents

Table of Contents 1

Unit 2: Algorithms and Programming 2

E-safety 3

How technology affects your sleep 3

Decomposition 4

Algorithms 5

Pseudocode 5

Introduction to text-based programming 6

Programming with data 6

Programming constructs 10

Advanced programming constructs 16

Incorporating existing code 18

Number analyser mini project 20

Using feedback 24

Unit 2 summary 26

Student reflection 26

End of unit quiz 27

Activity key

Theory activity

Interactive activity

Lab activity

Page 2: Unit 2: Algorithms and Programming 2

Unit 2: Algorithms and Programming

Answer Key

Page 3: Unit 2: Algorithms and Programming 2

E-safety

How technology affects your sleep

Noor is learning about how technology can affect sleep.

Explain how technology can affect sleep.

Use the words to fill in the blank spaces.

active many sleep chemicals screen blue

In modern society, you may spend many hours using computers and other devices for

communication, productivity and recreation. But these devices can affect your sleep.

When you use your phones or other devices, your brain gets excited, works harder and

starts to use more energy. If your brain is active, it will be more difficult for you to

sleep.

There are chemicals in the body that help control the wake and sleep cycle. These

chemicals change when it is day or night. During the day, the chemicals make you feel

awake, and at night the chemicals make you feel sleepy.

The blue light from device screens can influence the chemicals in your body. When you

look at a screen, your brain thinks it is daytime and does not want to sleep. Some

devices have an option to remove the blue light at night. However, the best thing to do

is to stop using your devices late at night.

Explain one way you can stop technology affecting your sleep.

Write your answer below.

No teacher answers provided for open ended questions.

Activity 1

Page 4: Unit 2: Algorithms and Programming 2

Decomposition

Ahmed is learning about decomposition, demonstrate decomposition.

Apply decomposition to break down the process of a plane taking off into sub-

problems and/or smaller parts.

No teacher answers provided for open ended questions.

Activity 2

Page 5: Unit 2: Algorithms and Programming 2

Algorithms

Pseudocode

Noor is learning how to create pseudocode algorithms, demonstrate how to create

pseudocode algorithms.

Creating a pseudocode algorithm for the process of a plane taking off.

No teacher answers provided for open ended questions.

Number Pseudocode instruction

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Activity 3

Page 6: Unit 2: Algorithms and Programming 2

Introduction to text-based programming

Programming with data

Activity 4

Ahmed is learning about variables. He wants to create a program that will store the

users name, age, height and citizenship status.

Identify the data type and suggest appropriate variable names for the data to be used

in the program.

Data Example Data type Appropriate

variable name

Name

“Noor” String str_Name

Age 14 Integer int_Age

Height 123.5 Float flt_Height

Citizenship status True Boolean bln_citizen

Accept any other appropriate answers for the variable names.

Page 7: Unit 2: Algorithms and Programming 2

Activity 5

Noor is learning about variables, demonstrate how to use appropriately named

variables.

Create a text-based data storage program, then execute the program.

The program will:

• use appropriately named variables to store your name and age

• use print commands to output your name and age

Challenge

Add more code to the program to:

• use appropriately named variables to store your height and citizenship status

• use a print commands to output your height and citizenship status

Teacher Answers

str_name = "Ahmed"

int_age = 12

print(str_name)

print(int_age)

#Challenge

flt_height = 123.5

bln_citizen = True

print(flt_height)

print(bln_citizen)

}

Page 8: Unit 2: Algorithms and Programming 2

Activity 6

Ahmed is learning about inputs and outputs, demonstrate how to use inputs and

outputs with a variable.

Create a text-based greeting program, then execute the program.

The program will:

• use an input() command to input a name

• store the name using an appropriately named variable

• use print commands for output

• output “Hello”

• output the name stored in the variable

Challenge

Add more code to the program to:

• Use a + operator to join the strings for output for example: “Hello Ahmed”

Teacher Answers

str_name = input("what is your name? ")

print("Hello")

print(str_name)

#Challenge

print("Hello " + str_name)

}

Did you know?

The + (plus) operator can be used to join string values. Joining string values is also

called concatenation. Here is an example of joining strings for output:

print("Hello " + str_Name)

Page 9: Unit 2: Algorithms and Programming 2

Activity 7

Ahmed is learning about data type casting and mathematical operators, demonstrate

how to use data type casting and mathematical operators.

Create a text-based next birthday program, then execute the program.

The program will:

• input your age using an input() command.

• use an appropriately named variable to store your age.

• use a casting command to convert your age to a number.

• calculate your age on your next birthday.

• use a casting command to format the output for example: “On your birthday you

will be 14”

Challenge

Add more code to the program to:

• input your name using an input() command.

• use appropriately named variables to store your name and number of characters

in it.

• calculate the number of characters in your name using a len() command.

• use a casting command to format the output for example: “Noor, your name has

4 characters.”

Teacher Answers

int_age = input("what is your age? ")

int_age = int(int_age)

int_next_age = int_age + 1

print("On your birthday you will be " + str(int_next_age))

#Challenge

str_name = input("what is your name? ")

int_characters = len(str_name)

print(str_name + ", your name has " + str(int_characters) + "

characters.")

}

Page 10: Unit 2: Algorithms and Programming 2

Programming constructs

Activity 8

Noor is reviewing sequence constructs, demonstrate how to use a sequence construct.

Create a text-based program to output vowels from the English alphabet using

sequence, then execute the program.

The program will:

• use print commands for output

• output “A”

• output “E”

• output “I”

Challenge

Add more code to the program to:

• output “O”

• output “U”

Teacher Answers

print("A")

print("E")

print("I")

#Challenge

print("O")

print("U")

}

Page 11: Unit 2: Algorithms and Programming 2

Activity 9

Noor is learning about conditional statements, demonstrate how to use relational

operators for conditional statements

For each example, write a conditional statement using appropriate relational operators.

Example Conditional statement

Student age is less than 21. int_age < 21

User height is greater than 110. flt_height > 110

Test score is equal to 100. int_score == 100

Game time is less than or equal

to 0. flt_time <= 0

Citizenship status is equal to

True. bln_citizen == True

Page 12: Unit 2: Algorithms and Programming 2

Activity 10

Ahmed is reviewing selection constructs, demonstrate how to use a selection construct.

Create a text-based number comparer program, then execute the program.

The program will:

• use conditional statements to compare two numbers

• use print commands for output

• output which number is highest

Challenge

Add more features to the program to:

• output if the numbers are equal

Teacher Answers

int_num1 = 15

int_num2 = 10

if int_num1 > int_num2:

print("First number is highest")

if int_num2 > int_num1:

print("Second number is highest")

#Challenge

if int_num1 == int_num2:

print("Numbers are equal")

}

Page 13: Unit 2: Algorithms and Programming 2

Activity 11

Noor is reviewing repetition constructs, demonstrate how to use repetition constructs.

Create a text-based counting program, then execute the program.

The program will:

• use a count-controlled loop.

• use a print command for output.

• output a count from 1 to 10.

Challenge

Add more features to the program to:

• use a conditional loop.

• use a print command for output.

• output a count from 1 to 20.

Teacher Answers

for A in range(1, 11):

print(A)

A = 1

while A < 21:

print(A)

A = A+1

}

Page 14: Unit 2: Algorithms and Programming 2

Activity 12

Ahmed is reviewing repetition constructs. Demonstrate how programs using

conditional loops can repeat a different number of times.

1. First, create a program that uses a conditional loop:

1. sum = 0

2. while sum < 10:

3. num = input (“Enter a number:”)

4. num = int (num)

5. sum = sum + num

6. print (“Sum is: “, sum)

2. Execute the program and enter 1 each time you are asked to enter a number. Then,

answer the questions. Write your answers below.

• How many times are you asked to enter a number? 10

• How many times is the while loop executed? 10

• Why did it stop? Sum > 10

3. Execute the program again and enter 5 each time you are asked to enter a

number. Then, answer the questions. Write your answers below.

• How many times are you asked to enter a number? 2

• How many times is the while loop executed? 2

• Why did it stop? Sum > 10

4. Execute the program again and enter 23 each time you are asked to enter a

number. Then, answer the questions. Write your answers below.

• How many times are you asked to enter a number? 1

• How many times is the while loop executed? 1

• Why did it stop? Sum > 10

Page 15: Unit 2: Algorithms and Programming 2

Activity 13

Ahmed is learning about how pseudocode algorithms can be used to create text-based

programs. Demonstrate how to use a pseudocode algorithm to create a text-based

program

Create a simple name and age program using the pseudocode algorithm, then execute

the program.

Pseudocode Algorithm

1. Input a name from the user.

2. Input an age from the user.

3. Output the name.

4. Output the age.

Teacher Answers

str_name = input("what is your name? ")

int_age = input("what is your age? ")

print(str_name)

print(int_age)

}

Page 16: Unit 2: Algorithms and Programming 2

Advanced programming constructs

Ahmed is learning about logical operators in compound conditionals

Complete the truth tables for the logical operators.

Truth table for the “AND” logical operator:

(conditional statement A) AND (conditional statement B)

Result of conditional

statement A

Result of conditional

statement B

Result of compound conditional

statement for “AND”

False False False

False True False

True False False

True True True

Truth table for the “OR” logical operator:

(conditional statement A) OR (conditional statement B)

Result of conditional

statement A

Result of conditional

statement B

Result of compound conditional

statement for “OR”

False False False

False True True

True False True

Truth table for the “NOT” logical operator:

Result of conditional statement A Result of conditional statement NOT (A)

False True

True False

Activity 14

Page 17: Unit 2: Algorithms and Programming 2

Activity 15

Noor is learning about advanced programming constructs, demonstrate compound

conditionals.

Create a text-based test score evaluation program, then execute the program.

The program will:

• input a test score using an input() command.

• use a casting command to convert the test score data to a number.

• use a compound conditional to evaluate the test score.

• if test score >= 7 and test score <=10

o output “High score”

Challenge

Add more code to the program to:

• use a range of compound conditionals to evaluate the test score.

• if test score >= 4 and test score <=6

o output “Average score”

• if test score >= 1 and test score <=3

o output “Low score”

• if test score <0 or test score >10

o output “Invalid score”

Teacher Answers

int_test =input("Enter a test score ")

int_test = int(int_test)

if int_test >= 7 and int_test <=10:

print("High score")

#Challenge

if int_test >= 4 and int_test <=6:

print("Average score")

if int_test >= 1 and int_test <=3:

print("Low score")

if int_test <0 or int_test >10:

print("Invalid score")

}

Page 18: Unit 2: Algorithms and Programming 2

Incorporating existing code

Activity 16

Ahmed is learning about incorporating existing code, demonstrate how to incorporate

code from a standard programming library.

Create a text-based timer program, then execute the program.

The program will:

• import the time library.

• use a count-controlled loop.

• use a print command for output.

• use a time.sleep command to wait one second between each output.

Challenge

Add more code to the program to:

• modify the program to count down from 20 seconds.

• output “Times up” at the end of the count down.

• output the current time and date using a time.ctime() command.

Teacher Answers

import time

for A in range(1, 11):

print(A)

time.sleep(1)

#Challenge

for A in range(20, 0, -1):

print(A)

time.sleep(1)

print("Times up")

print(time.ctime())

}

Page 19: Unit 2: Algorithms and Programming 2

Activity 17

Ahmed is learning about incorporating existing code, demonstrate how to incorporate

code from a standard programming library.

Create a text-based die rolling program, then execute the program.

The program will:

• import the random library.

• use a random.randrange command to return a value between 1 and 6.

• use a variable to store the random number.

• use a casting command to format the output for example: “You rolled a die and

scored 5”

Challenge

Add more code to the program to:

• use an if command to compare random number.

• if the random number >4

o output “you win”

• else

o output “you lose”

• store “heads” and “tails as items in a list.

• use a random.choice command to output a random item from a list.

Here is an example showing the output from the program:

Teacher Answers

import random

int_random = random.randrange(1,6)

print("You rolled a die and scored " + str(int_random))

#Challenge

if int_random >4:

print("You win")

else:

print("You lose")

mylist = ["Heads", "Tails"]

print(random.choice(mylist))

}

Page 20: Unit 2: Algorithms and Programming 2

Number analyser mini project

Use what you know about the program and your decomposition skills.

Apply decomposition to break down the number analyser problem into sub-problems

and or smaller parts.

Remember, the program will allow a user to input a number, then process and output

information about the number including:

• A multiplication table.

• The amount of digits in the number.

• Whether the number is odd or even.

• The number squared (multiplied by itself).

No teacher answers provided for open ended activities.

Activity 18

Page 21: Unit 2: Algorithms and Programming 2

Use what you know about the program and your pseudocode skills.

Create a pseudocode algorithm for the number analyser program.

Remember, the program will allow a user to input a number, then process and output

information about the number including:

• A multiplication table.

• The amount of digits in the number.

• Whether the number is odd or even.

• The number squared (multiplied by itself).

No teacher answers provided for open ended activities.

Number Pseudocode instruction

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Activity 19

Page 22: Unit 2: Algorithms and Programming 2

Activity 20

Create a text-based number analyser program, then execute the program.

The program will:

• input a number using an input() command.

• use a casting command to convert the number data to a number.

• use a count-controlled loop to output a multiplication table.

• use a compound conditional to identify the digits in the number.

• if number >= 0 and number <=9

o output “1 digit number”

• if number >= 10 and number <=99

o output “2 digit number”

o if number >= 100 and number <=999

o output “3 digit number”

Challenge

Add more code to the program to:

• calculate a square number (number * number).

• output the square number.

• use a modulo operator to identify if the number is odd or even.

• if number % 2 == 0

o output “Even number”

• else

o output “Odd number”

• import the random library.

• use a random.randrange command to return a value between 1 and 999 for

analysis.

• use casting commands to format the output from the program.

Page 23: Unit 2: Algorithms and Programming 2

Teacher Answers

int_num =input("Enter a number ")

int_num = int(int_num)

for A in range(1, 11):

print(A*int_num)

if int_num >=0 and int_num <=9:

print("1 digit number")

if int_num >=10 and int_num <=99:

print("2 digit number")

if int_num >=100 and int_num <=999:

print("3 digit number")

#Challenge

import random

int_num = random.randrange(1,999)

for A in range(1, 11):

print(str(A) + " x " + str(int_num) + " = " +

str(A*int_num))

if int_num >=0 and int_num <=9:

print("1 digit number")

if int_num >=10 and int_num <=99:

print("2 digit number")

if int_num >=100 and int_num <=999:

print("3 digit number")

int_square = int_num * int_num

print(int_square)

if int_num % 2 == 0:

print("Even number")

else:

print("Odd number")

}

Did you know?

The modulo (%) operator is used to calculate remainders in mathematical division. You

can use the calculation (number % 2 == 0) to identify if a number is odd or even.

Page 24: Unit 2: Algorithms and Programming 2

Using feedback

Working in pairs to complete peer reviews of the number analyser program.

Start by asking a peer to answer these three questions about your number analyser

program:

No teacher answers provided for open ended activities.

1. Identify a positive part of the program.

Write your answer below.

2. Explain a negative part of the program.

Write your answer below.

3. Explain a suggested improvement for the program.

Write your answer below.

Activity 21

Page 25: Unit 2: Algorithms and Programming 2

Then, analyse the feedback you gathered from a peer.

What improvements can you make to your number analyser program?

Write your answer below.

Now, use the information from the peer review to improve your number analyser

program.

Did you know?

Feedback is used within art, artificial intelligence, entrepreneurship science, technology,

engineering, and mathematics.

Page 26: Unit 2: Algorithms and Programming 2

Unit 2 summary

Student reflection

Activity 22

Write about what you have learned and liked in this unit.

Three things I have learned:

1. _________________________________________________________________________

2. _________________________________________________________________________

3. _________________________________________________________________________

Two things I have liked:

1. _________________________________________________________________________

2. _________________________________________________________________________

Knowledge and skills reflection

Please tick the box to show what you understand:

I applied decomposition to break down a problem into

subproblems and then into parts.

I used pseudocode to plan an algorithm for a programming

problem.

I applied appropriately names to variables that represent different

data types.

I developed a program that includes compound conditionals.

I constructed an original program incorporating existing code.

I reviewed feedback from users to help improve my program.

Teacher’s comments:

No teacher answers provided for open ended activities.

Page 27: Unit 2: Algorithms and Programming 2

End of unit quiz

Complete the quiz to check what you have learned.

Activity 23

What have you learned? Do the quiz.

1. In computer science, decomposition is breaking a problem down into sub-problems

and smaller parts.

A. True B. False

True

2. In pseudocode, each instruction is written like text-based programming code but

without using the syntax and grammar rules of actual code.

A. True B. False

True

3. ____________ are a basic method of data storage that you can use in programs. They

can store a piece of information using either text, number or Boolean data types.

A. values B. variables

C. variants D. venues

Variables

4. A compound conditional is a type of selection that uses statements with ______ or

more logical conditions

A. one B. two

C. three D. four

two

5. Using programming libraries is one way of incorporating existing code into new

programs.

A. True B. False

True