Anda di halaman 1dari 30

Chapter 1

Elementary Programming with C/Session 1/ 1 of 26

ALGORITHMS & FLOWCHARTS

Elementary Programming with C/Session 1/ 2 of 26

ALGORITHMS & FLOWCHARTS


Algorithms and flowcharts are two important tools used in problem solving. Problem solving is a creative process which defines the systematization and mechanization. It requires careful study, planning, logical precision and persistence.

Elementary Programming with C/Session 1/ 3 of 26

Solving a Problem
In order to solve a problem
Understand the problem clearly Gather the relevant information

Process the information

Arrive at the solution


Elementary Programming with C/Session 1/ 4 of 26

Steps involved in problem solving


Problem definition Analysis Design Coding Running the program Debugging Testing Documentation
Elementary Programming with C/Session 1/ 5 of 26

Problem Definition

Understanding the problem thoroughly in terms of the requirements. Capability to extract a set of well defined tasks, to obtain the desired output. If the problem is not properly interpreted then we can not obtain the desired results.
Elementary Programming with C/Session 1/ 6 of 26

Analysis
The given problem must be analyzed before it is solved.
We should specify the operations (Arithmetic & logical) to be performed on them and checking the suitability in terms of memory & execution time

Elementary Programming with C/Session 1/ 7 of 26

Design
Design of problems specifies the tools and techniques used to solve the problem. Design of techniques involves three ways 1.Algorithm 2 Flowchart 3.Pseudocode.

Elementary Programming with C/Session 1/ 8 of 26

Algorithm
Algorithm is defined as a step by step procedure to solve a particular problem It consists of set of statement, and each statement must be well defined to perform a specific operation for required result. The word algorithm is named after the famous ninth century Arabic author and mathematician, Abu Jafar Mohammed Ibn Musa Al Khowarizmi

Elementary Programming with C/Session 1/ 9 of 26

The last term of the name took different Pronunciations over the period such as Alkhowarism, Algorism & finally it became Algorithm.

Elementary Programming with C/Session 1/ 10 of 26

Characteristic of algorithm
Input Output

Well defined Finiteness

Effectiveness
Elementary Programming with C/Session 1/ 11 of 26

Each & every algorithm is characterized by the following five characteristics 1.Input : It may accept zero or more inputs. 2.Output: It should produce at least one output(result) 3. Definiteness : Each instruction must be clear, welldefined and precise. There should not be any ambiguity. 4. Finiteness : It should be a sequence of finite instructions. That is, it should end after a fixed time. It should not enter an infinite loop. 5. Effectiveness : This means that operation must be simple and carried out in a finite time at one or more levels of complexity. It should be effective whenever traced manually for the results.

Elementary Programming with C/Session 1/ 12 of 26

Notations of algorithm
While writing algorithms the following notations are considered 1.Name of the algorithm : It specifies the problem to be solved. 2Step Number : Identification tag of an instruction and it is an unsigned positive integer. 3.Explanatory Comment : It follows the step number and describes the operation. It should be written within a pair of invaded codes. 4.Termination : It specifies the end of the algorithm It is generally a STOP statement & the last instruction in the algorithm.
Elementary Programming with C/Session 1/ 13 of 26

Example
Algorithm to compute the area of circle
Algorithm: Area of circle Step1 : Read radius

Step2 : [Compute the area]


Area=3.142 * radius* radius Step3 : [Print the area]

Print Area of a circle, Area


Step4 : [End of algorithm] stop
Elementary Programming with C/Session 1/ 14 of 26

Flowchart
The flow chart can be defined as a visual or graphical or diagrammatic representations of an algorithm. It is referred to as blue print of an algorithm Flow charts make use of geometrical figures, to specify particular operation.

Elementary Programming with C/Session 1/ 15 of 26

The flow chart is easy to understand and analyze the problem. It is an useful aid for programmers and system analysts. It is machine independent. They can be used for any type of problems. Flow charts are classified into two types. They are 1.Program flowcharts. 2.System flowcharts.

Elementary Programming with C/Session 1/ 16 of 26

The Flowchart Symbol

Elementary Programming with C/Session 1/ 17 of 26

Draw a flowchart to find the area of circle

Elementary Programming with C/Session 1/ 18 of 26

Pseudocode
The pseudo code is neither an algorithm nor a program.
It consists of an English like statements which perform the specific operations. It is defined for an algorithm. It doesn't use any graphical representation.

Elementary Programming with C/Session 1/ 19 of 26

In pseudo code, the program is represented in terms of words and phrases, but the syntax of program is not strictly followed. Advantages: Easy to read Easy to understand Easy to modify

Elementary Programming with C/Session 1/ 20 of 26

Ex-Write a pseudo code to perform the basic arithmetic operations


Read n1,n2 Sum=n1+n2 Diff=n1-n2 Mult=n1*n2 Quot=n1/n2 Print sum,diff,mult,quot End

Elementary Programming with C/Session 1/ 21 of 26

Coding
A program is a set of instructions to solve a particular problem by the computer and actual process of writing a program is called coding. Coding is done by using a specific language, to execute a program.

Elementary Programming with C/Session 1/ 22 of 26

Running the program


Running of program involves three steps :-

Understand the instructions Store data and instructions Perform computations

Elementary Programming with C/Session 1/ 23 of 26

Debugging
The process of detecting and correcting errors is known as debugging. Three types of error :1. Syntax errors 2. Logical errors 3. Run-time errors

Elementary Programming with C/Session 1/ 24 of 26

SYNTAX ERRORS
This type of errors are the result of violation of rules of programming language. It is easy to debug these error. On encountering these error , system will displays error message with line number.

Elementary Programming with C/Session 1/ 25 of 26

LOGICAL ERROR
These error occur during coding process. While coding the program ,programmer must process the correct operation. During this error the program will execute but produce some unwanted error. We can eliminate it by tracing it again.

Elementary Programming with C/Session 1/ 26 of 26

RUN-TIME ERROR
These error occur due to unclear instructions. For ex. An infinite loop in program which causes no output. On encountering these error , system will displays error messageThese are also occur due to device errors, improper sequencing of constructs, errors in system software, incorrect data input, etcThe computer will print the error message. Some of the runtime errors are Divide by zero, Null pointer assignment & Data overflow

Elementary Programming with C/Session 1/ 27 of 26

TESTING
The process of running the program to test the correctness of outputs of a problem is called testing. The program is tested by executing with different sets of data. Logical errors are the outcome this process.

Elementary Programming with C/Session 1/ 28 of 26

Documentation
While writing programs, it is a good
programming practice to make a brief explanatory note on the program or program segments. This explanatory note is called a comment. It explains how the program works and how to interact with it. Thus, it helps other programmers to understand the program.

Elementary Programming with C/Session 1/ 29 of 26

1.

2.

There are two types documentation. They are Internal documentation : This documentation is a comment statement within a program. It describes the function of the program or segments. These statements are not translated to machine language. Translator simply discard these statements External documentation : This documentation is an executable statement in a program. It may be a message to the user to respond to the program requirement. This is accomplished using output statements. It makes the program more attractive and interactive.

Elementary Programming with C/Session 1/ 30 of 26

Anda mungkin juga menyukai