You are on page 1of 55

Unit 1 : Introduction (3

Hrs)

1
Content of the unit
• History and Advantages of C, Problems Analysis, Algorithm and
Flowchart (Introduction and characteristics of algorithm, introduction
and symbols of flowchart start/stop, read/print, processing
statement, condition check, direction of flow), Structure of a C
program (preprocessor directive, #include and #define directive,
header files and library files); Writing, compiling, Debugging,
Executing and Testing a C program in windows and Linux/Unit like
environment (Compiler, integrated development environment,
compiling and linking, compiler error, linker error and run-time error)

2
3
Problem Solving
• Problem solving is systematic process to find and implement the
solution to a problem.

• The computer is a symbol manipulating machine that solves the


problem by following the set of sequential instructions called
program.

• When a problem is solved by paper and pencil then we can write


algorithm and program.

4
Program
• A set of instructions to solve the problem is called program.

• Program is specification of the sequence of computational steps in


programming language.

• The process of writing a program to solve a particular problem is


called programming and person who writes program is called
programmer

5
Steps in problem solving by computer
1. Problem Definition
2. Problem Analysis
3. Designing a program
4. Coding
5. Testing
6. Installation and Maintenance

6
1 Problem Definition
• To solve a problem, the first step is to identify and define the
problem.

• The problem is stated clearly , accurately and unambiguously.

7
2 Problem Analysis
• In this phase, we determine input and output of the problem.

• The domain of input and domain output is also determined.

• The problem analysis helps in designing and coding.


• Input Specifications : The number of inputs and what forms of input are
available

• Output Specifications: The number of outputs and what forms the output
should be displayed
8
3 Designing a Program
• Formulate an algorithm and flowchart to solve the problem

• Algorithm: Step by step procedure of solving a problem.

• Flowchart: It is the graphical representation of the algorithm

9
4 Coding
• Writing instructions in a programming language to solve a problem.

• The compiler will convert the program code the machine language
which the compiler can understand.

10
5 Program Testing
• A program is further analyzed to see if is working or not for every
possible of inputs for which it is expected to run.

• In this phase, the program is tested for its completeness, correctness,


reliability and maintainability.
• There are different types of tests such as unit test, integration test etc

11
6 Installation and Maintenance
• Installation of a computer program is the act of making the program
ready for execution.

• Maintenance means periodic review of the programs and


modifications based on user’s requirements

• Users requirements are dynamic (changing with time) and hence its
requires updates in the program

12
Coding, compilation and Execution
• Problem Definition

Problem Analysis

Designing a Problem

Coding

Testing

Installation and Maintenance


13
Coding
• Coding is the translation of an algorithm or flowchart into program
using programming language like C, C++ or java etc.

• Coding is the real job of programmer.

• The code written by programmer using some programming language


is called source code

14
Compilation and Execution
• The source code written any programming language is not directly
executed by the computer.
• It should be translated into the machine readable format.
• The process of translation of source code into the target code is called
the compilation.
• Each programming language has its own compiler program that
translates the source code into its target code.
• The converted program in actual machine language is then executed
by the computer which is known as program execution

15
Debugging and Testing
• A written program may contain errors, some errors can be deleted by the
language compilers and some errors cannot be identified by the compiler
and occurred during the program run.
• Two types errors are common
• Syntax Errors: Identified by the compiler. Errors related the grammar of
programming language
• Logical Errors: Not identified by the compiler. Errors related to logic of the
problem such as mistakes in formula etc
• Testing is the process of checking the program for its correct functioning by
executing the program with some input data set and observing the output
of the program
16
Documentation
• From the start of the problem solving to the end of the implementation
of the program, all the tasks should be documented.
• It is also the important part of the problem solving.
• Documentation may be of two types
a. Technical Documentation: It is also known as programmer’s
documentation which includes the problem analysis to
implementation details for that program. It is needed for future
reference for any modification, update of the program
b. User Manual : User manual is the documentation prepared for the
end-user of the program that guides the user to operate the program
17
Algorithm
• An algorithm is a step by step description of the procedure written in human
understandable language for solving given problem.

• There may be many algorithms to solve a given problem.


