Topic 1: Problem Solving

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

03 Data types1June Data types CE : Fundamental Programming Techniques.
Pseudocode and Algorithms
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Computer Science 1620 Programming & Problem Solving.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Introduction to JavaScript for Python Programmers
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 - Algorithms and Design
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
By the end of this session you should be able to...
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
Decisions, Decisions, Decisions Conditional Statements In Java.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
4. Java language basics: Function
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Pseudocode Key Revision Points.
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
7 - Programming 7P, Q, R - Testing.
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 6 More Conditionals and Loops
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4 – Control Structures Part 1
While Loops in Python.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to Python
MSIS 655 Advanced Business Applications Programming
Coding Concepts (Sub- Programs)
Java Programming Loops
Chapter 4 - Program Control
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Coding Concepts (Data Structures)
We’re slowly working our way towards classes and objects
Topic 1: Problem Solving
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
3 Control Statements:.
Java Programming Control Structures Part 1
Coding Concepts (Data- Types)
Problem Solving Designing Algorithms.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computer Science Core Concepts
Python programming exercise
What's wrong with Easter jokes? They crack you up
Building Java Programs
Flowcharts and Pseudo Code
Unit 3: Variables in Java
CISC101 Reminders Assignment 3 due today.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
While Loops in Python.
Software Development Techniques
Introduction to Computer Science
Presentation transcript:

Topic 1: Problem Solving Implementing Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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