PROGRAMMING, ALGORITHMS AND FLOWCHARTS

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

ALGORITHMS AND FLOWCHARTS
Summer ’12 AP Computer Science APCS Summer Assignments Read thoroughly this ppt and solve examples 6 and 7.
More on Algorithms and Problem Solving
ALGORITHMS AND FLOWCHARTS
PROBLEM SOLVING TECHNIQUES
Introduction to Flowcharting
Introduction to Flowcharting
Understanding the Three Basic Structures
Introduction to Flowcharting
An Object-Oriented Approach to Programming Logic and Design
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
ALGORITHMS AND FLOWCHARTS
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Fundamentals of Algorithms MCS - 2 Lecture # 4
Chapter 2- Visual Basic Schneider
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Developing logic (Examples on algorithm and flowchart)
Chapter 3 Planning Your Solution
The Program Design Phases
Review Algorithm Analysis Problem Solving Space Complexity
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
CSC103: Introduction to Computer and Programming
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Flowcharting & Algorithms. Quick. Close your Eyes and Listen You are still sitting in the classroom. However, you have been called to the counselor’s.
1 Program Planning and Design Important stages before actual program is written.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Concepts of Algorithms CSC-244 Unit Zero Pseudo code, Flowchart and Algorithm Master Prince Computer College Qassim University K.S.A.
1 Introduction to Flowcharting Computer Science Principles ASFA.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
Introduction to Flowcharts
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and 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.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
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
GC101 Introduction to computers and programs
Problem Solving Concepts
Unit 3: ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
FLOWCHARTS.
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
CS111 Computer Programming
Algorithms and Flowcharts
Introduction to Flowcharting
Introduction To Flowcharting
Introduction to Flowcharting
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
ALGORITHMS AND FLOWCHARTS
How to develop a program?
Understanding the Three Basic Structures
ALGORITHMS AND FLOWCHARTS
Introduction to Algorithms and Programming
Introduction to Programming
Introduction to Flowcharting
Presentation transcript:

PROGRAMMING, ALGORITHMS AND FLOWCHARTS

What is Programming? Series of instructions to a computer to accomplish a task Instructions must be written in a way the computer can understand Programming languages are used to write programs

ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem this sequence of steps is called an algorithm Implementation phase implement the program in some programming language

Devise an algorithm for cooking a sausage on a barbecue

Steps in Problem Solving First produce a general algorithm. Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English.

Pseudocode & Algorithm Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. Start with a general algorithm.

General Algorithm Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print “FAIL” else Print “PASS”

Detailed Algorithm Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif

Task: add two numbers Pseudocode: Start Get two numbers Add them Print the answer End

Next Step - The Flowchart A graphical representation of the sequence of operations in an information system or program. Information system flowcharts show how data flows from source documents through the computer to final distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart. The sequence of the flowchart and the algorithm MUST match.

The Flowchart A Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next

Flowchart Symbols Basic Denotes a self-contained section of the program Module

Example Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 START Input M1,M2,M3,M4 GRADE(M1+M2+M3+M4)/4 IS GRADE<50 PRINT “FAIL” STOP Y N Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print “FAIL” else Print “PASS” endif PRINT “PASS”

Control Structures Represent the flow of logic through the programme. There are four main control structures: Sequence Decision – incorporating if-then-else Repetition Case

Sequence Structure a series of actions are performed in sequence START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END a series of actions are performed in sequence This is the flowchart for a program which calculates an employee’s gross pay.

Example 1 Write an algorithm and draw a flowchart to convert the length in feet to centimetres. Pseudocode: Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)

Example Algorithm Step 1: Input Lft Step 2: Lcm  Lft x 30 Flowchart Algorithm Step 1: Input Lft Step 2: Lcm  Lft x 30 Step 3: Print Lcm START Input Lft Lcm  Lft x 30 STOP Print Lcm

Example 2 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A

Example 2 Algorithm Step 1: Input W,L Step 2: A  L x W Step 3: Print A START Input W, L A  L x W STOP Print A

DECISION STRUCTURES Decision Structure One of two possible actions is taken, depending on a condition. A new symbol, the diamond, indicates a yes/no question.

Decision Structures Sometimes only one choice is necessary YES NO

Example 1 Pseudocode: Start Get year born Calculate age Print age If age > 50 print OLD End

Flowchart Start Get year born Calculate age Print age Get yr Flowchart Start Get year born Calculate age Print age If age > 50 print OLD End Calc age Print age Y Age>50 OLD N End

Decision Structure Sometimes two choices are offered. If the answer to the question is yes, the flow follows one path. If the answer is no, the flow follows another path YES NO

Decision Structure In the flowchart segment below, the question “is x < y?” is asked. If the answer is no, then process A is performed. If the answer is yes, then process B is performed. YES NO x < y Process B Process A

IF–THEN–ELSE STRUCTURE The structure is as follows If condition then true alternative else false alternative