• The language of algorithm is independent of programming languages
• Some characteristics of algorithms
1. Input: The algorithm takes input from user
2. Output: The algorithm returns output after processing inputs
3. Finiteness: The number of steps in an algorithm is finite
4. Definiteness: Each and every step of algorithm is unambiguous
18
Advantages of Algorithms
• An algorithm are easy to understand

• Algorithm is programming language independent

• Algorithm makes the problem simple, clear and easy to understand

19
Example 1: Write an algorithm to find sum
of two numbers
• Step 1: Start
• Step 2: Read any numbers A and B
• Step 3: Compute C = A+B
• Step 4: print “C”
• Step 5: Stop

20
Example 2 Write an algorithm that finds
simple interest
• Step 1: Start
• Step 2: Read P, T and R
• Step 3: Compute I = (P*T*R)/100
• Step 4: Print “I”
• Set 5: Stop

21
Example 3: Write an algorithm that finds
area and perimeter of rectangle
• Step 1: Start
• Step 2: Read length “L” and breadth “B”
• Step 3: Compute A = L*B
• Step 4: Compute P = 2*(L+B)
• Step 5: Print “ A” and “P”
• Step 6 : Stop

22
Example 4:Write an algorithm that reads a
number and checks if the number is even or odd
• Step 1: Start
• Stet 2: Read any number “N”
• Step 3: Compute R = N mod 2
• Step 4: If R = 0 then print “ N is even”
• else print “N is odd”
• Step 5: Stop

23
Example 5: Write an algorithm that reads a number
and tests whether it is negative or positive
• Step 1: Start
• Step 2: Read number “N”
• Step 3: If N>=0 then print” N is positive or equal to zero”
• else print “ N is negative”
• Step 4: Stop

24
Example 6: Write an algorithm that reads
different two numbers and find the larger
one
• Step 1: Start
• Step 2: Read any two number A, B
• Step 3: If A>B then print “ A is larger one”
• else print “B is larger one”
• Step 4: Stop

25
Example 7 : Write an algorithm to the
largest number among three numbers
• Step 1: Start
• Step 2: Enter any three numbers X, Y ,Z
• Step 3: If (X>Y and X>Z) then print “ X is the largest”
• else if (Y>X and Y>Z) then print “ Y is the largest”
• else
• print “ Z is the largest”
• Step 4 : Stop

26
Example 8: Write an algorithm and draw flowchart
hat finds sum of first 100 natural numbers
• Step 1 : Start
• Step 2: Let N = 100
• Step 3: Compute S = N*(N+1)/2
• Step 4: print “ S”
• Step 5: Stop

27
Write an algorithm that reads n numbers
and finds the largest one
• Step 1: Start
• Step 2: Read ‘n’ numbers A1,A2….An
• Step 3: let max = A1, i=2
• Step 4: if (max<Ai) then max = Ai
• Step 5 set i = i+1
• Step 6 if i<=n then goto 4
• Step 7 : print “ max”
• Step 8 : Stop

28
Write an algorithm that finds roots of
quadratic equation (AX2+BX+C=0)
• Step 1: Start
• Step 2: Read coefficients A,B ,C
• Step 3: compute D = B*B-4*A*C
• Step 4: If (D<0) then print “ Imaginary Roots” and goto step 6
• else if(D=0) then R1 = R2 = -B/(2*A)
• else
• R1 = (-B+√(D)/(2*A)) and R2 = (-B-√(D)/(2*A))
• Step 5: print “R1 and R2”
• Step 6: Stop

29
Write an algorithm to test whether the number N>1 is prime of composite

• Step 1: Start
• Step 2: Read any number “N” // 5
• Step 3: Let I = 1, C=0
• Step 4: Compute R = N mod I// R = 5 mod 5 = 0
• Step 5: I = I +1 // I = 6
• Step 6 If R = 0 then C = C+1 // C = 2
• Step 7: If I<=N goto step 4// 6<=5
else goto step 8
• Step 8: if C = 2 then print “ N is prime”
• else print “ N is composite”
• Step 9 : Stop
30
Flowchart
• A flowchart is a step by step diagrammatic representation of the logic
paths to solve a given problem.

