0% found this document useful (0 votes)
60 views18 pages

Lecture02 Week 1

The document discusses algorithms and programming fundamentals. It defines an algorithm as a finite set of steps to solve a problem and lists characteristics like having inputs and outputs, being definite, finite, and effective. It describes algorithm design tools like flowcharts and pseudocode. Pseudocode uses keywords to represent programming concepts in plain English. The document also covers designing programs by analyzing the problem, designing a solution, coding, testing, and evaluating it. Program design principles like modularity, documentation, debugging, and testing are discussed.

Uploaded by

abdullahjaan0112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views18 pages

Lecture02 Week 1

The document discusses algorithms and programming fundamentals. It defines an algorithm as a finite set of steps to solve a problem and lists characteristics like having inputs and outputs, being definite, finite, and effective. It describes algorithm design tools like flowcharts and pseudocode. Pseudocode uses keywords to represent programming concepts in plain English. The document also covers designing programs by analyzing the problem, designing a solution, coding, testing, and evaluating it. Program design principles like modularity, documentation, debugging, and testing are discussed.

Uploaded by

abdullahjaan0112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 2 FA-22 (CS/SE/AI)

Course :Programming Fundamentals

Instructor : Ms. Ayesha


Algorithm and Pseudo code
What is algorithm
Algorithm is the effective method to obtain step by step solution of
problems. Knowledge of algorithm forms the foundation of study
programming languages. Before starting with programming let us
understand algorithm. i.e how to write algorithm, characteristic of
algorithm, algorithm designing tool and conversion of algorithm to
programs.

Definition:
An algorithm is defined as the finite steps followed in order to solve the
given problem.
Characteristic of algorithm:

Input: An algorithm must provide with any number of input/data values.


– Output: An algorithm must produce at least one output.
– Definiteness: Each step of the algorithm must be clear and distinct which
ensures that the statement must be unambiguous.
For example:
To divide two numbers
Algorithm
Step1: Accept the numbers a,b. Step2: c=a/b
Step3:Display c.
Step 4: Stop.
Step 2 of this algorithm is not clear, as there will be no output . If b=0.
Since the system doesn’t such an answer, care must be taken while
writing an algorithm.

The above algorithm can be rectified as follows Step1: Read the


two numbers a, b.
Step2:If (b=0);
– Print “denominator value is 0;
–And go to step 5 Step3:c=a/b
Step4: print c
 Stop
• Finiteness: The algorithm must be terminated after a finite
number of steps. Let us illustrate this point with the help of an
example:
Algorithm:
Step1: Start Step2: Let a=9
Step3: x=y*z
Step4: Print x and go to step 2 Step5:
Stop
Here we noticed that in an algorithm, nowhere the value of a is
changed, which control the flow of algorithm nerve terminates.
Such statements must be avoided.
The finiteness property assures the unambiguity in the flow.
Effectiveness:
• It must be possible in practice, to carry out each step manually. The
statements must be feasible. Thus the algorithm even
through is definite, should also be practical. If one take care of the
above mentioned characteristics while writing the algorithm, then
we can be sure of the results, for any kind of inputs.
Algorithm Designing tools:
Algorithm must be designed in such a way that it is followed by
the pure top-down approach.
This will ensure the straight line execution of the algorithm. An
algorithm can be expressed or designed in many ways. One can
make a use of any language to specify the steps involved in solving
a particular problem but simple and precise language could be
adopted. Two famous ways of writing the algorithm are making
use of flowcharts and pseudo codes.
Flowchart:
• Flowchart is the diagrammatic way of representing, the steps to be
followed for solving the given problem.
• Flowcharts proves the visualization of the steps involved, since it
is in the form of a diagram one can understand the flow very
easily. Here are some diagrammatic symbols to design a flow
chart.

11/05/2023 8
START AND STOP

COMPUATIONAL

INPUT AND OUTPUT STATEMENTS

DECESION MAKING

CONNECTOR

FLOW INDICATOR

11/05/2023 9
Pseudocode:

