ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa.

Slides:



Advertisements
Similar presentations
Chapter 2: Understanding Structure
Advertisements

Chapter 3: Modularization
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
Flow Charts, Loop Structures
Understanding the Three Basic Structures
Programming Logic and Design Fourth Edition, Introductory
Lesson 5 - Decision Structure By: Dan Lunney
An Object-Oriented Approach to Programming Logic and Design
Reference :Understanding Computers
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design Seventh Edition
ITEC113 Algorithms and Programming Techniques
KISS Programming. What’s so great about computers? They are fast (so they can accomplish much in a short time… spell check a thesis) They don’t make mistakes.
LECTURER: DR MASRI AYOB Dr. Masri Ayob: TK2633 TK2633: MICROPROCESSOR & INTERFACING Structured Assembly Language Programming.
Program Design and Development
Modules, Hierarchy Charts, and Documentation
Understanding the Mainline Logical Flow Through a Program (continued)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
The Program Design Phases
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Simple Program Design Third Edition A Step-by-Step Approach
Programming Logic and Design Fourth Edition, Introductory
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Problem Solving with Decisions
Programming Logic and Design, Second Edition, Comprehensive
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC LECTURE ONE.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
ITEC113 Algorithms and Programming Techniques
I Power Higher Computing Software Development High Level Language Constructs.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
CHAPTER 2: Understanding Structure. Objectives 2  Learn about the features of unstructured spaghetti code  Understand the three basic structures: sequence,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
Structured Programming (4 Credits)
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
2 Chapter 21 Understanding Structure Programming Logic and Design, Second Edition, Comprehensive 2.
Systems Design.  Application Design  User Interface Design  Database Design.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Programming Logic and Design Fifth Edition, Comprehensive
FLOWCHARTING AND ALGORITHMS
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
An Object-Oriented Approach to Programming Logic and Design Second Edition Chapter 2 Understanding Structure.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
Array Applications. Objectives Design an algorithm to load values into a table. Design an algorithm that searches a table using a sequential search. Design.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
CHAPTER #7 Problem Solving with Loop. Overview Loop logical structure Incrementing Accumulating WHILE/WHILE-END FOR Nested loop Pointer Algorithmic instruction.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Programming Logic and Design Seventh Edition
Programming Logic and Design Eighth Edition
Lecture 2 Introduction to Programming
Using the Priming Read Priming read (or priming input):
A Beginner’s Guide to Programming Logic, Introductory
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Understanding the Three Basic Structures
ALGORITHMS AND FLOWCHARTS
Basic Concepts of Algorithm
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture two Dr. Hamdy M. Mousa

Structured Flowcharts A structure is a basic unit of programming logic; each structure is a sequence, selection, or loop; w ith these can diagram any event Designing structured programs is a matter of dividing tasks into logically independent units (or modules) and then joining these units in a limited number of predefined ways.

Structured programs have several advantages They are easier to understand - The modules can be considered individually. - Need not comprehend an entire program before understanding its individual parts. - The modules can be arranged makes it easier to identify the structure Structured programs are easier to maintain or change, because the individual modules can be changed or corrected without changing the entire program.

Structured flowcharts structured flowcharts must meet the following additional conditions: 1.Each part of the chart must be one of three to five specified structures. 2.Program flow must enter each part at exactly one place and must leave each part at exactly one place.

Structured Flowcharts Structured flowcharts include one or more of: 1.Sequence. 2.If-then (or if-then-else). 3.Do-while loop. 4.Do-until loop. 5.Case.

Sequence represents any series of individual processes. Requirements for Structured Flowcharts

If-then (or if-then-else) represents a decision or selection. The important feature of if-then that distinguishes it from unstructured decisions is that flow lines from both sides of the decision meet before program flow passes from this structure. Requirements for Structured Flowcharts

Do-while loop is a loop with an exit decision that occurs before processing within the loop. After processing, program flow returns to a point immediately before the exit decision. Requirements for Structured Flowcharts

Do-until loop is a loop with an exit decision that occurs after processing. The do-until loop returns program flow to a point immediately before the first symbol in the loop. Requirements for Structured Flowcharts

Case represents a decision with more than two possible outcomes. Requirements for Structured Flowcharts

Loop structure depends upon the placement of the loop exit decision. Do-while loops require that loop exit decisions occur first. do-until loops require that they occur last. Many unstructured loops can be redrawn as structured loops by moving the loop exit decision to the first or last position in the loop. Structured Loops and the EOF Decision

Structured Flowcharts

A priming read or priming input is the first read or data input statement in a program When EOF is the exit decision. Because a READ instruction must precede the first EOF decision, a prime READ as well as a READ instruction within the loop is required. Priming Read

This flowchart reads SALES and prints a total sales figure. It contains a do-while loop.

This flowchart uses a do-until loop to accomplish the same task as previous flowchart.

