Logic Formulation

advertisement
Logic
Formulation
What is Logic Formulation?
The process of coming out with the basic
steps to implement a procedure in computer
programming. This is usually resorted to
when having top-down design. Flowcharts
and pseudo codes are the two of most
common output of the exercise.
http://au.answers.yahoo.com/question/index?qid=20071110191353AABc
dvH
What is Logic Formulation? ….
 Bringing
the steps as required by the
task in PROPER ORDER so that when it
is executed procedurally the desired
result is achieved according to the
required output specification.
- Nilo M. Padre
Basic Control Structures
(logical constructs that specify how
instructions in a program are to be
executed)
 Sequence
 Selection
 Repetition
 Unconditional
Jump
Sequence
 Instructions
are designed to be
executed (performed by the computer)
in the order they are written (i.e., one
after another). This control structure
provides the building block for computer
programs. It is used to show a single
action or one action followed in order
(sequentially) by another. Actions can
be inputs, processes, or outputs.
Example
Go
to the phone
Dial the pizza place
Order the pizza
Hang up
Sequence Structure
Selection (conditional, branch, or
decision)

The program branches off to different
instructions depending on whether a condition is
met; or, one of several blocks of program code
is executed based on a test for some condition.
It is used to tell the program which action to
take, based on a certain condition. When a
condition is evaluated, its result is either true or
false. If the result is true, one action is
performed; if the result is false, a different
action is performed.
Example
Open your wallet
If
you have enough money,
THEN Go to the phone.
Dial the pizza place.
Order the pizza.
Hang up.
ELSE Forget the whole thing.
Selection Structure

Single Selection
T
F
Selection Structure ...

Double Selection
F
T
Selection Structure ...

Multiple Selection
T
F
T
F
T
F
Repetition (looping or iteration)
 The
program repeats the same
instructions over and over. It is used
when a set of actions is to be
performed repeatedly.
Example
DO
gobble down pizza
WHILE there is still pizza.
Or
DO
gobble down pizza
UNTIL none remains.
Repetition

While Structure
T
F
Repetition Structure ...

For Structure
T
F
Repetition Structure ...

Do/While Structure
T
F
Unconditional Branch
 The
program jumps from one place to
another. Structured programming
forbids this structure.
Program Development Life Cycle
A
part of SDLC’s implementation phase and
provides an organized plan for breaking
down the task of program development into
manageable chunks, each of which must be
successfully completed before moving on to
the next phase.
 An outline of each of the steps used to build
software applications.
1. Analyze: Define the problem.
 Be sure you understand what the
program should do.
What the output should be – that is,
exactly what the task should produce.
Identify the data, or input, necessary to
obtain the desired output.
Determine how to process the input to
obtain the desired output – that is,
determine what formulas or ways of doing
things can be used to obtain the output.

2. Design: Plan the solution to the
problem.
 Develop
the algorithm (a logical
sequence of precise steps that solve the
problem). Every detail, including
obvious steps, should appear in the
algorithm.
 Translate
the algorithm into a logic plan
using any of the popular methods –
flowcharts, pseudocode, top-down charts.
These design tools help the programmer
break a problem into a sequence of small
tasks the computer can perform to solve the
problem. Planning also involves using
representative data to test the logic of the
algorithm by hand to ensure that it is correct.
3. Choose the interface: Select the objects
(text boxes, command buttons etc.)
 Determine
how the input will be obtained
and how the output will be displayed. Then
create objects to receive the input and
display the output. Also, create appropriate
command buttons and menus to allow the
user to control the program.
This step is used with Windows Application but not
with Console Application.
4. Code: Translate the
algorithm/flowchart/pseudocode
into a programming language.
 During
this stage, the program is written in a
particular language (e.g., Visual Basic, C,
Java) and entered into the computer. The
programmer/coder uses the
algorithm/flowchart/pseudocode devised in
Step 2 along with a knowledge of the
programming language, say, Visual Basic.
5. Test and debug. Locate and remove
any errors in the program.

As the program is typed, some code editors point
out certain types of program errors. Other types
will detected by when the program is compiled (
or interpreted) and/or executed; however, many
errors due to typing mistakes, flaws in the
algorithm, or incorrect usages of the language
rules can be uncovered and corrected only by
careful detective work. An example of such an
error would be using addition when multiplication
was the proper operation.
6. Complete the documentation:
Organize all the material that
describes the program.
 Documentation
is intended to allow
another person, or the programmer at a
later date, to understand the program.
Internal documentation consists of
statements in the program that are not
executed, but point out the purposes of
various parts of the program.
Documentation…
 Documentation
