Nothing Special   »   [go: up one dir, main page]

Algorithms and Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Chapter 03

Computational Thinking
Algorithms and Programs
Slides by
Ms. Abeera Sikandar
lecturer
bahria college anchorage
computer sciences
What is an algorithm
It is a process/procedure that should be followed for some output.
Program requirement
The requirement tells you what the program must do.
For example:
Calculating a total by adding ten numbers input by the user.
Start simple
A good method is to start with a simple version of the problem. A
programmer might make a program that solves the simple
problem.
For example:
Calculate a total by adding one number input by the user.
Plan the variables
The program must have variables. They will store the values.

Scratch has ready-made variable called answer. It is ‘light blue’


sensing blocks.
It store the values input by user.
Another variable of ‘total’ will be needed to store result of total.
It has value ‘0’ first.
Create an algorithm
Algorithm: set of steps needed to solve a problem.
It works as a plan when you make a program.
Create program
Using scratch program, we’ll create a program now.
• make a variable
• Set the value of the variable
• Use ‘ask’ block to get input
• Use operators to add and join values.
• Use ‘say’ block for program input.
Loop
Now extend the program to ‘ten’ numbers input.
Program wants to add a series of numbers.
Purpose: It needs to do same task over and over again.
Loop is a program structure which will iterate number of
times.
Loop
Several types of loop from scratch are shown.
When there is a set limit, like 10 numbers to be added,
‘counter’ or ‘fixed’ loop will be used.
Revised algorithm
Previous Algorithm Revised Algorithm
Revised program
Previous Program Revised Program
Deliberate mistake:
Uncover the error:
Sequence:
How to save time:
Writing a computer program is a lot of work. So programmers reuse and repurpose their program.
• Reuse: means using the same program for a new task.
For example, program to calculate bill in restaurant will be same as program to calculate
bill in supermarket.

• Repurpose: means making some changes to a program so it does a new thing.


For example, program to add 17% sales tax to an invoice can be repurposed to add 10 %
service charges to invoice.
Advantages of reuse & repuporse:

1. Saves time and work: it takes less time and work to make a small
change in a program, rather than writing a whole new program.
2. Less risk of an error: Repurposing the program is less risky than
writing a new program.
3. Teamwork: Sharing program with others is good teamwork.
New requirement
Program to calculate average of numbers from total.
How to turn an algorithm into a program
Combining algorithms
Programs you have made
How to combine features you’ve made
1. Programs to count inputs

2. Add borrowed commands


How to combine features you’ve made
2. Add borrowed commands
QUIZ: COMBINING FEATURES OF PROGRAMS
Match each line of code with relevant
block of scratch
Set total to 0
Set counter to 0
Repeat until answer = X
Input a number
Set total to total + answer
Set counter to counter + 1
Output the total
Set average to total/counter
Output average
Output counter

You might also like