PROGRAMMING PAPER 2 AS Algorithms.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

Chapter 2: Problem Solving
Chapter 2: Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
INTRODUCTION TO PROGRAMMING
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
Program Design and Development
A High-Level Model For Software Development
Chapter 2: Algorithm Discovery and Design
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Pseudocode.
Chapter 3 Planning Your Solution
The Program Design Phases
PRE-PROGRAMMING PHASE
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
DCT 1123 Problem Solving & Algorithms
Chapter 2: Problem Solving
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 2: Problem Solving.
Chapter 2: Problem Solving
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
U NDERSTANDING P ROBLEMS AND HOW TO S OLVE THEM BY USING C OMPUTERS.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
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.
กระบวนการแก้ปัญหาด้วย คอมพิวเตอร์ 3 พฤษภาคม :00-17:00.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Flowcharts. Problem Solving Computer programs are written to solve problems or perform tasks Programmers translate the solutions or tasks into a language.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
ITEC113 Algorithms and Programming Techniques
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Principles of Programming - NI July Chapter 2: Problem Solving In this chapter you will learn about: Introduction to Problem Solving Software development.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
 In this chapter you will learn about:  Introduction to Problem Solving  Software development method (SDM)  Specification of needs  Problem analysis.
CSEB114: Principle of programming
The Hashemite University Computer Engineering Department
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
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.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Programming Fundamentals Introduction to Problem Solving  Programming is a problem solving activity. When you write a program, you are actually writing.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
Problem Solving Algorithms. PSPD Using C Design Slide 2 of 40 Topic & Structure of the lesson In this chapter you will learn about: Problem Solving Algorithm.
Fundamentals of Algorithms MCS - 2 Lecture # 3. Representation of Algorithms.
Program Program is a collection of instructions that will perform some task.
Slide 1 of 40 Topic & Structure of the lesson In this chapter you will learn about: Problem Solving Algorithm Pseudocodes Flowcharts.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
| 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.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
 Problem Analysis  Coding  Debugging  Testing.
Algorithms and Flowcharts
Algorithms and Flowcharts
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
ALGORITHMS AND FLOWCHARTS
Introduction to Algorithms and Programming
Understanding Problems and how to Solve them by using Computers
Basic Concepts of Algorithm
Presentation transcript:

PROGRAMMING PAPER 2 AS Algorithms

What it’s all about Problem Solving Algorithm Pseudo codes Flowcharts

At the end of this you should be able to : Understand Problem definitions Solve these using Algorithms Solve these using Flowcharts Be able to identify, define and understand various programming terms

Key Terms You should know what these terms are and when to use them program pseudocode flowchart algorithm

Problem Solving Techniques Here you will learn about: What problem solving is The software development method of problem solving using computers Basic algorithm control structures The sequence structure The selection structure The repetition structure

Problem Solving Techniques What do they translate to : Apply the software development method to solve problems Difference between the Algorithm & the Flowchart Knowing about the control structures

What do you do first First : Read the question and understand it UNDERSTAND THE PROBLEM What is the objective? What are the data? What is the condition? Is it possible to satisfy the condition? Is the condition sufficient to determine the unknown?

Problem Solving Second: DEVISING A PLAN Find the connection between the data and the unknown. Auxiliary problems may be devised if needed. DEVISING A PLAN Have you seen it before? Or have you seen the same problem in slightly different form? Do you know a related problem? Split the problem into smaller, simple sub-problems. If you cannot solve the proposed problem try to solve first some related problem. Or solve more general problem. Or special case of the problem. Or solve the part of the problem.

Problem Solving CARRYING OUT THE PLAN WHAT DID YOU DO, THINK BACK Third: Carry out your plan. CARRYING OUT THE PLAN Carrying out your plan of the solution, check each step. Fourth: Examine the solution obtained. WHAT DID YOU DO, THINK BACK Can you derive the result differently? Can you use the result, or the method, for some other problem?

The software development method Slightly off track… The software development method The software development method consists of the following steps: Requirements specification Analysis Design Implementation Testing and verification Documentation

Algorithmic Problem Solving Algorithmic problem: Any problem whose solution can be expressed as a set of executable instructions. Algorithm: A well defined computational procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output.

Characteristics of an Algorithm   Each step of an algorithm must be exact, preciously and unambiguously described. It must terminate, i.e. it contains a finite number of steps. It must be effective, i.e.., produce the correct output. It must be general, i.e.. to solve every instance of the problem.

Characteristics of an Algorithm   An Algorithm is implemented in some programming language. program = Algorithm + Data Structures. Data Structures refer to the types of data used and how the data are organized in the program. An algorithm is usually presented in the form of some pseudo-code, which is a mixture of English statement,some mathematical notations,and selected keywords from a programming language.

