48
Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Embed Size (px)

Citation preview

Page 1: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Fundamentals of C programming

- Ompal Singh

Sharda University,Gr.Noida

Page 2: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Topics

• Algorithm• Pseudo code• Flow Chart

Sharda University,Gr.Noida

Page 3: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Planning of Computer Programming- In today’s world a computer is used to solve various types of problems because it takes very less time as compared to a human being. 1. Analyze the given problem2. Divide the process used to solve the problem in a series of elementary task.3. Formulate the algorithm to solve the problem. Now in other words we say that “program must be planned before they are written”.

Page 4: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

ALGORITHMS

• A typical programming task can be divided into two phases:

• Problem solving phase– produce an ordered sequence of steps that describe

solution of problem– this sequence of steps is called an algorithm

• Implementation phase – implement the program in some programming language

Page 5: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Algorithm• The Term Algorithm refers to the logic of a

program. It is a step-by-step description of a solution to the given problem.

• When sequence of instructions are executed in the specified format and sequence the desired result are obtained.

• Definition:- A format or set of steps for solving a particular problem is called as algorithm.

• It has clear starting and stopping point. With specific step by step instructions in each line.

Page 6: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Characteristics of Algorithm

• Each instruction should be precise and clear.• Each instruction should be executed in a finite

time.• One or more instructions should not be

repeated Infinitely.• After executing the instructions the desired

result are obtained.• It range from Simple to the Complex.

Page 7: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Process of algorithm

• An algorithm must express the steps in the solution in a way that will be suitable for computer processing.

• It Read Value perform a simple procedure and output the required result.

• After algorithm a Flowchart is prepared and than it is run in the language form in the computer.

Page 8: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Three constructs

Page 9: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Algorithm• Find area of a rectangle • Step 1: Input L,B

Step 2: area=L*BStep 3: Print area

Step 4: Exit• Find the area of a circle• Step 1: Input R

Step 2: area=3.14*R*RStep 3: Print area

Step 4: Exit

Sharda University,Gr.Noida

Page 10: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

• Step 1: Input SP,CP Step 2: if (SP>CP) then

p=SP-CP Print p else

l=CP-SP Print l

endif Step 3:Exit

Sharda University,Gr.Noida

Page 11: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

• Detailed Algorithm • Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then

Print “FAIL” else

Print “PASS”endif

Step 4:Exit

Sharda University,Gr.Noida

Page 12: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Step 1: Input N1, N2, N3 Step 2: if (N1>N2) then if (N1>N3) then MAX ← N1 [N1>N2, N1>N3] else MAX ← N3 [N3>N1>N2] endif else if (N2>N3) then MAX ← N2 [N2>N1, N2>N3] else MAX ← N3 [N3>N2>N1] endif endif Step 3: Print “The largest number is”, MAX

Sharda University,Gr.Noida

Page 13: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

• Detailed Algorithm • Step 1: Input M1,M2,M3,M4

Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE > 60) then

Print “Ist” elseif (GRADE > 50)and (GRADE <60) then Print “IInd” else

Print “Fail”endif

Step 4:Exit

Sharda University,Gr.Noida

Page 14: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

• Step 1: Input the value of NStep 2: Set i=1Step 3: Repeat step 4 and 5 until i<=N

Step 4: Print i Step 5: Set i=i+1 end loop Step 6: Exit

Sharda University,Gr.Noida

Page 15: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudo code• First produce a general algorithm (one can use pseudo

code) • Refine the algorithm successively to get step by step

detailed algorithm that is very close to a computer language.

• Pseudo code is an artificial and informal language that helps programmers develop algorithms. Pseudo code is very similar to everyday English.

Sharda University,Gr.Noida

Page 16: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudocode• Pseudocode is an informal description of analgorithm• English-like statements that require less precisionthan a formal programming language.• Allows the designer to focus on the logic of thealgorithm without being distracted by details oflanguage syntax.• A good pseudocode algorithm should beindependent of, but easily translated into, any formalprogramming language.

Sharda University,Gr.Noida

Page 17: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudocode Control Structures• Sequence control structures Instructions are performed linearly• Conditional control structures A choice/selection is made between multiple courses of action• Looping/iteration control structures Enables repeating instructions within the algorithm A test condition determines when the repetition ends

Sharda University,Gr.Noida

Page 18: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudocode Control Structures

• Selection and conditional1. if (condition) then2. statement 13. ...4. endif• • If the (condition) is TRUE then

statements between THEN and ENDIF are executed once

Sharda University,Gr.Noida

Page 19: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

• Two-way decision1. if (condition) then2. statement 13. ...4. else5. statement n+16. ...7. endif

– • If the (condition) is TRUE then statements between THEN and ELSE are executed once

– • If the (condition) is FALSE then statements between ELSE and ENDIF are executed once

Sharda University,Gr.Noida

Page 20: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

LOOP– For Loop– Used to implement statements that needs to be

repeated a fixed number of times.

1. for i ←1 to n2. Statement 13. ...4. next i• i is the index or counter that starts with some value and

increases by one every time through the loop until it reaches the given maximum value

Sharda University,Gr.Noida

Page 21: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Repeat …until

– Repeat Loop– Used to implement statements that needs to be repeated until the condition becomes TRUE.

1. repeat2. Statement 13. ...4. until (condition)

Sharda University,Gr.Noida

Page 22: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

While LOOP

– While Loop– Used to implement statements that needs to be repeated while the condition is TRUE.

1. while (condition)2. Statement 13. ...4. endwhile

Sharda University,Gr.Noida

Page 23: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Advantages

• Converting a pseudo code to a programming language is easier than converting a flow chart to programming language.

