Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of C programming

Similar presentations


Presentation on theme: "Fundamentals of C programming"— Presentation transcript:

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

2 Sharda University,Gr.Noida
Topics Algorithm Pseudo code Flow Chart Sharda University,Gr.Noida

3 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 problem 2. 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”.

4 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

5 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. Sharda University,Gr.Noida

6 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. Sharda University,Gr.Noida

7 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. Sharda University,Gr.Noida

8 Three constructs

9 Sharda University,Gr.Noida
Algorithm Find area of a rectangle Step 1: Input L,B Step 2: area=L*B Step 3: Print area Step 4: Exit Find the area of a circle Step 1: Input R Step 2: area=3.14*R*R Sharda University,Gr.Noida

10 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

11 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

12 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 if (N2>N3) then MAX ← N2 [N2>N1, N2>N3] MAX ← N3 [N3>N2>N1] Step 3: Print “The largest number is”, MAX Sharda University,Gr.Noida

13 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

14 Sharda University,Gr.Noida
Step 1: Input the value of N Step 2: Set i=1 Step 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

15 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

16 Sharda University,Gr.Noida
Pseudocode Pseudocode is an informal description of an algorithm English-like statements that require less precision than a formal programming language. Allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. A good pseudocode algorithm should be independent of, but easily translated into, any formal programming language. Sharda University,Gr.Noida

17 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

18 Pseudocode Control Structures
Selection and conditional 1. if (condition) then 2. statement 1 3. ... 4. endif • If the (condition) is TRUE then statements between THEN and ENDIF are executed once Sharda University,Gr.Noida

19 Sharda University,Gr.Noida
Two-way decision 1. if (condition) then 2. statement 1 3. ... 4. else 5. statement n+1 6. ... 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

20 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 n 2. Statement 1 3. ... 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

21 Sharda University,Gr.Noida
Repeat …until Repeat Loop– Used to implement statements that needs to be repeated until the condition becomes TRUE. 1. repeat 2. Statement 1 3. ... 4. until (condition) Sharda University,Gr.Noida

22 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 1 3. ... 4. endwhile Sharda University,Gr.Noida

23 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

24 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

25 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

26 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

27 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. Sharda University,Gr.Noida

28 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

29 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

30 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. Sharda University,Gr.Noida

31 Advantages of flowchart.
Communication. Effective analysis. Proper documentation. Efficient coding. Proper debugging. Efficient program maintenance. Easy and clear presentation. Sharda University,Gr.Noida

32 Limitation of flowchart
Complex logic. Drawing is time consuming. Alteration and modification. Redrawn many times. Difficult to draw and remember. Reproduction ( replica ). Technical detail. Sharda University,Gr.Noida

33 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. Sharda University,Gr.Noida

34 Sharda University,Gr.Noida
BASIC SYMBOLS Terminal 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. Sharda University,Gr.Noida

35 Sharda University,Gr.Noida
SYMBOLS Decision = 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. Sharda University,Gr.Noida

36 Sharda University,Gr.Noida
Flowchart Symbols Basic Sharda University,Gr.Noida

37 Flowcharts for three constructs

38 Sharda University,Gr.Noida
Example START Input M1,M2,M3,M4 GRADE(M1+M2+M3+M4)/4 IS GRADE<50 PRINT “FAIL” STOP Y N 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 PRINT “PASS” Sharda University,Gr.Noida

39 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

40 Sharda University,Gr.Noida
Example 2 Algorithm Step 1: Input Lft Step 2: Lcm  Lft x 30 Step 3: Print Lcm Flowchart START Input Lft Lcm  Lft x 30 Print Lcm STOP Sharda University,Gr.Noida

41 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

42 Sharda University,Gr.Noida
Example 3 Algorithm Step 1: Input W,L Step 2: A  L x W Step 3: Print A START Input W, L A  L x W Print A STOP Sharda University,Gr.Noida

43 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 Sharda University,Gr.Noida

44 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

45 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 START Input a, b, c d  sqrt(b x b – 4 x a x c) Print x1 ,x2 STOP x1 (–b + d) / (2 x a) X2  (–b – d) / (2 x a) Sharda University,Gr.Noida

46 Example 5

47 Sharda University,Gr.Noida
Thank you Ompal Singh Sharda University,Gr.Noida

48 Sharda University,Gr.Noida


Download ppt "Fundamentals of C programming"

Similar presentations


Ads by Google