Pseudocode is an artificial and informal language that helps the programmers to develop algorithm in the text format. It allows the
programmer to focus on the logic of the algorithm without being distracted by details of the language syntax. It narrates steps of the
algorithm more precisely.
• Following are the keywords used to indicate input, output and other operations.
• Input – READ, GET
• Output – PRINT, DISPLAY
• Compute – CALCULATE, DETERMINE
• Initialize SET, INT
• Add one – INCREMENTER
• Sub one- DECREMENTER
• Conditions and loops
– IF-THEN ELSE
– Repetitive execution
– WHILE
– CASE
– REPEAT UNTIL
– FOR

11/05/2023 10
• Pseudocode to obtain sum of two numbers. BEGIN
INPUT X,Y
DETERMINE SUM = X+Y PRINT SUM
END

• Pseudocode to obtain average of three numbers. BEGIN


DISPLAY “INPUT 3 NOS” INPUT X,Y,Z
DETERMINE SUM = X+Y+Z DETERMINE
AVG=SUM/3 PRINT AVG
END
11/05/2023 11
Designing a program

A program is a set of instructions that are grouped together to accomplish a


task or tasks. The instructions consist of task like reading and writing memory,
arithmetic operations, and comparisons.
Aim of a particular Program is to obtain solution to a given problem.

We can design a program by going through the following first four
major steps:
• Analyze the program.
• Design a solution/Program
• Code/Enter the program
• Test the program
• Evaluate the solution.
11/05/2023 12
• Analyze the program: When we analyze a problem, we think about the
requirements of the program and how the program can be solved.
• Design a solution/Program: This is the stage where we decide how our
program will work to meet the decision made during analysis. Program
design does not require the use of a computer. We can design program
using pencil and paper. This is the stage where algorithm are
designed.
• Code/Enter the Program: Here we enter the program into the
machine by making use of suitable programming language.
• Test the Program: This part deals with testing of programs for various
inputs and making necessary changes if required. Testing cannot show
that a program produces the correct output for all possible inputs,
because there are typically an infinite number of possible inputs. But
testing can reveal syntax errors, run-time problems and logical mistakes.
11/05/2023 13
• Evaluate The solution: Thus, finally program can be implementing to
obtain desired results. Here are some more points to be considered
while designing a program.
– Use of procedures.
– Choice of variable names.
– Documentation of program
– Debugging program
– Testing.
Use of procedures:
 Procedures is a part of code used to carry out independent task. Separate
procedures could be written to carry out different task and then can be
combined and linked with the main procedure. This will help in making the
algorithm and eventually programs readable and modular.
11/05/2023 14
• Choice of variable: We can make programs more meaningful and easier to
understand by choosing appropriate variable and constant names. For
example: if wish to store age of two different people we can define
variable age1,age2 to store their ages. The main advantage of choosing
correct variable is that the program becomes self explanatory.

• Documentation of Program: Brief and accurate comments can be included


at the beginning of each procedure/function.Program should be
documented so that it can be used easily by the other people unfamiliar
with the working and input requirements of the program.Thus
documentation will specify what response it requires from the user during
execution.
11/05/2023 15
Debugging the Program: It is expected that one should carry out
number of tests during implementation of algorithm, to ensure that
the program is behaving correctly according to its specifications.
The program may have some logical errors, which may not be
detected during compilation. To detect such type of errors we may
print computed values at various steps involved in algorithm. We
should always manually execute the program by hand before ever
attempting to execution it on machine.

11/05/2023 16
• Program Testing: The program should be tested against many possible inputs.Some of
the things we might check are whether the program solves the smallest possible
problem, whether it may not be possible or necessary to write programs that handle
all input conditions, all the time. Whenever possible, programs should be
accomplished by input and output section.
• Here are some desirable characteristics of program
– Integrity:R3fer to the accuracy of the program.
– Clarity: Refer to the overall readability of the program, with emphasis on its underlying
logic.
– Simplicity: The clarity and accuracy of the program are usually enhanced by keeping the things
as simple as possible, consistent with the overall program objectives.
– Efficiency: It is concerned with the execution speed and efficient memory utilization.
– Modularity:Many program can be decomposed into a
independent procedures or modules.
– Generality: Program must be as general as possible.
11/05/2023 17
11/05/2023 18

You might also like