EOF Pretest When structured programs are designed for computer systems that test for EOF before executing the READ instruction, the prime READ is not required. This is so because the EOF decision can be made without a previous READ. In the rest of our examples that the EOF decision requires a prior READ.

Combining the Structures Design program flowchart that will read amounts, keep track of the number of amounts greater than 50, print the number of amounts over 50, and print the total of all amounts.

The flowchart is structured because it is composed of only the permissible logical structures.

Redesign the previous program flowchart by using do-until loop rather than a do-while loop.

Structured flowcharts The construction of structured flowcharts will not seem natural at first; their restrictions will seem artificial. But structured flowcharts can be an important step in structured programming, and structured techniques result in greater programmer efficiency, even though the resulting programs may be less efficient.

Reason to use Structured Flowcharts 1.Clarity: small doubling method 2.Professionalism: It’s the way things are done Professionally 3.Efficiency: modern computer languages are structured languages 4.Maintenance: easier 5.Modularity: routines assigned to number of programmers

An "Unofficial" Explanation of structure Structured programs If a decision is a loop exit decision it must  be the only loop exit decision in its loop  be either the first or the last symbol in the loop. If a decision is an if-then-else decision, the flow lines from each side of the decision must meet.

The following conventions facilitate constructing structured flowcharts: 1.Draw flow lines so that flow enters each decision at the top. 2.Place the affirmative branch of every decision on the right. 3.Place the negative decision of an if-then- else to the left. 4.Place the negative decision of a loop exit decision at the bottom.

Structured and unstructured flowcharts Structured loop is a do-while Unstructured loop exit is not first or last

Structured and unstructured flowcharts Structured -do-until loop contained within an if-then-else Unstructured - flow enters if-then-else at two locations

Structured and unstructured flowcharts Structured nested do-while loops Unstructured - exit decision for second loop is not first or last

Structured and unstructured flowcharts

Indicators in Loop Exit Decisions Indicators are often used in the loop exit decisions of structured programs when flow must exit a loop for more than one reason. Example: Require that a loop end either when EOF is reached or when a negative sale is read Solution: One decision tests for EOF; the other, for a negative sale. This program could run as described, but it is not structured as shown in Figure.

Indicators in Loop Exit Decisions

Use DONE indicator to stops the program when it is yes. Thus, two exit decisions of previous Figure are replaced by a single loop exit decision, and the program is structured. Indicators in Loop Exit Decisions

Types of structured flowcharts based on EOF exit & OTHER exit in DO- WHILE loop

Types of structured flowcharts based on EOF exit & OTHER exit in DO- UNTIL loop

The following characteristics of the flowcharts 1.Structured flowcharts with EOF exit decisions contain two READ instructions. 2.Structured loops with some other exit decision contain a single READ within the loop. 3.Do-until loops with EOF exit decisions are preceded by a preliminary EOF if-then-else decision. 4.Do-until loops with some other exit decision contain a single EOF decision, and it is an if- then-else decision. 5.When some decision other than EOF is the loop exit decision, the only difference between the do-while and do-until is the location of the exit decision within the loop.

Modular Flowcharts Module: –Unit of code that performs one small task –Called a subroutine, procedure, function, or method Modularization: breaking a large program into modules Advantages of modularization: –Provides abstraction –Allows multiple programmers to work simultaneously –Allows code reuse –Makes identifying structures easier

Each of the submodules in Figure represents a single structure from list of permissible logical structures. It is always possible to modularize a program to this degree Modular Flowcharts Control module Submodules

modularized version of previous Figure Control module Submodules

Exercises

Structured Flowcharts for Printing Tables The programs to be designed create tables by calculating and printing the value of one variable of an equation, given the values of other variables. Example: Draw flowchart to creates a table for the two-variable equation X =3Y by calculating and printing the value of X for each positive-integer value of Y.

Flowchart for the two-variable equation X = 3Y

Two-dimensional tables for equations with three variables Design flowchart for program that will generate the values required to complete the tables for the following equations: X=Y+Z Z Y

Program flowchart & table for equation X=Y+Z

Two-dimensional tables for equations with three variables Design flowchart for program that will generate the values required to complete the tables for the following equations: X=Y+Z Z Y

Program flowchart & table for equation X=Y+Z

Design structured flowchart for program that will generate the values required to complete the tables for the following equations: X=Y+Z and the total for each row Z Y TOTAL Two-dimensional tables for equations with three variables

Program flowchart & table for equation X=Y+Z & Row Total

Two-dimensional tables for equations with three variables Design a structured flowchart with do-until loops for a program that will calculates and prints both the column totals and a grand total for the following equation: X=Y+Z. The values for Y, Z are: Y2468 Z

structured flowchart that uses do-until loops to calculate column totals and a grand total

REVIEW QUESTIONS