Problem Solving Using C
Problem Solving Using C
USING C PROGRAMMING
■ 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:
Websites:
http://www.cprogramming.com/
http://www.richardclegg.org/previous/ccourse/
Develop an algorithm.
Example: (data1+data2)/2
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
Example:
WRITE A PROGRAM TO ADD TWO NUMBERS
A, B C=A+B C
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.
Π
Understanding the problem.
Problem Analysis.
Program coding.
PROBLEM ANALYSIS
DESIGN-ALGORITH,
FLOWCHART
PROGRAM CODING
DEBUGGING& TESTING
DOCUMENTATION
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.
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.
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.
Input/output
system, such as customer order (input) and
servicing (output).
Limitations of Flowcharts
Flowchart be used for designing the basic concept of the
can
■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.
Features of C
Highly portable.
Well-suited for structured programming.
Dynamic memory allocation.
First C Program
Initially, learn how to write, compile and run the C program.
■ To write the first C program, open the C console.
Save F2
RunCtrl +F9
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
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.
■ 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
■ Stages of Compilation
Source code
Preprocessor
expanded code
Compiler
assembly code
Assembler
executable code
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.
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'.
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.
main() section
Declaration part
Execution part
■ Sub program section
Example:
/* Documentation Section */
// File: Addition.c
// Description : Addition of Three Numbers
// Author: Student123
}
Presented by: RamyaDevi R/Guru Nanak College (Autonomous)
1.7 СCharacter Set
2. Digits
3. Special Characters
4. White Spaces
■ Alphabets
Letters Letters Digits
Upper Case A to Z Lower Case a to Z 0 to 9
■ Special Characters
&
.Ampersand #.Number Sign
.Period (Dot)
*
.Asterisk
Equal to .Semicolon
С ТОКЕ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
case enum
register typedef
char extern return union
do if static while
■ 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.