You are on page 1of 31

 

Programming Fundamentals

Lecture 2 & 3
Why Programming?
- What is Computer?
- What is Program?
- What is Programming?

Computer: An electronic device for storing and


processing data, typically in binary form,
according to instructions given to it in a variable
program.
Program
A computer program is a collection of instructions that
can be executed by a computer to perform a specific
task. A computer program is usually written by a
computer programmer in a programming language.

The process of writing programs is called programming.


Problem solving using computer
• Define the Problem
• Design the Solution
Problem solving using computer
Define the problem: Examine the problem until you understand it
thoroughly.

The task of programming involves a lot of effort and careful planning.


Without this, the computer will produce erroneous results. The
following steps should go into the planning of program:

 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

In a computer program you tell a computer, step by step, exactly


what you want it to do. The computer then executes the
program, following each step mechanically, to accomplish the
end goal.
- The sequence of steps to be performed in order to solve a problem by the
computer is known as an algorithm.
- Flowchart is a graphical or symbolic representation of an algorithm. It is the
diagrammatic representation of the step-by-step solution to a given problem.
- Program Design consists of the steps a programmer should do before they
start coding the program in a specific language. Proper program design helps
other programmers to maintain the program in the future.
Algorithm
An algorithm is step-by-step procedure to solve a problem. The
process of solving a problem becomes simpler and easier with
help of algorithm. It is better to write algorithm before writing
the actual computer program.

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.

Flowchart C++ Code

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.

Flowchart C++ Code

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

• One of several possible actions is taken, depending


on the contents of a variable.
Connectors
• Sometimes a flowchart will not fit on one
page.
• A connector (represented by a small circle)
allows you to connect two flowchart
segments.

A
Connectors

•The “A” connector indicates that


the second flowchart segment A
START
begins where the first segment
ends.

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

constructed for the module. function.

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?

Display “x is outside NO YES


the limits.”
x < max?

Display “x is outside Display “x is


the limits.” within limits.”

You might also like