Computer Programming Fundamentals

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

PROBLEM SOLVING TECHNIQUES
Algorithms and Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
 Introduction to Programming History of programming.
Pseudocode and Algorithms
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Review Algorithm Analysis Problem Solving Space Complexity
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
Adapted from slides by Marie desJardins
Chapter 2: Problem Solving
Chapter 2: Problem Solving
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Muhammad Adnan Talib Lec#4 27 th Sep,2012 Computer Science Department COMSATS Institute of Information Technology Sahiwal Introduction to Computer Programming.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Flowcharting & Algorithms. Quick. Close your Eyes and Listen You are still sitting in the classroom. However, you have been called to the counselor’s.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Programming Fundamentals Introduction to Problem Solving  Programming is a problem solving activity. When you write a program, you are actually writing.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Flowcharts
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
Program design Program Design Process has 2 phases:
Problem Solving & Computer Programming
ALGORITHMS AND FLOWCHARTS
CS1010 Programming Methodology
3.1 Fundamentals of algorithms
GC101 Introduction to computers and programs
Lesson 2 Flowcharting.
REPETITION CONTROL STRUCTURE
Repetition Structures Chapter 9
ALGORITHMS AND FLOWCHARTS
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
CS111 Computer Programming
Algorithms and Flowcharts
Introduction To Flowcharting
Ch 7: JavaScript Control Statements I.
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
ALGORITHMS AND FLOWCHARTS
Algorithms & Pseudocode
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Algorithm Discovery and Design
ALGORITHMS AND FLOWCHARTS
Introduction to Algorithms and Programming
Understanding Problems and how to Solve them by using Computers
CHAPTER 4 Iterative Structure.
Introduction to Programming
Basic Concepts of Algorithm
Introduction to Algorithms
REPETITION Why Repetition?
Presentation transcript:

Computer Programming Fundamentals Algorithms

Agenda What is an algorithm? How do we record an algorithm? An example The basic building blocks How do we record an algorithm? Flow charts Pseudocode An example Sieve of Eratosthenes 1

Basic building blocks Four principal programming structures Sequence Selection (if … else …) Iteration (loops) Sub-programs (functions) Can construct any algorithm from these basics

What is an algorithm? A ‘recipe’ you use to complete a task Use combinations of the basic building blocks For many common tasks algorithms are already known Eratosthenes 276 – 194 BC Many still to be discovered Factorisation of large numbers

Recording algorithms Flowcharts Pseudocode We have already seen how to describe each of the basic building blocks as a flowchart Graphical They can get cumbersome for more complex algorithms Pseudocode An alternative to flowcharts A generic form of the basic code building blocks Text-based No special software required

Pseudocode Like writing code but…. No predefined format No formal syntax Although it should be unambiguous Allows you to defer decisions about details No compiler or interpreter !! Relatively easy to convert into ‘real’ code In most target languages

Building blocks Sequence Selection Iteration IF - THEN - ELSE SELECT - ENDSELECT Iteration FOR - NEXT WHILE - ENDWHILE REPEAT - UNTIL

Building blocks - sequence Variables A and B each have a value How do we exchange the two? Exchange A and B: Exchange the values in variables A and B: temp = A A = B B = temp Start Finish temp = A A = B B = temp

Building blocks - selection IF-THEN-ELSE Set MAX equal to the larger value in variables A and B: IF A > B THEN MAX = A ELSE MAX = B ENDIF # Set MAX equal to the larger value # in variables A and B: if A > B: MAX = A else: MAX = B 8

Building blocks - selection # Process direction variable: # SELECT CASE direction OF # north: move up if direction == 'NORTH': move("UP") # south: move down elif direction == 'SOUTH': move("DOWN") # east: move right elif direction == 'EAST': move("RIGHT") # west: move left elif direction == 'WEST': move("LEFT") # OTHERWISE error else: bad_move(direction) SELECT Process direction variable: SELECT CASE direction OF north: move up south: move down east: move right west: move left OTHERWISE error ENDSELECT 9

Building blocks - iteration FOR - NEXT A = [1,…] # Process each element # in an Array ‘A’: #FOR k = 0 TO (size_of_A - 1) # process A(k) for k in range(len(A)): process_item(A[k]) Process each element in an Array ‘A’: FOR k = 0 TO (size_of_A - 1) process A(k) NEXT k 10

Building blocks - iteration WHILE - ENDWHILE # Print Fibonacci numbers # less than MAX : A = 0 B = 1 #WHILE B less than MAX while B < MAX : print (B) temp = A+B A = B B = temp Print Fibonacci numbers less than MAX : A = 0, B = 1 WHILE B less than MAX print B temp =A+B A = B B = temp ENDWHILE 11

Building blocks - iteration REPEAT - UNTIL #Calculate average of entered values: count = 0 total = 0 #REPEAT while True: # INPUT x x = int(input("Enter integer:")) count += 1 total += x # INPUT more more = (input("Any more? [Y or N]")).upper() # UNTIL more is not 'yes' if more[0] != 'Y': break #Calculate average print ("Average is ",total/count) Calculate average of entered values: count = 0, total = 0 REPEAT INPUT in count = count + 1 total = total + in INPUT more UNTIL more is not ‘yes’ Calculate average 12

Example Finding prime numbers Sieve of Eratosthenes Finds all primes less than a particular value 13

Sieve of Eratosthenes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 29 40 41 42 43 44 45 46 47 48 49

The sieve in pseudocode Generate primes using sieve method: INPUT maxprime Create integer array called primes with maxprime elements Initialise primes so that for all x primes[x] = x (…primes[3] = 3, primes[4] = 4, … etc.) Show 1 is not prime by setting primes[1] =0 Initialise current_prime = 2 WHILE current_prime is less than length of primes IF primes[current_prime] indicates a prime (it is not 0) THEN Initialise counter to square of current_prime WHILE counter less than length of primes mark primes[counter] as not prime by setting to 0 increment counter by current_prime END WHILE END IF increment current_prime by 1 ENDWHILE Now print the results: FOR k = 0 TO size of primes IF primes[k] is not 0 THEN k must be a prime so print it NEXT k

Exercises Implement some algorithms defined in pseudocode Implement the ‘Sieve of Eratosthenes’ algorithm