Unit 1: Problem Solving with Computer (2 Hrs.
Presented By : Tekendra Nath Yogi
Tekendranath@gmail.com
College Of Applied Business And Technology
Contd…
• Outline:
– Problem solving by using computer:
• Problem analysis
• Algorithm development and Flowcharting
• Coding, Compilation and Execution
• Debugging, Testing and Documentation
– Introduction of C
– Structure of C program
• Some example programs
– Compiling and Executing C program
– Header files and Library files
– Preprocessor directives
– Types of errors in a Program
4/15/2021 By: Tekendra Nath Yogi 2
Introduction
• Number of problems in our daily life.
– E.g., sometime we have to calculate simple interest based on given
principal, time and interest rate and sometimes we have to prepare
mark sheet of students.
• We can solve such problems by using computer. But a
computer is a DUMB machine. So, a computer can not do
anything alone without software .
• The software gives life to the computer.
4/15/2021 By: Tekendra Nath Yogi 3
Contd…
• A software is a set of programs written to solve a particular
problem.
• A program is a set of instructions on the basis of which
computer gives result/ output.
• If the instructions are not correct, the computer gives wrong
result.
4/15/2021 By: Tekendra Nath Yogi 4
Contd…
• Never Ever forget!
– Just writing a code is not sufficient to solve a problem.
– Program must be planned before coding in any computer language
available.
– There are many activities to be done before and after writing the code.
– These activities are known as steps of solving a problem using
computer.
4/15/2021 By: Tekendra Nath Yogi 5
Stages while solving a problem using computer
• There are number of steps while solving a problem using computer. They
are:
– Problem analysis
– Algorithm Development
– Flowcharting
– Coding
– Compilation and Execution
– Debugging and Testing
– Documentation
4/15/2021 By: Tekendra Nath Yogi 6
Contd…
• Problem Analysis:
– The user’s requirements can not be fulfilled without clear
understanding of his/her problem in depth.
– Inadequate identification of problem may cause program less useful
and inefficient.
– So, We need to analyze and understand it well before solving.
– problem analysis is the Process of becoming familiar with the
problem.
– E.g., add two number
4/15/2021 By: Tekendra Nath Yogi 7
Contd…
• Algorithm Development:
– Algorithm is a Procedure for solving a problem in a finite
number of steps.
– Helps to make clear understanding about the processing
steps to be performed by the computer in order to solve the
given problem.
4/15/2021 By: Tekendra Nath Yogi 8
Contd…
• Some conventions used in developing algorithms:
– Each algorithm can be enclosed by two statements START and STOP.
– To accept data from user, the INPUT or READ statement is used.
– To display any user message, the PRINT or DISPLAY statement is
used.
– Any operators can be used in the statements (e.g., +, -, …., <, >….
AND, OR, NOT……etc).
– Sequence( also known as process): If a previous step must be executed
before any other next steps then specify steps in a sequence.
4/15/2021 By: Tekendra Nath Yogi 9
Contd…
• Decision(also known as selection):
– If the outcome of the decision is true, one thing is done otherwise other.
Then specify algorithm step as shown below:
If condition then process1
Else process2
– E.g., If number is exactly divisible by 2, then the number is even
otherwise odd.
4/15/2021 By: Tekendra Nath Yogi 10
Contd…
• Repetition(also known as iteration or loop):
– Repetition can be implemented using construct like the repeat, loop,
while….. Etc.
– For example:
• Repeat
– Fill water in the kettle Until Kettle is full
4/15/2021 By: Tekendra Nath Yogi 11
Contd…
• Example1: An algorithm to find sum of two numbers:
– Step1: Start
– Step2: Read A and B
– Step3: C= A+B
– Step4: Display “C as sum of two numbers”
– Step5: Stop
4/15/2021 By: Tekendra Nath Yogi 12
Contd…
• Example2: An algorithm to find the greatest of two input
numbers
– Step1: Start
– Step2: Read A and B
– Step3: if A>B
then Display A is large
else
Display B is large
– Step4: Stop
4/15/2021 By: Tekendra Nath Yogi 13
Contd…
• Example3: An algorithm to find sum of first 5 natural
numbers
– Step1: Start
– Step2: Initialize count=0 and sum=0
– Step3: Repeat until count<=5
• Count= count+1
• Sum=sum+count
– Step4: Stop
4/15/2021 By: Tekendra Nath Yogi 14
Contd…
• Flowcharting:
– Graphical representation of an algorithm using standard symbols.
– Includes a set of various standard shaped boxes that are interconnected
by flow lines.
– Flow lines have arrows ( Direction of flow).
– Activities are written within boxes in English.
– Helps to establish Communication between programmers and business
persons
4/15/2021 By: Tekendra Nath Yogi 15
Contd…
• Flowchart symbols:
4/15/2021 By: Tekendra Nath Yogi 16
Contd…
• Things to consider:
– There should be start and stop to the flowchart.
– Only one flow line should emerge from a process symbol.
– Only one flow line should enter a decision symbol, but two or more
flow lines can leave the decision symbol.
4/15/2021 By: Tekendra Nath Yogi 17
Contd…
• Example 1: Write an algorithm and draw flowchart for finding the sum of
any two numbers.
– Step1: Start
– Step2: Read A and B
– Step3: C= A+B
– Step4: Display “C as
sum of two numbers”
– Step5: Stop
4/15/2021 By: Tekendra Nath Yogi 18
Contd…
• Example2: Write an algorithm and draw flowchart to find the
greatest of two input numbers
– Step1: Start
– Step2: Read A and B
– Step3: if A>B
then Display A is large
else
Display B is large
– Step4: Stop
4/15/2021 By: Tekendra Nath Yogi 19
Contd…
• Example3: An algorithm to find sum of first 5 natural
numbers
– Step1: Start
– Step2: Initialize count=0 and sum=0
– Step3: Repeat until count<=5
• Count= count+1
• Sum=sum+count
– Step4: Stop
4/15/2021 By: Tekendra Nath Yogi 20
Contd…
• Advantages of Flowcharts:
– Communication: Quickly provide ideas and descriptions of
algorithms.
– Effective Analysis: Clear overview of the entire program.
– Proper documentation: Documents the steps followed in an algorithm.
That helps us to understand its logic in future.
– Efficient Coding: More ease with comprehensive flowcharts as a guide.
– Easy in debugging and program maintenance: Debugging and
maintenance of operating program.
4/15/2021 By: Tekendra Nath Yogi 21
Contd…
• Limitations of using flowcharts:
– Complex Logic: A flowchart becomes complex and clumsy when the
program logic is quite complicated.
– Difficulty in alteration and modification: If alterations are required ; the
flowchart may need to be redrawn completely.
4/15/2021 By: Tekendra Nath Yogi 22
Contd…
• Coding:
– The process of transforming the program logic design into computer
language format.
• i.e., An act of transforming operations in each box of the flowchart
in terms of the statement of the program.
– The code written using programming language is also known as source
code.
4/15/2021 By: Tekendra Nath Yogi 23
Contd…
• Example1:
4/15/2021 By: Tekendra Nath Yogi 24
Contd…
• Example2:
4/15/2021 By: Tekendra Nath Yogi 25
Contd…
• Example 3:
4/15/2021 By: Tekendra Nath Yogi 26
Contd…
• Compilation:
– Process of changing high level language into machine level language.
– It is done by special software, COMPILER
– The compilation process tests the program whether it contains syntax
errors or not.
– If syntax errors are present, compiler can not compile the code.
4/15/2021 By: Tekendra Nath Yogi 27
Contd…
• Execution:
– Once the compilation is completed then the program is linked with
other object programs needed for execution, there by resulting in a
binary program and then the program is loaded in the memory for the
purpose of execution and finally it is executed.
– The program may ask user for inputs and generates outputs after
processing the inputs.
4/15/2021 By: Tekendra Nath Yogi 28
Contd…
• Debugging and Testing:
– Some errors may remain in the program because the designer/
programmer might have never thought about a particular case.
– Testing: is a process which is used to discovering the system to find
defects existing in the software.
– Debugging: After receiving issue report of defect from testing team,
developers start debugging. The purpose of this phase is to trace the
bug and make the software error free.
4/15/2021 By: Tekendra Nath Yogi 29
Contd…
• Debugging and Testing:
4/15/2021 By: Tekendra Nath Yogi 30
Contd…
• Program documentation:
– Helps to those who use, maintain and extend the program in future.
– A program may be difficult to understand even to programmer who
wrote the code after some days.
– Properly documented program is necessary which will be useful and
efficient in debugging, testing, maintenance and redesign process
4/15/2021 By: Tekendra Nath Yogi 31
Contd…
• Two types of documentations:
– Programmer’s documentation(Technical documentation):
• Maintain, redesign and upgrade
• DFD, ER, algorithm and flowchart
– User documentation(user manual):
• Support to the user of the program
• Instructions for installation of the program
4/15/2021 By: Tekendra Nath Yogi 32
Introduction to C
• C is general-purpose, procedural, high level programming language.
• However, often referred as a middle-level programming language; since
it provides rich support to low-level programming constructs.
• C is a successor of 'Basic Combined Programming Language' (BCPL)
called B language.
• Developed by Dennis Ritchie at AT&T’s Bell laboratories in 1972.
4/15/2021 By: Tekendra Nath Yogi 33
Contd…
• C has become very popular due to the following reasons:
– One of the early programming languages.
– Still, the best programming language to learn quickly.
– C language is simple and easy to use.
– Modern programming concepts are based on C.
– It can be compiled on a variety of computer platforms.
4/15/2021 By: Tekendra Nath Yogi 34
Contd…
• Uses of C programming language: C can be used for developing
system software as well as application software.
• Example:
– Database Systems: E.g. MYSQL
– Language Interpreters: PHP
– Compilers and Assemblers: e.g., c++, Java, etc…
– Operating Systems: e.g., LINU, UNIX, etc
– Network Drivers
– Word Processors
4/15/2021 By: Tekendra Nath Yogi 35
Contd…
• Features of C:
– C is a robust language with a rich set of built-in functions and
operators.
– Programs written in C are efficient and fast.
– C is highly portable, programs once written in C can be run on other
machines with minor or no modification.
– C is easily extensible. i.e., C is a collection of C library functions; we
can also create our function and add it to the C library.
4/15/2021 By: Tekendra Nath Yogi 36
Basic structure of c programs
• A C program may contain one or more sections as shown below.
• A function is a set of one or more statements designed to perform a specific task.
4/15/2021 By: Tekendra Nath Yogi 37
Contd…
• Documentation section:
– The documentation section consists of a set of comment lines giving
the name of the program, the author and other details, which the
programmer would like to use later.
– Act as a user manual.
– E.g., /* This program adds two integer numbers*/
4/15/2021 By: Tekendra Nath Yogi 38
Contd…
• Link section:
– The link section provides instructions to the compiler to link functions
from the system library.
– E.g., The statement #include<stdio.h> links I/O functions like printf()
and scanf() with the program.
4/15/2021 By: Tekendra Nath Yogi 39
Contd…
• Definition section:
– The definition section defines all symbolic constants using the #define
directive.
– E.g., #define PI 3.14
4/15/2021 By: Tekendra Nath Yogi 40
Contd…
• 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-defined functions.
4/15/2021 By: Tekendra Nath Yogi 41
Contd…
• main ( ) function section:
– Every C program must have one main function section. This
section contains two parts; declaration part and executable part
• Declaration part: The declaration part declares all the variables 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 begins at the opening brace and ends at the closing
brace. The closing brace of the main function is the logical end of the program.
All statements in the declaration and executable part end with a semicolon.
4/15/2021 By: Tekendra Nath Yogi 42
Contd…
• Subprogram section:
– If the program is a multi-function program then the subprogram section
contains all the user-defined functions that are called in the main ()
function.
– User-defined functions are generally placed immediately after the main
() function, although they may appear in any order.
Note: All section, except the main ( ) function section may
be absent when they are not required.
4/15/2021 By: Tekendra Nath Yogi 43
Contd…
• Example 1:
• Output:
4/15/2021 By: Tekendra Nath Yogi 44
Contd…
• Example 2:
• Output:
4/15/2021 By: Tekendra Nath Yogi 45
Contd…
• Example 3:
Documentation section
link section
definition section
global declaration section
Main function section
Sub-program section
• Output:
4/15/2021 By: Tekendra Nath Yogi 46
Executing a C program
• Executing a program written in c involves a series of steps. These are:
– Creating the program
– Compiling the program
– Linking the program with functions that are needed from the c library
and
– Executing the program
• The following figure illustrates the process of creating, compiling and
executing a C program.
4/15/2021 By: Tekendra Nath Yogi 47
Contd…
Fig: Process of compiling and running a C program
4/15/2021 By: Tekendra Nath Yogi 48
Preprocessor directive
• Also known as preprocessor commands.
• Are placed in the source program at the beginning before the main.
• Syntax: They all begin with the symbol # and do not require semicolon at
the end.
– E.g., #include<stdio.h>
• Three categories of directives:
– Macro substitution directives: e.g. #define directive
– File inclusion directives: e.g., #include directive
– Compiler control directives: e.g., #ifdefine directive
4/15/2021 By: Tekendra Nath Yogi 49
Contd…
• #define directive:
– Is a macro substitution directive.
– Syntax: #define IDENTIFIER string
– If this statement is included in the program at the beginning, then the
preprocessor replaces every occurrence of the IDENTIFIER in the source by the
string
4/15/2021 By: Tekendra Nath Yogi 50
Contd…
• #include directive:
– C programs are divided into modules or functions. Some functions are
written by users and some are stored in C library. Library functions are
grouped category wise and stored in different files known as header
files. To access the file stored in the library, it is necessary to tell the
compiler about the files to be accessed. This is achieved by the pre-
processor directive #include as follows,
– Syntax: #include<filename>
– E.g.,: #include<stdio.h>
– If this statement is included in the program at the beginning, then the
preprocessor inserts the entire contents of the file “filename” into the
source code of the program.
4/15/2021 By: Tekendra Nath Yogi 51
Header files and Library files
• Header Files :
– These are the files that are included at the top of any program. If we use
any function inside a program, then the header file containing
declaration of that function ,has to be included. Like printf() is declared
in stdio.h. So, we must include it (by #include in order to use printf().
• Library Files:
– These are the files which the compiler uses in order to define the
functions which have been used in the program and had been declared
inside the header file. Like, printf() has its complete definition ,like
how it will work etc. in an I/O library! So, the compiler uses that
library to get the machine code for printf( ).
4/15/2021 By: Tekendra Nath Yogi 52
Contd…
• Difference between header files and library files:
– The key difference between header file and library file is that header file
contains the function declarations to be shared between several source
files while library file is a file that contains the function definition for the
declared functions in the header file.
– Other differences includes:
• Header files are TEXT files while library files are BINARY. This
means, we can read and modify the header file but not the library!
• Header file is in C language while the library is in machine language!
• Header file has to be included by the programmer while the compiler
automatically relates the library file(s) with the program!
4/15/2021 By: Tekendra Nath Yogi 53
Program Testing and debugging: Types of Errors
• Refers to tasks of detecting and removing errors in a program, so that the
program produces the desired results on all occasions.
• Four types of errors:
– Syntax errors/ compile time error : due to the violation of language
rules.
– Run-time errors: errors such as mismatch of the data types, or returning
an out of range array elements results in run time errors.
– logical errors : Errors related to the logic of the program execution are
logical errors. E.g., misuse of the operators.
– Latent errors: These are hidden error that shows up only when a
particular set of data is used e.g., r= (x+ y)/(p-q) this expression
generates error only when p=q.
4/15/2021 By: Tekendra Nath Yogi 54
Homework
• What is computer program and programming language? What is the importance of
programming languages?
• What are the different steps while solving a problem using computer?
• What is flow chats? What are advantages and limitations of flow charts?
• Differentiate between algorithm and flow chart with suitable example.
• What is debugging? What is the main difference between testing and debugging?
• What are the main features of C language?
• C is also known as middle level language. Justify the statement.
• What is the basic structure of c program? Explain each section of its structure.
• What is the purpose of header file? Compare it with library file.
• What are different steps required while executing a C program?
• What is source code or program? Why is compilation needed before executing a C
program?
4/15/2021 By: Tekendra Nath Yogi 55
Contd..
• Write an algorithm and draw flowchart to determine a number whether it is positive or
negative.
• Write an algorithm and draw a flowchart for calculating the simple interest using the
formula SI= (PTR)/100, where, P denotes the principal, T time and R rate of interest.
• Write an algorithm and draw flowchart to test a number for even or odd.
• Write an algorithm and draw flowchart to find the largest number among three
numbers.
• Develop an algorithm and flowchart to read N numbers from user and display sum of
all entered numbers.
• Develop an algorithm and flowchart for finding the sum of the series 1+2+3+4+5…..up
to n terms.
• Develop an algorithm and flowchart For calculating the factorial of a given number n
4/15/2021 By: Tekendra Nath Yogi 56
Contd…
• Draw flowchart and algorithm for allowing a user to enter any 10 numbers
and display only those numbers which are prime numbers?
• Draw a flow chart to read a number N and display all its divisors.
• Draw a flowchart for computing the sum of the digits of any numbers.
• Draw a flow chart for program which allows to read N from user and
display sum of only even numbers from 1 to N.
4/15/2021 By: Tekendra Nath Yogi 57
Lab1
• Write a program to add two numbers and display the sum.
• Write a program for calculating the simple interest using the formula SI= (PTR)/100,
where, P denotes the principal, T time and R rate of interest.
• Write a program to decide whether a number is positive or negative.
• Write a program to test a number for even or odd.
• Write a program to calculate area (=PI*r*r) and circumference (2*PI*r) of a circle
having radius r. The radius r should be taken from user.
4/15/2021 By: Tekendra Nath Yogi 58
Lab1
• Write a program to swap the value of two variables.
• Write a program that convert temperature in centigrade into Fahrenheit (f=9/5*c+32).
• Write a program that convert temperature in Fahrenheit into centigrade (c=5/9*(f-32)).
• The length and breadth of a rectangle are input through the keyboard. Write a program
to calculate the area and perimeter of the rectangle.
• Write a program to read height and base of a triangle and find area of it
(area=0.5*height*base)
• Write a program that asks three coefficients of a quadratic equation and calculate its
root.
4/15/2021 By: Tekendra Nath Yogi 59
Thank You !
4/15/2021 By: Tekendra Nath Yogi 60