Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Problem Solving

Similar presentations


Presentation on theme: "Topic 1: Problem Solving"— Presentation transcript:

1 Topic 1: Problem Solving
Implementing Algorithms

2 Implementing Algorithms
After having created an algorithm (or being given one), we need to be able to program them Known as implementation We essentially need to be able to translate from pseudocode/flowcharts to program code We need to do this for Each construct we saw (sequencing, branching, and iteration) Every data-type we can think of (numbers, logic, and text) Every data-structure we can use (arrays, both single and multi dimensional) Problem Solving: Implementing Algorithms

3 Implementing Algorithms: Sequencing
For this, we need to be confident in Declaring variables Outputting to the console Getting user input Let’s look at how we can do that for all three languages Using the example on the right Example Say we have the following algorithm BEGIN Main() x  INPUT(‘Enter a number’) x  x * 2 OUTPUT(x) END Main We need to be able to program this in Python, C#, or Java. Each language has its own way of getting input, declaring variables, and outputting. Problem Solving: Implementing Algorithms

4 Implementing Algorithms: Sequencing
First we have Python When making variables, we don’t have to worry about any data-types We simply give them a name When outputting, we use the print() function And put what we want to put inside the brackets When inputting, we use the input() function Can also include text to output here Will appear before the cursor for the user Note the use of int() when getting input User input is technically text We convert it to an integer so we can double it Problem Solving: Implementing Algorithms

5 Implementing Algorithms: Sequencing
Next we have C# Here, we need to specify the data-type a variable uses To output, we use Console.WriteLine() We put the value to output in the brackets To input, we use Console.ReadLine() We don’t put anything in the brackets If we want to put the cursor after the text to show, use Console.Write() instead Doesn’t move on to the next line We also need to convert the text to an integer Problem Solving: Implementing Algorithms

6 Implementing Algorithms: Sequencing
Finally we have Java Follows the same rules as C# But output uses System.out.println() Input needs something called a Scanner That can read the System.in Has to be closed using close() And conversion uses Integer.parseInt() Problem Solving: Implementing Algorithms

7 Problem Solving: Implementing Algorithms
Implement the following program in your programming language of choice BEGIN Main() OUTPUT(‘Please enter the following details’) name  INPUT(‘Name:’) birthdate  INPUT(‘Date of Birth (DD/MM/YY):’) OUTPUT(‘Please check your details:’) OUTPUT(‘ Name: name’) OUTPUT(‘ Date of Birth: birthdate’) END Main Problem Solving: Implementing Algorithms

8 Implementing Algorithms: Branching
To implement a branch, we need to know how to Choose between if’s and switches Use the correct condition Work out when to use multiple if’s, or else-if’s These work largely the same across all three programming languages Except for one thing Switches are NOT in Python Example Say we have the following algorithm: BEGIN F() x  INPUT(‘Enter a number’) IF MOD(x, 2) = 0 OUTPUT(‘Even’) ELSE OUTPUT(‘Odd’) END IF END F As well as getting input, we need to use the correct syntax for checking for equality, and using if/else statements. Problem Solving: Implementing Algorithms

9 Implementing Algorithms: Branching
This is a simple affair in Python We can add a branch using the if keyword The condition goes after this keyword NOT in brackets After the condition we include a comma It’s how Python defines scope We also add the else keyword afterwards With its own comma Note the indentation after those commas Problem Solving: Implementing Algorithms

10 Implementing Algorithms: Branching
There are three differences in C# We need to include circular brackets after the if keyword For its condition We use curly braces to define scope We don’t include a comma Problem Solving: Implementing Algorithms

11 Implementing Algorithms: Branching
Java is close to C# Except for the input/output differences Other than that, they’re the same Problem Solving: Implementing Algorithms

12 Problem Solving: Implementing Algorithms
Implement the following program in your programming language of choice BEGIN Main() x  INPUT(‘Enter your height’) y  INPUT(‘Enter the ride height requirement’) IF x < y OUTPUT(‘You are not tall enough to ride’) ELSE OUTPUT(‘You are tall enough to ride’) END IF END Main Problem Solving: Implementing Algorithms

13 Implementing Algorithms: Iteration
Finally, to implement an iteration, we need to Choose between a for and a while loop Use the correct condition If using a for, use the correct counter The while loop works the same across languages It takes a single condition Repeats its scope until that condition returns false For loops work the same in C# and Java In Python, they’re a bit different Example Say we have the following algorithm BEGIN F() x  INPUT(‘Enter a number’) FOR i FROM 0 TO x OUTPUT(i) END FOR END F Although this uses a FOR loop, we can choose between while and for (as long as they ultimately do the same thing). We just need to make sure to create a counter and go from 0 to x (exclusive) no matter what. Problem Solving: Implementing Algorithms

14 Implementing Algorithms: Iteration
In Python, to start a while loop we use the while keyword Otherwise it’s the same as an if statement Include a comma at the end Indent the next line With for loops, we give it a series of values to pull from The range() function gives us a range Put in a lower-bound, and a upper-bound Problem Solving: Implementing Algorithms

15 Implementing Algorithms: Iteration
While-loops are the same in C# Except for the usual differences in commas, curly braces, and brackets For-loops are a bit more ‘hands on’ We give them A counter (and a starting value) A condition (which keeps the loop going) A change (how the counter changes at the end of every loop) Problem Solving: Implementing Algorithms

16 Implementing Algorithms: Iteration
Java is the same as C# in this regard As usual, the only differences are in the input/output Problem Solving: Implementing Algorithms

17 Problem Solving: Implementing Algorithms
Implement the following program in your programming language of choice BEGIN Main() width  INPUT(‘Enter a rectangle width’) height  INPUT(‘Enter a rectangle height’) FOR row FROM 0 to height - 1 FOR column FROM 0 to width - 1 OUTPUT(‘0’) END FOR OUTPUT(‘\n’) END Main Problem Solving: Implementing Algorithms

18 Arrays and Random Numbers
We have two more things to look at That aren’t linked to sequence, branching, or iteration The first one is: how to make an array The second one is: how to make a random number These are used together quite often For example, to make an array of random numbers Problem Solving: Implementing Algorithms

19 Arrays and Random Numbers
First we have arrays When making these, we have to specify The number of spaces we’ll use In C# and Java: the data-type of the contents of the array We also need to know how to give them values Problem Solving: Implementing Algorithms

20 Arrays and Random Numbers
Here’s how we can make arrays in all three languages We can then assign values to them at any point using something like numbers[0] = 12 C# Java Python Problem Solving: Implementing Algorithms

21 Arrays and Random Numbers
If we want to make a random number, then we need to import the random library in each language Then use the correct function to make a random number C# Java Python Problem Solving: Implementing Algorithms

22 Arrays and Random Numbers
We can put these together to create an array of 10 random numbers Python C# Java Problem Solving: Implementing Algorithms

23 Problem Solving: Implementing Algorithms
Implement the following program in your programming language of choice BEGIN Main() size  INPUT(‘Please enter an array size’) numbers  array with size spaces FOR index FROM 0 to size – 1 numbers[index]  RANDOM(0, 99) END FOR OUTPUT(numbers) END Main Problem Solving: Implementing Algorithms

24


Download ppt "Topic 1: Problem Solving"

Similar presentations


Ads by Google