• It is a graphical representation of algorithm.

• Flowchart uses different symbols for performing different tasks.

• The symbols and their meaning is as follows

31
Flowchart Symbols and Their Meaning

32
Advantages of Flowchart
• A flowchart shows the logic of a problem displayed in pictorial form

• It is useful for debugging and testing of programs.

• Program could be coded efficiently using flowchart

• The flowchart is good means of communication to other users

33
Disadvantages of Flowchart
• It is not useful to represent complex program logic

• For many alternations, the flowcharts have to be redrawn completely

34
Rules of drawing Flowchart
• The flowchart should be clear, neat and easy to follow
• It should be drawn from top to bottom
• The flowchart always starts with start symbol and ends with stop
symbol.
• Flow lines are used to join the symbols
• Decision box should have one entry point and two exit points.
• For lengthy flowchart, connectors are used to join them

35
Draw flowchart to compute simple interest

36
Write an algorithm and draw flowchart to
test whether a number is even or odd
• Step 1: Start
• Step 2: Enter any number N
• Step 3: if N % 2 ==0 the print “The number is even”
• else print” The number is odd”
• Step 4: Stop

37
Flowchart

38
Write an algorithm and draw flowchart that
finds the largest number among three numbers
• Algorithm
• Step 1: Start
• Step 2: Input any three numbers A, B, C
• Step 3: If (A>B) and (A>C) then print “A is the largest”
• else if (B>A) and (B>C) then print “ B is the largest”
• else print “ C is the largest”
• Step 4: Stop

39
Flowchart

40
Write an algorithm and draw flowchart that
tests whether the number is prime or composite
• Step 1: Start
• Step 2: Read number N
• Step 3: Let I=2, flag =1
• Step 3
• Step 4 Compute R = N mod I
• Step
• Step 5: I = I+1
• Step 6

41
42
Introduction to C
• C programming was developed by Dennis Ritchie at AT & T Bell Labs in
1972.
• It is a high level programming language used to develop applications
for high level business and low level system programs
• C is popular because of its power, simplicity and ease of use
C is reliable, simple and easy to use

43
Features of C
• Robust language, which can be used to write any complex program
• Has rich set of built-in functions and operators
• Well suited for writing both system software and business
applications
• Efficient and faster in execution
• Highly portable
• Well suited for structured programming
• Dynamic memory allocation

44
History of C
• C language has evolved from three different structured language
ALGOL, BCPL and B language.
• It uses many concepts from these languages and introduced many
new concepts such as data types, structure, pointer.

45
Structure of C program

46
Structure of C program

47
1 Documentation Section
• The documentation section is first part of the program where the
programming gives the details associated with the program such as
names of programmer, function of program etc.
• /* C program to find factorial of a number
• programmer: XYZ
• Date : 2078/01/14
• */

48
2 Link Section
• The link section provides instructions to the compiler to link functions
from the system library such as using the include directive as below
• #include<stdio.h>
• #include<math.h>

• The directive “stdio.h” is an input output header file which contains


the input /output file handling functions which are used in C program

49
3 Definition Section
• The definition section defines all symbolic constants such as using the
#define directive
• For example:
• #define PI 3.1416

50
4 Global Declaration Section
• There are some variables that are used in more than one function.
• Such variables are called global variables and are declared in the
global declaration section that is outside of all the functions.
• This section also declares all the user user-defined functions

• Eg int N= 100;
• int sum(int a,int b);

51
5 Main () Function Section
• Every C program needs to have the main function.
• Each main function contains two parts
• Declaration Part: The declaration part declares all the variable used in
the executable part
• Executable Part: There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces.
The program execution starts at the opening brace and ends at the
closing brace.
• The closing brace of the main program is the logical end of the program

52
5 Main () Function Section
• For example
• Int main()
•{
• int a =10;
• int b = 20;
• int c = a+b;
• printf(“The sum = %d”,c);
• }

53
6 Subprogram Section
• User can define their own functions in this section which perform
particular task as per the user requirement.
• So user create this according their needs.
• Eg.
• int sum(int a , int b)
•{
• return a+b;
•}

54
Executing a C Program

55

You might also like