might also consist of a
detailed description of what the
program does and how to use the
program (for instance, what type of
input is expected).
Documentation…
 For
commercial programs, documentation
includes an instruction manual and on-line
help. Other types of documentation are
the flowchart, pseudocode, and top-down
chart that were used to construct the
program. Although documentation is listed
as the last step in the program
development cycle, it should take place as
the program is being coded.
Flowcharting Symbols
Some Application Flowcharting Symbols
Card File
Direct Access File
Computer Program
Document (such as hard copy
from a computer system
Some Application Flowcharting Symbols…
Display
Multidocument
Manual Input
Preparation
Manual Operation
Some Application Flowcharting Symbols…
Punched Tape
Collate
Sort
Extract
Some Application Flowcharting Symbols…
Merge
Sequential Access Storage
Magnetic Disk
Direct Access Storage
Application Flowcharting
 The
purpose of an application flowchart is to
use symbols to give a general picture of
what functions the computer system should
perform.
 It reveals how many program are to be
written, what reports or documents are to be
produced, and the form and source of the
input data.
 It gives an overall picture of the application.
Program Flowcharting Symbols
Documentation…
Documentation…
Example Problem
 Suppose
you are asked to determine
the number of stamps to place on an
envelop with the rule or policy that
one stamp will be used for every five
sheets of paper.
Problem Analysis
Program Design
Documentation…
Choosing the Interface

Form Layout
Properties Table
Properties Table…
Coding the Program
Coding the Program…
Coding the Program…
Coding the Program…
Coding the Program…
Problem

Given a street number of a one-way in
New York, decide the direction of the
street, either eastbound or westbound.
Even-numbered streets run eastbound.
Thank you for listening!
Arrays
Array
An ordered set of values associated with a
single variable name. –Shelly, Cashman, Dorin, Quasney
 A list of data items that all have the same
type and the same name. - Ferrel
 A data structure whose elements are
accessed by means of index positions. –

Lambert & Osborne

A collection of a fixed number of variables of
the same data type or a list (or table) of
variables of the type. - Goldstein
Declaring an Array

Arrays are declared in a variable declaration
section.

General Syntax in C/C++
component type arrayname[sizeN]…[size1];
Dimension

The number of subscripts required to
describe an array.
The subscript or index is the item’s position
in an array. It is an integer enclosed
within the brackets (square or rounded).
Declaration Syntaxes

For One-Dimensional
type array_name[size];

For Two-Dimensional
type array_name[rows][columns];

For Three-Dimensional
type array_name[pages][rows][columns];
Examples

double
Department[7];
The component of this array are:
Department[0]
Department[1]
Department[2]
Department[3]
Department[4]
Department[5]
Department[6]

char
Sign[25];
The components of this array are:
Sign[0]
Sign[1]
Sign[2]
.
.
.
Sign[25]
typedef enum {Red, Green, Yellow}
TrafficLight;
TrafficLight Traffic[4];
The components are:
Traffic[0]
Traffic[1]
Traffic[2]
Traffic[3]
Assigning A Value to an Array
The components of an array may be
assigned values just like any other
variables.
Examples:
FirstVisit[1] = Fri;
Traffic[3]=Red;
Department[5]=290.12;
Sign[24]=‘*’;

Initializing an Array

Syntax
type-specifier array_name[sizeN]…[size1] = {
value-list }
Examples

Int num[7] = {1,2,3,4,5,6,7}

char str[8]=“Laughter”;
which is the same as

Char str[8]={‘L’,’a’,’u’,’g’,’h’,’t’,’e’,’r’}

int sqrs[7][2]= {
1,1,
2,4,
3,9,
4,16,
5,25,
6,36,
7,49
};
Unsized-Array Initialization
char err1[12]=“read error\n”;
 char err2[13]=“write error\n”;
 char err3[18]=“cannot open file\n”;

which is the same as,
 char err1[]=“read error\n”;
 char err2[]=“write error\n”;
 char err3[]=“cannot open file\n”;

int sqrs[][2]= {
1,1,
2,4,
3,9,
4,16,
5,25,
6,36,
7,49
};
Logic
The grammar of correct thinking and reasoning.
 Two Elements:
 To think correctly and;
 To reason out correctly.

If one of the elements would be missing, then no
logic can be derived from.
– Nilo M. Padre

Formulation

-
An act or the product of formulating.
Merriam Webster (http://www.merriamwebster.com/dictionary/formulation)
● The act of creating something by thinking
- The Free Dictionary
(http://www.thefreedictionary.com/formul
ation)
Download