IF–THEN–ELSE STRUCTURE The algorithm for the flowchart is as follows: If A>B then print A else print B is A>B Print A Print B N Y

Example 1 A ticket seller is issuing show tickets at the gate. When the patrons arrive, he asks how old each one is. It they are under 12, they pay half price. If they are 12 or over, they pay full price. Algorithm Get age Age < 12? If Age < 12 Then Pay half price Else Pay full price

Flowchart Begin Input age YES NO Age < 12 Half price Full price End

Example 2 Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message. ALGORITHM Step 1: Input VALUE1, VALUE2 Step 2: if (VALUE1 > VALUE2) then MAX  VALUE1 else MAX  VALUE2 endif Step 3: Print “The largest value is”, MAX

Example 2 MAX  VALUE2 STOP N Y START Input VALUE1,VALUE2 MAX  VALUE1 is VALUE1>VALUE2 Print MAX

Example 3 Write the algorithm and draw the flowchart for the following: Enter two numbers, x and y. If x is less than y, square x to give the result a, otherwise, add x + y to give the result a.

YES NO x < y Algorithm Input x Input y x < y? If x <y Then a = x^ Else a = x+y YES NO x < y Calculate a as x times 2. Calculate a as x plus y.

NESTED IFS One of the alternatives within an IF–THEN–ELSE statement may involve further IF–THEN–ELSE statement

Repetition (Iteration) Structure Repetition Structure Repetition (Iteration) Structure A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop.

Repetition Structure Notice the use of the diamond symbol. A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists.

Repetition Structure In the flowchart segment, the question “is x < y?” is asked. If the answer is yes, then Process A is performed. The question “is x < y?” is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited. x < y Process A YES

So this might look like… x < y Add 1 to x YES When would this loop exit?

Out of Control! YES x < y Display x The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created. In this flowchart segment, x is never changed. Once the loop starts, it will never end. QUESTION: How can this flowchart be modified so it is no longer an infinite loop? x < y Display x YES

Pre-test ( Before) Loop This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed. x < y? Display x Add 1 to x YES

Post-test (After) Loop Display x Add 1 to x YES x < y This flowchart segment shows a post-test repetition structure. The condition is tested AFTER the actions are performed. A post-test repetition structure always performs its actions at least once.

Case Structure One of several possible actions is taken, depending on the contents of a variable. Used for MORE THAN TWO decisions

The structure below indicates actions to perform depending on the value in years_employed. CASE years_employed 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800

1 2 3 Other If years_employed = 2, bonus is set to 200 CASE years_employed 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 If years_employed = 1, bonus is set to 100 If years_employed is any other value, bonus is set to 800

Draw the Flowchart for the above example Case Algorithm Input a Grade Case based on Grade     Case =100         Report “Perfect Score” (or suitable command eg Grade = Perfect)     Case > 89         Report “Grade = A”     Case > 79         Report “Grade = B”     Case > 69         Report “Grade = C”     Case > 59         Report “Grade = D”     Default (or Else)         Report “Grade = F” End Case Draw the Flowchart for the above example

>89 >79 >69 > 59 100 other Flowchart CASE grade Grade =A Grade = B Grade = C Grade = D 100 other Grade = Perfect Grade = F

Construct the algorithm and the flowchart for the following procedure: An egg packing business needs to devise a process to sort eggs for delivery to supermarkets. If the eggs weigh 50 grams, they are Class A eggs. If they weigh 60 grams, they are Class B eggs. If they weigh 70 grams, they are Class C eggs. If they weigh more than 70 grams, they are Class D eggs.

50 60 70 > 70 Algorithm Flowchart Input a Weight Case based on Weight     Case =50         Class = A     Case =60         Class = B     Case = 70         Class = C     Case > 70         Class = D     OR         Default/ Else Class = D  End Case CASE weight 50 60 70 > 70 Class =A Class = B Class = C Class = D

Modules (stepwise refinement) A program module is a part of a program that makes sense on its own, just like a paragraph in an essay should make sense on its own. In large programs, such as those used in industry, programs are developed as separate modules and then put together. The process of representing modules in flowcharts is known as stepwise refinement. You will look at this more closely when you start programming.

A separate flowchart can be constructed for the module. START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END START END Read Input. Call calc_pay function. Display results. The position of the module symbol indicates the point the module is executed. A separate flowchart can be constructed for the module.

A simpler example… could look like CASE weight 50 60 70 > 70 Class =A Class = B Class = C Class = D Input weight Calculate class Display class

A complete program example When you use stepwise refinement, the complete, refined flowchart is drawn first. Then the modules are named and drawn in order. This program lists the AFL team name and points total for all teams that have a full-forward from the AFL competition who has kicked at least 10 goals.

Startup Headings

MainLoop Check pos Check conf Check goals Goals >= 10 Check AFL

Although this procedure is listed as a module in the opening flow chart, it’s unsuitable. Why? Finish