Documente Academic
Documente Profesional
Documente Cultură
PROGRAMMING
UNIT - I
Introduction to Programming:Concept of algorithms, Flow Charts, Data
Flow diagrams etc., Introduction to the Editing tools such as vi or MS-VC
editors, Concepts of the finite storage, bits bytes, kilo, mega and
gigabytes.Concepts of character representation, Number Systems &
Binary Arithmetic.[No. of
Hrs. 8]
UNIT - II
Programming using C:The emphasis should be more on programming
techniques rather than the language itself.The C Programming language
is being chosen mainly because of the availability of the compilers, books
and other reference materials.
Example of some simple C program. Concept of variables, program
statements and function calls from the library (Printf for example)
C data types, int, char, float etc., C expressions, arithmetic operation,
relational and logic operations, C assignment statements, extension of
assignment of the operations.C primitive input output using getchar and
putchar, exposure to the scanf and printf functions, C Statements,
conditional executing using if, else.Optionally switch and break
UNIT - III
Iterations and Subprograms:Concept of loops, example of loops in C using
for, while and do-while.Optionally continue may be mentioned.
One dimensional arrays and example of iterative programs using arrays, 2-d
arrays Use in matrix computations.
Concept of Sub-programming, functions Example of functions.Argument passing
mainly for the simple
variables.[No. of Hrs. 8]
UNIT - IV
Pointers and Strings:Pointers, relationship between arrays and pointers
Argument passing using pointers Array of pointers.Passing arrays as arguments.
Strings and C string library.
Structure and Unions.Defining C structures, passing strings as arguments
Programming
examples.
[No. of Hrs. 8]
TEXT BOOKS:
1.Yashwant Kanetkar, Let us C, BPB Publications,
2ndEdition, 2001.
2.Herbert Schildt, C:The complete reference,
Osbourne Mcgraw Hill, 4thEdition, 2002.
REFERENCE BOOKS:
1.Raja Raman, Computer Programming in C,
Prentice Hall of India, 1995.
2.Kernighan & Ritchie, C Programming Language,
The (Ansi C Version), PHI, 2ndEdition.
What is Computer
Computer is an electronic device that is designed to
work with information.The term computer is derived
from the Latin term computare, this means to
calculate.Computer can not do anything without a
Program.
Computer is an advanced electronic device that takes
raw data as input from the user and processes these
data under the control of set of instructions (called
program) and gives the result (output) and saves
output for the future use.
HARDWARE ORGANIZATION
of a
COMPUTER
Characteristics of a
computer
High speed
More reliable and accurate
High memory capacity
Can be able to perform various tasks
Low Level
Languages
PROGRAM
Note
The only language understood by computer
hardware is machine language.
PROGRAM
Note
Symbolic language uses symbols, or mnemonics, to represent
the various machine language instructions.
Third-generation languages
Fourth-generation languages
Fifth-generation languages
C
C++
Java
ActiveX
PROGRAM
21
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
Format of an Algorithm
Algorithm name(input,output)
1.
2.
n(Algorithm body which contain
finite no. of steps )
Algorithm can be write in English
language style or English
programming style.
Ex.
Sum(A,B, out)
1. Get the two inputs from the user
2. Assign the inputs to A & B
3. Set out:= A+B or set out <A+B
4. Exit
Algorithm
Example 1: Write an algorithm to
determine a students final grade
and indicate whether it is passing or
failing. The final grade is calculated
as the average of four marks.
Algorithm
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
Algorithm Analysis
It is analyze in terms of their
efficiency i.e. how much computer
memory space and execution time it
uses, to store and to execute
Space complexity- for fixed and vary
variable
Time complexity- compile time and
run time
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.
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
Flowchart Symbols
Basi
c
Example
START
Input
M1,M2,M3,
M4
GRADE(M1+M2+M3+M
4)/4
N
IS
GRADE<
50
PRINT
PASS
PRINT
FAIL
STOP
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
Example 2
Write an algorithm and draw a
flowchart to convert the length in
feet to centimeter.
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
Example 3
Write an algorithm and draw a flowchart
that will calculate the roots of a
quadratic
ax 2 bx c equation
0
b 2 4ac
Hint: d = sqrt (
) and the
roots are: x1 = (b + d)/2a and x2 =
(b d)/2a
Example 4
Pseudocode:
Input the coefficients (a, b, c) of the
quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Example 4
START
Algorithm:
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
Input a, b, c
d sqrt ( b b 4 a c
x1 (b + d) / (2 x a)
x2 (b d) / (2 x a)
Print x1, x2
Input
a, b, c
)
d
sqrt(b x b 4 x a x
c)
x1
(b + d) / (2 x a)
X2
(b d) / (2 x a)
Print
x1 ,x2
STOP
40
DFD Symbols
The square is an external agent (a person or
organization, outside the boundary of a system that
provides data inputs or accepts data outputs)
The rectangle with rounded corners is a process
(named Look up item available and can be referred
to by its number, 1)
A process defines rules (algorithms or procedures) for
transforming inputs into outputs
The lines with arrows are data flows (represents
movement of data). Slide shows two data flows
between Customer and process 1: a process input
Item inquiry and process output named Item
availability details
The flat rectangle is a data store (a file or part of a
database that stores information about data entity)
41
Data
Flow
Diagram
Symbols
42
43
44
DFD is a modeling technique that breaks the system into a hierarchical set
of increasingly more detailed models
DFD may reflect the processing at either a higher level (more general view of
the system) or at lower level (a more detailed view of one process)
These different views of the system (higher level versus low level) creates
the levels of abstraction
45
Layers of DFD
Abstraction for
Course
Registration
System
46
Context Diagrams
DFD that summarizes all processing
activity for the system or subsystem
Highest level (most abstract) view of
system
Shows system boundaries
System scope is represented by a single
process, external agents, and all data flows
into and out of the system
47
48
49
What is C?
A
Why use C?
Mainly because it produces code
that runs nearly as fast as code
written in assembly language. Some
examples of the use of C might be:
Operating Systems
Language Compilers
Assemblers
Text Editors
This is used mainly because of the portability
that writing standard C programs can offer.
Major parts of popular operating systems like Windows,
UNIX, Linux are still written in C.
History
Gateway
programming efficiency
to machine oriented
machine efficiency (as
level languages)
1960
ALGOL
International Group
1967
BCPL
Martin Richards
1970
Ken Thompson
1972
Traditional C
Dennis Ritchie
1978
K&R C
1989
ANSI C
ANSI Committee
1990
ANSI/ISO C
ISO Committee
Feature of C
Hardware independent
Provide functionality through rich set
of function libraries
Case-sensitive
PREPROCESSOR
Preprocessor :It is a program that process the source code
before it passes through the compiler
#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 preprocessor directive#includeas
follows,
#include<filename>
Filename is the name of the library file that contains
the required function definition. Preprocessor directives are
placed at the beginning of the program.
Basic structure of C
programming
Documentation section
Link section
Definition section
Global declaration section
main () Function section
{
Declaration part
Executable part
Subprogram section
Function 1
Function 2
..
..
Function n
Execution of a C program
Executing a C program involves a
series of steps. They are i)Creating the program.
ii)Compiling the program.
iii)Linking the program with
functions that are needed from the
C library.
iv)Executing the program.
STARTING WITH
PROGRAMMING.
C CHARACTER SETS
Reserved Words
Keywords that identify language entities
such as statements, data types,
language attributes, etc.
Have special meaning to the compiler,
cannot be used as identifiers in our
program.
Should be typed in lowercase.
Example: const, double, int, main, void,
while, for, else (etc..)
Displayed in BLUE color in MS Visual C+
+
KEYWORDS
Identifiers
Words used to represent certain
program entities (program variables,
function names, etc).
Example:
int my_name;
my_name is an identifier used as a program
variable
Constants
Entities that appear in the program code as
fixed values.
4 types of constants:
Integer constants
Floating-point constants
Character constants
String Literal
Integer Constant
Positive or negative whole numbers with no fractional
part
Optional + or sign before the digit.
It can be decimal (base 10), octal (base 8) or
hexadecimal (base 16)
Example:
const int MAX_NUM = 10;
const int MIN_NUM = -90;
const int Hexadecimal_Number = 0xf87;
Integer Constant
Rules for Decimal Integer Constants
1. Decimal integer constants must begin with a
nonzero decimal digit, the only exception being 0,
and can contain decimal digital values of 0 through
9. An integer that begins with 0 is considered an
octal constant
2. If the sign is missing in an integer constant, the
computer assumes a positive value.
3. Commas are not allowed in integer constants.
Therefore, 1,500 is illegal; it should be 1500.
Example of legal integer constants are 15, 0, +250
and 7550
0179 is illegal since the first digit is zero
1F8 is illegal since it contains letter F
1,700 is illegal since it contains comma
Character constants
A character enclosed in a single quotation
mark
Example:
const char letter = n;
const char number = 1;
printf(%c, S);
Output would be: S
String Literals(constant)
A sequence of any number of characters
surrounded by double quotation marks.
Example:
Human Revolution
String Literals
Escape
Sequence
Name
Meaning
\a
Alert
Sounds a beep
\b
Backspace
Backs up 1 character
\f
Formfeed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\\
Back slash
Single quotation
Double quotation
\?
Question mark
Punctuators (separators)(delimiters)
Symbols used to separate different
parts of the C program.
These punctuators include:
[ ] ( ) { } , ; : * #
Usage example:
void main (void)
{
int num = 10;
printf (%i, num);
}
Expressions
It is a combination of operators,
constants,variables and functioncalls.
The function can be arithmetic,
logical or relational.
x+y(arithmetic operator)
a>b (relational expression)
a==b(logical expression)
Func(a,b)(function call)
STATEMENTS
In a C program, instructions are written in
the form of statements. A statement is
an executable part of the program.
Statements can be categorized as:
1)Expression 2)Compound 3)selection(if,
ifelse,switch)
4) Iterative (for,while,do.while)
5)Jump (goto,continue,break,return)
6) Label(case,default,label statement used
in goto)
Expression Statement
Expression statement:
X=5; x=y-z; func(a,b);
;(null statement)
Compound Statement:
{
Int l=4; b=2;h=3;
Int area,volume;
Area= 2*(l*b*h);
Volume=l*b*h ;
}
DATA TYPES
A data type defines a set of
values and a set of operations
that can be applied on those
values.
Data types
VARIABLES
Variable is a quantity that may vary during program
execution.
Variable names are names given to locations in memory,
where a constant is stored.
Ex int a=3;
Examples:
/* OK */
_1993_tar_return
Hello#there
/* illegal */
double
/* shouldnt work */
2fartogo
/* illegal */
Basic Datatypes
There are only a few basic data types
in C
Bytes Required
DATA
char
int
float
double
All types have a fixed size associated with
them
Variable Declaration
Generic Form
<typename>
Examples:
int count;
float a;
double percent, total;
char p;
Initialization
Examples:
int count; /* Set aside storage space for count */
count = 0; /* Store 0 in count */
Documentation section
Inclusion or link section
Global declaration section
Main () function section
{
Declaration part
Executable part
}
#include<stdio.h> (preprocessor
directive)(standard input output
header file)
#include<conio.h>(console input
output header file)
Simple C Program
A program to print Welcome to c
/* A first C Program*/
# include<stdio.h>
#include<conio.h>
Void main()
{
Clrscr();
printf(Welcome to C\n");
getch();
}
void main()
This statement declares the main
function.
A C program can contain many
functions but must always have one
main function.
A function is a self-contained module
of code that can accomplish some
task.
{
This opening bracket denotes the
start of the program
printf("Welcome to C\n");
printf is a function from a standard C library
that is used to print strings to the standard
output, normally your screen.
The compiler links code from these standard
libraries to the code you have written to
produce the final executable.
The "\n" is a special format modifier that tells
the printf to put a line feed at the end of the
line.
If there were another printf in this program, its
string would print on the next line.
}
This closing bracket denotes the
end of the program.
Comments in C
Multi-line comment
//--comment
for
Manav Rachna College of Engg
Comments
are
used
documentation purpose
Two types of comments:
// Display sum
Comments in C
Multi-line comment
//--comment
for
Manav Rachna College of Engg
Comments
are
used
documentation purpose
Two types of comments: