Problem Solving: Structure Charts

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Problem Solving ⇑

← Top-down design and Step-wise refinement Structure charts Decision tables →


Structure charts can map the structure and data flow of complicated tasks

A Structure Chart in software engineering is a chart which shows the breakdown of a system to its lowest manageable parts. They are used in structured programming to arrange program modules into a tree. Each module is represented by a box, which contains the module's name. The tree structure visualizes the relationships between modules, showing data transfer between modules using arrows. Structured Charts are an example of a top-down design where a problem (the program) is broken into its components.

Symbol Name Meaning
Module
Name
Process Each Box represents a programming module, this might be something that calculates the average of some figures, or prints out some pay slips
CPT-Structured Chart Data Couple.svg Data Couple Data being passed from module to module that needs to be processed.
CPT-Structured Chart Flag.svg Flag [Extension - you don't need to know this for the exam] Check data sent to process to stop or start processes. For example when the End of a File that is being read is reached, or a flag to say whether data sent was in the correct format

Let's take a look at a simple example of how this might be executed when representing the following code:

dim num1 as integer
dim num2 as integer
 
sub calculateAverage()
  dim avg as integer
  inputNums()
  avg = average(num1, num2)
  outputAvg(avg)
end sub
 
function average(a,b)
  return (a + b) / 2
end function
 
sub inputNums()
  num1 = console.readline()
  num2 = console.readline()
end sub
 
sub outputAvg(x)
  console.writeline("average = " & x)
end sub
A structure chart for the above code
Exercise: Structure Charts

Create structure charts for the following code:

sub main()
  dim num1 as integer
  dim num2 as integer
  dim avg as integer
  sayHello()
  num1 = 34
  num2 = 89
  avg = average(num1, num2)
end sub
 
function average(a,b)
  return (a + b) / 2
end function
 
sub sayHello()
  console.writeline("hello")
end sub

Answer :

CPT-Structured-Chart-Q1.svg

Selection[edit]

Structure Chart representation of the selection code

A selection in a Structure Chart is determined by the diamond symbol. This means a condition will be checked and depending on the result, different modules will be executed.

sub main()
  dim num1 as integer
  num1 = console.readline()
  if num1 = 7 then
     luckyNumber()
  else
     otherNumber()
  endif
end sub

Iteration[edit]

Structure Chart of the code to the left

Using the semi circular arrow we can represent iteration in Structure Charts. The arrow encompasses a link to a module, implying that module is executed multiple times. Let's take a look at a coded example:

sub main()
  dim num1 as integer
  num1 = console.readline()
  while num1 > 0 do
     num1 = countdown(num1)
  end while
end sub
 
sub countdown(a)
  return a - 1
end sub
Exercise: Structure Charts, Iteration and Selection

Create structure charts for the following code:

sub howManyThrees()
  dim num1, count, total as integer
  num1 = startMsg()
  count = 0
  total = 0
  while num1 > 0 do
     checkNumber(count, total, num1)
     num1 = num1 - 1
  end while
  endMsg(count)
end sub
 
sub checkNumber(byRef c, byRef t, byVal n)
  If n MOD 3 = 0 Then
     c = divBy3(c)
  Else
     t = add(n, t)
  EndIf
end sub
 
function divBy3(x)
  return x + 1
end function
 
function add(n, t)
  return n + t
end function
 
function startMsg()
  console.writeline("program started, enter your number")
  return console.readline()
end function
 
sub endMsg(n)
  console.writeline("number of threes : " & n)
end sub

Answer :

CPT-Structured-Chart-Q2.svg