PROBLEM SOLVING
USING C PROGRAMMING
Bachelor of Computer Application
SEMESTER - I
GURU NANAK COLLEGE(Autonomous)
VELACHERY ROAD, CHENNAI 600042 –
(Re-Accredited 'A' grade by NAAC)
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
Syllabus
Planning the Computer Program:
Problem definition,
Π
Program design,
Debugging,
Types of Errors in programming,
Techniques of Problem Solving: Flowcharting, Algorithms.
C Fundamentals:
Character set
Π
Identifiers and Keywords
Π Data Types
Constants - Variables
Declarations - Expressions - Statements
■ Operators: Arithmetic, Unary, Relational and Logical,
Assignment and Conditional.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
Related Books
Recommended Books:
■ P. K. Sinha & Priti Sinha, "Computer Fundamentals", BPB Publications, 6th Edition.
■ Dr. Anita Goel, Computer Fundamentals, Pearson Education, 2010.
■ E. Balaguruswamy, 2016, 7th Edition, Programming in ANSI C, TMH Publishing Company Ltd.
Kanetkar Y., 1999, Let us C, BPB Pub., New Delhi.
Reference Books:
K.R.Venugopal, Programming with C, 1997,McGraw-Hill
■ Varalakshmi, Programming using C,2000(Reprint July 2001), V.Ramesh5
R.Rajaram,C Programming Made Easy,V.Ramesh
Π
B.W. Kernighan and D.M.Ritchie, 1988, The C Programming Language, 2nd Edition, PHI.
H. Schildt, C,2004, The Complete Reference, 4th Edition, TMH
■ Gottfried. B.S, 1996, Programming with C, Second Edition, TMH Pub. Co. Ltd., New Delhi.
Websites:
http://www.cprogramming.com/
http://www.richardclegg.org/previous/ccourse/
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.1 PROBLEM DEFINITION
The computer is the symbol- manipulating machine that
follows the set of instructions called a program. Any
computing has to be performed independently without
depending on the programming language and the
computer.
The problem solving techniques involves the following
steps
Define the problem.
Formulate the mathematical model.
Develop an algorithm.
Write the code for the problem.
Test the program.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.1 PROBLEM DEFINITION
1) Define the Problem
A clear and concise problem statement is provided.
Π
The problem definition should specify the input and output.
Full knowledge about the problem is needed.
Example: TO FIND THE AVERAGE OF TWO NUMBERS.
2) Formulate the Mathematical Problem
■ Any technical problem provided can be solved mathematically.
Full knowledge about the problem should be provided along with
the underlying mathematical concept.
Example: (data1+data2)/2
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.1 PROBLEM DEFINITION
3) Develop an Algorithm
■ An algorithm is the sequence of operations to be performed.
■ It gives the precise plan of the problem.
■ An algorithm can be of flowchart or pseudo code. Example:
Problem Definition:
TO FIND THE AVERAGE OF TWO NUMBERS.
Algorithm:
1. Set the sum of the data values to 0.
2. Set the count of the data values to zero.
3. As long asthe data values exist, add the next data value to
the sum and add 1 to the count.
4. To compute the average, divide the sum by the count.
5. Print the average.
Task.
To find the average of 20 and 30 manually.
20 + 30=50; 50/2 =25.
1.1 PROBLEM DEFINITION
4) Write the Code for the Problem
The algorithm developed must be converted to any programming
language.
The compiler will convert the program code to the machine
language which the computer can understand.
5) Test the Program
Testing involves checking errors both syntactically and
semantically.
The errors are called as "bugs".
Π
When the compiler finds the bugs, it prevents compiling the code
from programming language to machine language.
Π
Check the program by providing a set of data for testing.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.2 PROGRAM
■A program consists of a series of instructions that a computer
processes to perform the required operation.
Set of computer programs that describe the program are called
software.
The process of software development is called Programming and
the person who develops the computer programs are called
Programmer.
Π
Thus, in order to design a program, a programmer must determine
three basic requirements:
The instructions to be performed.
The order in which those instructions are to be performed.
The data required to perform those instructions.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.2 PROGRAM
Example:
WRITE A PROGRAM TO ADD TWO NUMBERS
Input two numbers.
Add these two numbers.
Display the output.
Π
Suppose we want to calculate the sum of two numbers, A and B,
and store the sum in C, here A and B are the inputs, addition is the
process, and C is the output of the program.
A, B C=A+B C
Input Processing Output
Fig Processing of the data
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.2 PROGRAM
Characteristics of a Program
■ Any computer program should have the following characteristics
Π Accuracy of calculations. Any calculation made in the program
should be correct and accurate.
■ Clarity: It refers to the overall readability of the program which
helps the user to understand the underlying program logic easily
without much difficulty.
Π
Modularity: When developing any program, the task is sub divided
into several modules or subtasks. These modules are developed
independently (i.e.) each task does not depend on the other task.
Portability: Portability is defined as the ability to run the
application program on different platforms.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.2 PROGRAM
Flexibility: Any program written should be flexible (i.e.) the
program should be developed in such a way that it can handle the
most of the changes without rewriting the entire program.
Efficiency: Any program needs certain memory and processing
time to process the data. This memory and processing unit should
be of least amount. This is the efficiency of the program.
Generality: The program should be in general. If a program is
developed for a specific task then it can be used for all the similar
tasks in the same domain.
Documentation: any application program developed should be
well documented such that even in the absence of the developer
and the author, the programmers could be able to understand the
concept behind it.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
Any program has to be broken into a series of smaller steps.
These series are independent of programming language.
Π
The programmer should have wide knowledge about the problem
and the way to solve it.
Generally any problem solving involves
Π
Defining the problem.
Π
Understanding the problem.
Providing the solution.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
The program development cycle involves
Problem Analysis.
Design- Algorithm and Flowchart development.
Program coding.
Program compilation and execution.
Program debugging and Testing.
Documentation.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
PROBLEM ANALYSIS
DESIGN-ALGORITH,
FLOWCHART
PROGRAM CODING
COMPILATION & EXECUTILON
DEBUGGING& TESTING
DOCUMENTATION
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
1. Problem Analysis
Define the problem by identifying the input and output of it.
■ Variable name is assigned for each item.
Π
After analyzing, the programmer has to develop various solutions for the
given problem.
Optimal solution is taken from the set of solutions obtained.
2. Design - Algorithm & Flowchart Development
Π
Algorithm and flowchart are developed to provide a sequence of actions
to be performed.
■ Algorithm provides a basic logic in solving the problem by providing
sequence of instructions.
Algorithm can be of
Flow chart
Pseudo Code.
Program Design Language (PDL): It has no specific standard rules
for defining the PDL statements. PDL is independent of any
programming language. It is also called as Pseudo Code.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
3. Program Coding
■ Code the algorithm in the selected programming language.
Π
The processes of translating the algorithm or the flowchart into
an exact instruction that will make up the program are called
program coding.
4. Program Compilation and Execution
Π
After program coding, the program has to be compiled and
executed.
Π
During compilation, if no error is produced, then the program is
executed successfully.
If errors are available, then the errors are displayed in the
terminal, and corrected later with correct syntax and then
compiled.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.3 PROGRAM DEVELOPMENT CYCLE
5. Program Debugging and Testing
Errors are called as "bugs"
Π
Errors can be categorized as follows
Syntax errors(during compilation).
Example: Program does not compile, missing bracket, bad
punctuation.
Run time (during execution)
Example: Program crashes, Check input data.
■ Logical (incorrect or illogical answers) Example: Program runs
and give wrong output.
6. Documentation
■ Once the programmer is free from the errors, it is the duty of the
programmer to document all the necessary documents which is
provided to the program users as manual.
Helps the user to operate correctly.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.4 ALGORITHМ
Algorithms are one of the most basic tools that are used to develop
the problem solving logic.
An algorithm is defined as a finite sequence of explicit instructions
that, when provided with a set of input values produces an output
and then terminates.
Π
In algorithm, after a finite number of steps, solution of the problem
is achieved.
Algorithms can have steps that repeat (iterate) or require decisions
(logic and comparison) until the task is completed.
Different algorithms may accomplish the same task, with a different
set of instructions, in more or less the same time, space, and
efforts.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.4 ALGORITHM
EXAMPLE:
To determine the largest number out of three numbers A, B, and C
Π Step 1: Start
Step 2: Read three numbers say A, B, C
Π
Step 3: Find the larger number between A and B and store it in
MAX_AB
Step 4: Find the larger number between MAX_AB and C and store
it in MAX Step 5: Display MAX
■ Step 6: Stop
The above-mentioned algorithm terminates after six steps. This
explains the feature of finiteness. Every action of the algorithm is
precisely defined; hence, there is no scope for ambiguity. Once the
solution is properly designed, the only job left is to code that logic
into a programming language.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.4 ALGORITHM
Characteristics of Algorithm
An algorithm has five main characteristics:
■It should have finite number of inputs.
Terminates after a finite number of steps.
Instructions are precise and unambiguous.
Operations are done exactly and in a finite amount of
time.
Outputs are derived from the input by applying the
algorithm
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.4 ALGORITHМ
Quality of Algorithm
■There are few factors that determine the quality of а
given algorithm
An algorithm should be relatively fast.
Any algorithm should require minimum computer
memory to produce the desired output in an acceptable
amount of time.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.5 FLOWCHART
■ Flowchart is a diagrammatic representation of an algorithm that
illustrates the sequence of operations to be performed to get a
solution.
■ The different boxes are interconnected with the help of arrows.
The boxes represent operations and the arrows represent the
sequence in which the operations are implemented.
The primary purpose of the flowchart is to help the programmer in
understanding the logic of the program.
Symbol Symbol Name Description
Flow lines are used to connect symbols. These lines
indicate the sequence of steps and the direction of
Flow Lines
flow of control.
This symbol is used to represent the beginning
Terminal (start), the termination (end), or halt (pause) in the
program logic.
It represents information entering or leaving the
Input/output
system, such as customer order (input) and
servicing (output).
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.5 FLOWCHART
Guidelines for preparing flowcharts
The following guidelines should be used for creating a flowchart:
■ The flowchart should be clear, neat, and easy to follow.
Π
The flowchart must have a logical start and finish.
In drawing a proper flowchart, all necessary requirements should
be listed in logical order.
Only one flow line should come out from a process symbol.
Only one flow line should enter a decision symbol. However, two
or three flow lines (one for each possible answer) may leave the
decision symbol.
Π
Only one flow line is used with symbol. a terminal
In case of complex flowcharts, connector symbols are used to
reduce the number of flow lines.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.5 FLOWCHART
Benefits of Flowcharts
■ A flowchart helps to clarify how things are currently working and
how they could-be improved. The reasons for using flowcharts as a
problem-solving tool are given below.
i. Makes Logic Clear.
ji Communication.
iii. Effective Analysis.
iv. Useful in Coding.
V.
Proper Testing and Debugging.
vi. Appropriate Documentation.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.5 FLOWCHART
Limitations of Flowcharts
Flowchart be used for designing the basic concept of the
can
program in pictorial form but cannot be used for programming
purposes. Some of the limitations of the flowchart are given as
follows:
i. Complex.
ii. Costly.
iii. Difficult to Modify.
iv. No Update.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
■C Programming,
Π
Was developed by Dennis Ritchie at AT&T Bell Labs, USA in
1972.
Is a high-level programming
language used to develop
applications for high-level business programs and low-level
system programs.
Became popular because of its power, simplicity and ease of
use.
Enables system program writing, using pointers.
It is reliable, simple and easy to use.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
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, struct, pointer.
Year Developed by Drawbacks
1960 ALGOL International Group Too general and too
abstract
Less Powerful and too
1967 BCPL Martin Richard
Specific
B Ken Thompson Too Specific
1970
1973 Traditional C Dennis Ritchie
1989 ANSI C ANSI commitee
1990 ANSI/ISOС ISO commitee
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
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.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
First C Program
Initially, learn how to write, compile and run the C program.
■ To write the first C program, open the C console.
Blue Screen Black Screen(Console)
Save F2
Compile →alt +F9 Show output→ alt+F5
RunCtrl +F9
Write programs Get output
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
Example of C Program:
#include<stdio.h>
Void main(
{
printf("Good Morning!");
}
File Edit Search Run Compile Debug Pro ject Options Window Help X
GM.C -2-[1] DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0, Progra...
mclude<stdio.h>
void main() C:\TURBOC3\BIN>TC
Good Morning!
printf( ;
}
1:1
Message
F1 Help F2 Save F3 Open Alt-F9 Compile F9 Make F10 Menu
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
☐ Header file is included
#include <stdio.h> includes the standard input
output library functions.
printf() function is defined in stdio.h.
The
■main() The main() function is the entry point of
every program in c language.
■printf() The printf() function is used to print data on
the console.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
What is Compilation?
The compilation is a process of converting the source code into
object code. It is done with the help of the compiler.
Π
The compiler checks the source code for the syntactical or
structural errors, and if the source code is error-free, then it
generates the object code.
#include <stdio.h> 01000000000000
011111111111111
int main(({
01010101101010
printf("Hello C Language"); 00000011111111
0000011!]
return 0;
00000010101011
}
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
■ Stages of Compilation
Like most high-level languages, C also uses compiler to convert its
source code (files with the extension .c) to object code (files with the
extension.obj).
The object code will be link-edited by the linker to form the machine
language also known as executable codes (files with the extension .exe)
☐
The compilation process can be divided into four steps
Pre-processor
Π
Compiler
Assembler
Linker
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
■ Stages of Compilation
Source code
Preprocessor
expanded code
Compiler
assembly code
Assembler
Other object object code Libraries
files
Linker
executable code
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
Stages of Compilation
Preprocessor
■ The source code is the code which is written in a text editor and the
source code file is given an extension ".c".
This sourcecode is first passed to the preprocessor, and then the
preprocessor expands this code. After expanding the code, the expanded
code is passed to the compiler.
Compiler
The code which is expanded by the preprocessor is passed to the
compiler.
The compiler converts this code intoassembly code. Or we can say
that the C compiler converts the pre-processed code into assembly code.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
Stages of Compilation
■ Assembler
Π
The assembly code is converted into object code by using an
assembler.
The name of the object file generated by the assembler is the same as
the source file.
The extension of the object file in DOS is '.obj,' and in UNIX, the
extension is 'o'.
If the name of the source file is 'hello.c', then the name of the object
file would be 'hello.obj'.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
Stages of Compilation
Linker
Mainly, all the programs written in C use library functions.
Π
These library functions pre-compiled, and the object
are code of these
library files is stored with '.lib' (or '.a') extension.
■ The main working of the linker is to combine the object code of library
files with the object code of our program.
The output of the linker is the executable file.
Π The name of the executable file is the same as the source file but differs
only in their extensions.
In DOS, the extension of the executable file is '.exe'
For example, if we are using printf() function in a program, then the
linker adds its associated code in an output file.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
■ The Structure of a C Program
Documentation Section
■ Header File section
Π Definition Section
Global declaration section
main() section
Declaration part
Execution part
■ Sub program section
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.6 C Fundamentals
Example:
/* Documentation Section */
// File: Addition.c
// Description : Addition of Three Numbers
// Author: Student123
/* Header File Section */ #include<stdio.h> #inlcude<conio.h>
/* Definition Section */
# define c 3
/* Global declaration section */
int calcsum(int,int,int);
/* main() section */
int main()
{
/* Declaration part */
int a,b,sum;
/* Execution part */ printf("Enter Two numbers"); scanf("%d od", &a, &b); sum=calcsum(a,b,c);
printf("The sum is: %d", sum);
}
/* Sub program section*/
int calcsum(int x,int y, int z)
{
int d; d=x+y+z; return d;
}
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.7 СCharacter Set
Character set denotes alphabet, digit or special character.
Characters combine to form variables.
■ Characters in C are grouped into Letters, Digits, Special characters
and White spaces. Compiler generally ignores white space when it is
not a part of string constant.
White Space may be used to separate words, but not used between
characters of keywords or identifiers.
Π
The character set in C Language can be grouped into the following
categories.
1. Letters
2. Digits
3. Special Characters
4. White Spaces
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.7 С Character Set
■ Alphabets
Letters Letters Digits
Upper Case A to Z Lower Case a to Z 0 to 9
Escape Sequences (White Space Characters)
■ A character constant represents a single character. It is also
called as "Backslash Character Constant".
together with input and output statements. It has no
It is used
meaning but it has the control to decide the way an input has to
be displayed.
1. Blank Space \b 2. Horizontal Tab \t
3. Carriage Return \r 4. New line \n
5. Form Feed \f 6. Vertical tab v
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.7 С Character Set
■ Special Characters
&
.Ampersand #.Number Sign
.Apostrophe < .Opening Angle (Less than sign)
.Period (Dot)
*
.Asterisk
@ At symbol %.Percentage Sign
.Backslash + .Plus Sign
.Caret ?.Question Mark
.Closing Angle (Greater than sign) .Quotation Marks
.Colon .Right Flower Brace
.Comma .Right Parenthesis
$ .Dollar Sign .Right Bracket
Equal to .Semicolon
! .Exclamation Mark .Slash
.Left Parenthesis ☑ .Tilde
.Left Bracket .Underscore
{ .Left Flower Brace .Vertical Bar
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.8 С TOKENS
С ТОКЕNS
■ Among the group of text individual word, punctuation marks are called
Tokens.
It is a smallest individual unit in a C program.
Π Classification of tokens in C
3 4
2 5
Constants Strings 6
1
Identifiers Special
Keywords Symbols Operators
Classification of C Tokens
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.9 Keywords
☐
Keywords
Every word in C language is a keyword or an identifier.
.
Keywords in C language cannot be used as a variable name.
Π
The compiler specifically uses them for its own purpose and they serve
as building blocks of a c program.
The following are the Keyword set of C language.
auto double int struct
break else long switch
case enum
register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.10 Identifiers
■ Identifiers
Π
Identifier is a name given to program elements such as variables,
functions, procedure, arrays and soon.
Π
First character must be an Alphabet or Underscore.
■ Identifier consists of sequence of letters, digits or combination of both.
Rules for Identifier
First character must be an Alphabet or Underscore( _).
Special characters and embedded commas are not allowed.
First 31 characters are given high priority or preference.
Keyword cannot be used as Identifier.
There should not be any white space.
Both uppercase and lowercase letters are permitted.
The underscore character is also permitted in identifiers.
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)