Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Structured Programming Lecture: Algorithms, Flowcharts, and Pseudo-codes, Lecture notes of Calculus for Engineers

A lecture note from ENG 224, Structured Programming course. It covers the basics of algorithms, flowcharts, and pseudo-codes. how to write algorithms using pseudo-code and find the average of a given set of numbers using flowcharts. It also introduces the systematic approach to defining a problem and explains top-down and bottom-up design approaches.

Typology: Lecture notes

2019/2020

Uploaded on 01/29/2020

doireann-a
doireann-a 🇨🇦

4 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 2
ENG 224
STRUCTURED PROGRAMMING
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Structured Programming Lecture: Algorithms, Flowcharts, and Pseudo-codes and more Lecture notes Calculus for Engineers in PDF only on Docsity!

Lecture 2

ENG 224

STRUCTURED PROGRAMMING

Overview

  • Algorithm
  • Flowchart
  • Pseudo-codes
  • A Systematic Approach to Defining a Problem
  • Top-down design
  • Bottom-up design
  • State Diagrams
  • Modular Designs

Flowchart Symbols

Find the average of a given set of numbers with flowcharts

How to write Pseudo code

  • A computer can receive information. Typical pseudo code instructions to receive information are: Read name, Get name, Read number1, number
  • A computer can output (print) information. Typical pseudo code instructions are: Print name Write "The average is", ave
  • A computer can perform arithmetic operation Typical pseudo code instructions: Add number to total, or Total = Total + Number Ave = sum/total
  • A computer can assign a value to a piece of data: e.g. To assign/give data an initial value: Initialize total to zero Set count to 0 To assign a computed value: Total = Price + Tax
  • A computer can compare two (2) pieces of information and select one of two alternative actions. Typical pseudo code e.g. If number < 0 then add 1 to neg_number else add one to positive number end-if

Algorithm Average This algorithm reads a list of numbers and computes their average.

Let: SUM be the total of the numbers read COUNTER be the number of items in the list AVE be the average of all the numbers Set SUM to 0, Set COUNTER to 0. (i.e. initialize variables) While there is data do: Read number COUNTER = COUNTER + 1 (i.e. add 1 to COUNTER, storing result in COUNTER) SUM = SUM + number (i.e. add number to SUM, storing result in SUM) end-while if COUNTER = 0 then AVE = 0 else AVE = SUM/ COUNTER Stop.

A Systematic Approach to Defining a Problem

  • Defining the problem is the first step towards a problem solution. A systematic approach to problem definition, leads to a good understanding of the problem. Here is a tried and tested method for defining (or specifying) any given problem:

Divide the problem into three (3) separate components: (a) input or source data provided (b) output or end result required (c)processing - a list of what actions are to be performed

Top-Down Design Approach (Modularization) The modularization approach involves breaking a problem into a set of sub-problems, followed by breaking each sub-problem into a set of tasks, then breaking each task into a set of actions. e.g. Problem 1 Add 23 and 35

  • already solved. Problem 2 Turn on a light bulb

-Sub-problem 1: locate bulb (one task, one action) -Sub-problem 2: depress switch

EXAMPLE 4 Given a list of students’ test scores, find the highest and lowest score and the average score.

-Sub-problem 1: read students’ scores -Sub-problem 2: find highest score -Sub-problem 3: find lowest score

Sub-problem 1 can be considered as one action and therefore needs no further refinement. Sub-problems 2 and 3 however can be further divided into a group of actions.

Bottom Up Design Approach (Modularization)

  • In Bottom-up programming, functions without writing or knowing how they will be used. Typically, this will be a module. The theory is that by writing blocks of program that interact with other program we would have created program building blocks that can be used in many programs.

Feature Bottom-up Approach

  • Bottom-up is hard to code.
  • This method is complicated because it's not easy to formulate simple problems and end up with a global problem.
  • It is result oriented

Example 4: Draw a state diagram

for the state transition table

State Transition Table

Input State 1 0

S1 S 1 S 2

S2 S 2 S 1

State Diagram Algorithm

  • Draw circles to represent the states given.
  • For each of the states, scan across the corresponding row and draw an arrow to the destination state(s). There can be multiple arrows for an input character.
  • Designate a state as the START STATE.
  • Designate one or more states as ACCEPT STATE. This is also given in the formal definition.