• It is easier to modify the pseudo code of a program logic when program modification is necessary.

• Required less time and effort than flow chart.

Sharda University,Gr.Noida

Page 24: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Limitations

• A graphical representation of program logic is not available.

• There are no standard rules to follow in using pseudo code.

• It is very difficult for beginners.

Sharda University,Gr.Noida

Page 25: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudocode

• Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Sharda University,Gr.Noida

Page 26: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Pseudocode

Pseudocode:• Input a set of 4 marks• Calculate their average by summing and dividing by 4• if average is below 50

Print “FAIL”else

Print “PASS”

Sharda University,Gr.Noida

Page 27: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Flowchart• A Flowchart is a pictorial representation of an

algorithm.• The First flowchart is made by John Von

Newman in 1945.• It is a symbolic diagram of operation

sequence, dataflow, control flow and processing logic in information processing.

• The symbol used are simple and easy to learn.• It is a very help full tool for programmers and

beginners.

Page 28: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

The Flowchart• (Dictionary) A schematic representation of a sequence of operations,

as in a manufacturing process or computer program.• (Technical) A graphical representation of the sequence of operations in

an information system or program. Information system flowcharts show how data flows from source documents through the computer to final distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart.

Sharda University,Gr.Noida

Page 29: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

The Flowchart

A Flowchart– shows logic of an algorithm– emphasizes individual steps and their

interconnections– e.g. control flow from one action to the next

Sharda University,Gr.Noida

Page 30: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Purpose of flowchart

• Provide Communication.• Provide an overview.• Show all elements and its relationship.• Quick method of showing program flow.• Check program logic.• Facilitate coding.• Provide program revision.• Provide Program documentation.

Page 31: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Advantages of flowchart.

• Communication.• Effective analysis.• Proper documentation.• Efficient coding.• Proper debugging.• Efficient program maintenance.• Easy and clear presentation.

Page 32: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Limitation of flowchart

• Complex logic.• Drawing is time consuming.• Alteration and modification.• Redrawn many times.• Difficult to draw and remember.• Reproduction ( replica ).• Technical detail.

Page 33: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

Symbols used in flowchart

• All symbols are of different shape and size.• All have specific meaning and use.• The ANSI ( American National Standard

Institute) group categories symbols in 3 types basic, specialized and additional symbols.

• These institution and standardized these basic symbols for use.

Page 34: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

BASIC SYMBOLSTerminal Symbol = for start and stop.

INPUT and OUTPUT = Any function of input and output data.

Processing = An arithmetic and data movement instruction. Shows mathematical calculation and logic operations.

Page 35: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida

SYMBOLSDecision = Diamond indicate decision point in the program flow. IT may have 2 way branch or 3 way also.

Flow Lines = A Straight line between two boxes shows the path of logic flow in the program. An arrow head on the top of the line shows the direction of the flow, data from top to bottom and left to right.

Connectors = 2 small circles are used to connect separated portions of a flowchart without drawing lines between the parts. One connector indicated where the flow breaks off the other where it resumes.

Page 36: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Flowchart Symbols

Oval

Parallelogram

Rectangle

Diamond

Hybrid

Name Symbol Use in Flowchart

Denotes the beginning or end of the program

Denotes an input operation

Denotes an output operation

Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE)

Denotes a process to be carried oute.g. addition, subtraction, division etc.

Flow line Denotes the direction of logic flow in the program

Sharda University,Gr.Noida

Basic

Page 37: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Flowcharts for three constructs

Page 38: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example

Sharda University,Gr.Noida

PRINT“PASS”

Step 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then

Print “FAIL” else

Print “PASS” endif

START

InputM1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

ISGRADE<50

PRINT“FAIL”

STOP

YN

Page 39: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 2

• Write an algorithm and draw a flowchart to convert the length in feet to centimeter.

Pseudocode:• Input the length in feet (Lft)• Calculate the length in cm (Lcm) by multiplying

LFT with 30• Print length in cm (LCM)

Sharda University,Gr.Noida

Page 40: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 2

Algorithm • Step 1: Input Lft• Step 2: Lcm Lft x 30 • Step 3: Print Lcm

Sharda University,Gr.Noida

START

InputLft

Lcm Lft x 30

PrintLcm

STOP

Flowchart

Page 41: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 3

Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area.

Pseudocode • Input the width (W) and Length (L) of a rectangle• Calculate the area (A) by multiplying L with W• Print A

Sharda University,Gr.Noida

Page 42: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 3

Algorithm • Step 1: Input W,L• Step 2: A L x W • Step 3: Print A

Sharda University,Gr.Noida

START

InputW, L

A L x W

PrintA

STOP

Page 43: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 4

• Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation

• Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a

2 0ax bx c 2 4b ac

Sharda University,Gr.Noida

Page 44: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 4

Pseudocode: • Input the coefficients (a, b, c) of the quadratic

equation• Calculate d• Calculate x1• Calculate x2• Print x1 and x2

Sharda University,Gr.Noida

Page 45: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 4

• Algorithm: • Step 1: Input a, b, c• Step 2: d sqrt ( )• Step 3: x1 (–b + d) / (2 x a)• Step 4: x2 (–b – d) / (2 x a)• Step 5: Print x1, x2

4b b a c

Sharda University,Gr.Noida

START

Inputa, b, c

d sqrt(b x b – 4 x a x c)

Printx1 ,x2

STOP

x1 (–b + d) / (2 x a)

X2 (–b – d) / (2 x a)

Page 46: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Example 5

Page 47: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Thank you

Ompal Singh

Sharda University,Gr.Noida

Page 48: Fundamentals of C programming - Ompal Singh Sharda University,Gr.Noida

Sharda University,Gr.Noida