Algorithm Design and Problem Solving

revision
assignment

Variables allow us to store and process data. We can assign data to variables:

What are the datatypes of these 5 variables?

algorithms

We have studied a little programming.

Problems are solved by computer programs step by step. That means there is a sequence of defined steps to solve a problem. This is the definition of an algorithm.

Here is an example of a simple algorithm, written in pseudocode. Can you imagine what it does?

iteration i

Imagine we wanted to do the above process 10 times.

We know that we want to repeat the process 10 times. So, use a FOR loop. This is known as a count-controlled loop. The variable i is controlling the count.

iteration ii

Imagine if we are not sure that we want a process to be repeated, not even once. For example, we will repeat the process but only while the user doesn't enter 0:

Imagine that the user inputs 0 first. What would happen?

What would happen if the user entered 6 then 3 then -1 then 0?

Note: can you find the condition controlling the loop? It occurs before the code inside the loop. So, this kind of loop is known as a pre-condition loop.

iteration iii

Imagine we know that we want a process to happen at least once, after which it may iterate again, depending on the condition.

Imagine that the user inputs 0 first. What would happen?

What would happen if the user entered 6 then 3 then -1 then 0?

Note: can you find the condition controlling the loop? It occurs after the code inside the loop. So, this kind of loop is known as a post-condition loop.


selection

We can select a path through a program by using a CASE OF OTHERWISE ENDCASE structure:

Can you see how it works? Remember, this is pseudocode. A Python programmer would probably use a series of IF-ELSE statements to implement this idea. Python does not have CASE statements in its language. A Pascal programmer would be familiar with CASE statements as this is a key word in the Pascal language.

selection ii

We can select a path through a program using IF ELSE statements too.:

Remember, every ELSE statement must have an IF, but not every IF needs an ELSE!

sequence i

What if we wanted to do the above process 10 times?

counting i

A user is going to enter 1000 numbers into a computer system. The system will count how many times the number 3 is entered:


counting ii

The same process as above will happen but the program will end when the user enters -1:


totaling i

A checkout program in a supermarket inputs price data and adds it to a total. At the start of the process the total is zero. This process repeats until the cashier enters -1


Lucky customers!!!

If a programmer coded the above algorithm and the supermarket used the program, the customers would be very happy!

Can you see the problem?

Can you think of a solution?


ARRAYS

If you want to review the concept of arrays, click here!

Past Paper Exercises

Study Tip - Attempt each set before watching solution videos.

Task Set 1

Q2 and Q4

Task Set 2

Q2, Q3, Q5, Q6

Task Set 3     

Q2, Q3, Q4 and Q5

Task Set 4

Q2, Q3, Q4 and Q5

   video solution available upon request

Tags

unethicalcopyrightfreeware shareware malware hack creative commons disrupt illicit


SUBSCRIBE

Join my mailing list to receive updates on the latest blog posts and other things.