Characteristics of an Algorithm   An Algorithm should emphasize the WHAT’s and not the HOW’s. Consider the problem below: PROBLEM: You are required to design a complete system which will enable the sum of two values to be calculated.

Problem Solving To grapple with this problem, we have to understand the problem from the human perspective. A question to ask yourself is this, “How Would You Calculate the Sum of Two Values?”

What Are Variables? Variables are memory locations within the computer which allows pieces of data to be stored. The word variable comes from the word vary, which means that whatever you place within a variable can be changed. A variable can be viewed as a container used to store things. Data (for example, name, age, salary) can be stored in these containers.

Defining diagram Input Processing Output Now that we have an exact idea about how the problem is solved, let us represent this in a clearer manner, using the defining diagram. Input Processing Output Value1 Value2 Sum

LIST THE STEPS The next step is to identify the actual processing steps required to convert the input to become the output. Input Processing Output Value1 Value2 1) Read Value1, Value2 2) Calculate Sum 3) Display Sum Sum

Algorithm Development Once the defining diagram has been developed, the next logical step is to develop the algorithm (which is much more detailed). Input Processing Output Value1 Value2 1) Read Value1, Value2 2) Calculate Sum 3) Display Sum Sum The developed processing steps have to be more detailed in the algorithm.

Algorithm Development The basic mathematical operators used in algorithms are as follows:- + addition - subtraction * multiplication / division = assignment ( ) brackets for grouping calculations

Algorithm Development Example of an algorithm (using pseudocodes) which can be used to carry out the tasks outlined in the defining diagram is as follows:- 1) Read Value1, Value2 2) Calculate Sum = Value1 + Value2 3) Display Sum

Pseudocoding   A Pseudocode language is semiformal, English-like language with a limited vocabulary that can be used to design and describe algorithms. The pseudocode language can be used for: Designing algorithms Communicating algorithms as programs Implementing algorithms as programs Debugging logic errors in program

Pseudocode for the Control Structures   The Sequence Control Structure: The sequence control structure is a series of steps or statements that are executed in the order in which they are written in an algorithm. For Example: read taxable income read filing status compute income tax

Cont’d The Selection Control Structure:   The Selection Control Structure: The selection control structure defines two courses of action, depending on the outcome of a condition. A condition is an expression that, when evaluated, computes to either true or false. Syntax is: if condition then-part else else-part end-if

Decision Making Being able to mimic the way the human brain works, the computer also has the ability to make decisions. Decision making can be represented in pseudocodes using the IF...THEN construct. IF (expression) THEN : ENDIF

Decision Making The expression is a comparison between two values which evaluates to either true of false. IF (expression) THEN : ENDIF Statements are placed here.

Decision Making Example:- We are looking for a job which pays more than Rs. 4000. Example of an Expression IF (Salary>4000) THEN Say "I Will Take The Job!!" ENDIF

Decision Making Commonly used relational operators in expressions:- > Greater Than < Less Than = Equals To < > Not Equals To >= Greater Than or Equals To <= Less Than or Equals To ( ) Brackets used for prioritising certain calculations

Decision Making Since all expressions works out to be either true or false, what the IF..THEN statement represents is a two-state condition. For example, A potential employer is waiting for you to give a reply (on the spot) about the job offer with a salary of Rs.2000. Your decision would be to only take a job worth more than RM4000. What would you say? IF (Salary>4000) THEN Say “YES!” ELSE Say “NO!” ENDIF

Decision Making Certain conditions may give rise to more than one expression being evaluated. These are known as compound expressions. Example:- You are interested in taking up a job which pays more than Rs.4000 and that the company must also provide a credit card. IF (Salary>4000) And (CreditCard=YES) THEN Take Job!! ENDIF

Decision Making Compound expressions can be represented using the following operators:- AND Every expression must evaluate to be true in order for the whole expression to be true. OR As long as any one of the expression can be true, the entire IF statement will be true. NOT The inverse (opposite) of the entire expression.

Decision Making IF statements can be nested, that is, placed within another IF statement. This is used in situations when the expression is more complex than the simple decisions (as seen earlier).

Decision Making For example, this statement......... IF (Salary>4000) And (CreditCard=YES) THEN Say “Yes I Will Take The Job!!” ENDIF can be represented like this......... IF (Salary>4000) THEN IF (CreditCard=YES) THEN Say “Yes I Will Take The Job!!” ELSE Say “No Credit Card?” Say “Sorry!!” ENDIF Say “Not Enough Pay!!” ........ whereby more possibilities can be represented.

