Professional Documents
Culture Documents
Programming Fundamentals
Lecture 2 & 3
Why Programming?
- What is Computer?
- What is Program?
- What is Programming?
Define & Analyze the Problem: Before writing a program, we have to define
exactly what Data we need to provide (input) and Information we want the
program to produce (the output).
Expand the outline of the solution into an algorithm: Write a step-by step
procedure that leads to the solution.
Problem solving using computer
Convert the algorithm into a program: Translate the instructions in the
algorithm into a computer program using any programming language.
Run the program: Instruct the computer to execute the program. The
process of running the program differs from language to language.
Debug the program: Make sure that the program runs correctly without
any errors or bugs as they are called in computer terminology. Finding
the errors and fixing them is called debugging. Don’t get depressed
when bugs are found. Think of it as a way to learn.
Problem solving using computer
Document the program clearly: Describe each line of
instruction or at least some important portions in the
program. This will make the program easy to follow when
accessed later for corrections or changes.
Algorithm, Flowchart & Program Design
Properties of Algorithm:
• The given problem should be broken down into simple and
meaningful steps.
• The steps should be numbered sequentially.
• The steps should be descriptive and simple English.
Example
To find the sum of two numbers.
Step1: Start
Step2: Accept the two numbers n1, n2
Step3: sum=n1+n2
Step5: Display sum
Step6: Stop
Flow Chart
Program flowcharts show the sequence of instructions
in a single program or subroutine. Different symbols
are used to draw each type of flowchart.
A Flowchart
– shows logic of an algorithm
– emphasizes individual steps and their
interconnections
– e.g. control flow from one action to the next
Flow Chart
A flowchart is the graphical or pictorial representation of an
algorithm with the help of different symbols, shapes, and arrows
to demonstrate a process or a program. With algorithms, we can
easily understand a program. The main purpose of using a
flowchart is to analyze different methods. Several standard
symbols are applied in a flowchart:
Flow Chart
Four Flowchart Structures
• Sequence
• Decision
• Repetition
• Case
Sequence Structure
• a series of actions are performed in sequence
• The pay-calculating example was a sequence
flowchart.
Decision Structure
• One of two possible actions is taken, depending on a
condition.
Decision Structure
• A new symbol, the diamond, indicates a yes/no question. If
the answer to the question is yes, the flow follows one path. If
the answer is no, the flow follows another path
NO YES
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.
NO YES
x < y?
Process A Process B
Decision Structure
• The flowchart segment below shows how a decision structure
is expressed in C++ as an if/else statement.
NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times 2.
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.
YES
x < y? Process A
Repetition Structure
• The flowchart segment below shows a repetition structure
expressed in C++ as a while loop.
while (x < y)
YES x++;
x < y? Add 1 to x
Controlling a Repetition Structure
• 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
YES
it is no longer an infinite
x < y? Display x
loop?
Controlling a Repetition Structure
• ANSWER: By adding an action within the repetition that
changes the value of x.
YES
x < y? Display x Add 1 to x
Case Structure
A
Connectors
END
A
Modules
• A program module (such as a function in C++)
is represented by a special symbol.
Modules
START
•The position of the module
symbol indicates the point the Read Input.
module is executed.
•A separate flowchart can be Call calc_pay
Display results.
END
Combining Structures
• Structures are commonly combined to create more complex
algorithms.
• The flowchart segment below combines a decision structure
with a sequence structure.
YES
x < y? Display x Add 1 to x
Combining Structures
• This flowchart segment
shows two decision
structures combined. NO YES
x > min?