Decision Making For good practice........... IF (Salary>4000) THEN IF (CreditCard=YES) THEN Say “Yes I Will Take The Job!!” ELSE Say “No Credit Card?” Say “Sorry!!” ENDIF Say “Not Enough Pay!!” ........ ensure that statements are properly indented to indicate block of statements which belong together.

Cont’d For Example: if a is greater than b then print “A is greater”   For Example: if a is greater than b then print “A is greater” else print “B is greater” end if

Cont’d Repetition Control Structure:   Repetition Control Structure: The repetition control structure specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. Syntax is: while condition loop-body end-while

ITERATIVE STATEMENTS Looping constructs (also known as repetition or iteration constructs) are a kind of construct found in pseudocodes which allows statements (or a group of statements) to be repeated. The main reason why looping constructs are provided is because most of the problems which we encounter everyday requires some degree of repetition.

ITERATION EXAMPLE An example of a process which is iterative:- Processing Pay of teachers by the school is very much an iterative process as the person processing the payroll applies the same calculations for each employee to produce the pay slip.

TYPES OF LOOPS The looping constructs available in pseudocodes are as follows:- DOWHILE...ENDDO FOR…NEXT REPEAT...UNTIL

DO WHILE LOOP The format of the DOWHILE...ENDDO construct is shown below:- DOWHILE (expression) : ENDDO Group of statements An expression which determines whether the loop will continue.

FOR LOOP The format of the FOR...NEXT construct is shown below:- FOR (initialze TO expression) STEP increment : ENDDO Group of statements An expression which determines whether the loop will continue.

REPEAT…UNTIL The format of the REPEAT...UNTIL construct is shown below:- REPEAT : UNTIL (expression) Group of statements An expression which determines whether the loop will continue.

Looping Constructs Take a look at the following example:- You are required to develop a complete system which will allow the total payroll to be calculated. The system is required to read in the amount to be paid for each employee. The moment the system receives an input value of -99, the system is required to stop and display the total payroll.

Looping Constructs The Defining Diagram Input Processing Output Salary 1) Read Salary 2) Calculate Total 3) Display Total Total

Looping Constructs Algorithm (Using Pseudocodes) 1) Display "Enter Salary" 2) Read Salary 3) Total = 0 4) DOWHILE (Salary<>-99) Total = Total + Salary Display "Enter Salary" Read Salary ENDDO 5) Display "Total Payroll = ", Total

Cont’d Example: Dowhile (income is less than 50000)   Example: Dowhile (income is less than 50000) print “Enter taxable income;should be greater than or equal to 50000” read income Enddo

Program Flowcharts As humans are more inclined towards understanding diagrams and pictures rather than words, pseudocodes tends to become tedious to understand if too lengthy. Program flowcharts, because they are represented graphically, makes understanding easier.

Program Flowcharts The following are the commonly used symbols for drawing program flowcharts. off-page connector terminator process storage decision making document input/output connector arrowheads

Program Flowcharts Begin Read Value1, Value2 Calculate Sum = Value1 + Value2 Display Sum End

Program Flowcharts Begin Read Amount YES NO Amount>20.00? Calculate Actual=Amount * 0.80 Calculate Actual=Amount End

Flowcharting   Another technique used in designing and representing algorithms. Alternative to pseudocoing A pseudocode description is verbal, a flowchart is graphical in nature. Definition: A flowchart is a graph consisting of geometrical shapes that are connected by flow lines.

Sequence Structure Statement -1 Statement -2 Statement -n   Pseudocode: Flowchart: statement_1 statement_2 ------------ statement_n Statement -1 Statement -2 Statement -n

Selection Structure false condition true else-part then-part   Pseudocode: Flowchart: if condition then-part else else-part end_if false condition true else-part then-part

Selection Structure true condition then-part false   Pseudocode: Flowchart: if condition then-part end_if Y condition true N then-part false

Repetition Structure condition loop-body Pseudocode: Flowchart: while condition loop-body end-while T condition Y loop-body F N

Summary Problem Solving– the process of transforming the description of a problem to its solution. To Solve complex problems, we use computers as a tool and develop computer programs that give us solutions. A commonly used method for problem solving using computers is the software development method,which consists of six steps.

Summary An algorithm is a sequence of a finite number of steps arranged in a specific logical order that, when executed, produce the solution for a problem. A pseudocode language is a semiformal,English-like language with a limited vocabulary that can be used to design and describe algorithms.

Summary Any algorithm can be described in terms of three basic control structures.They are the sequence,selection and repetition structures. The top-down stepwise refinement of algorithms is a fundamental problem-solving strategy. A Flowchart is a graphical representation of an algorithm.

Quick Review Question State the difference between the Dowhile – Enddo structure and the Repeat – Until structure. Write an algorithm that will display the first hundred even numbers using the Do-While loop.