CS3251 (UNIT 2) NOTES EduEngg

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

CONNECT WITH US

WEBSITE: www.eduengineering.in

TELEGRAM: @eduengineering

 Best website for Anna University Affiliated College Students


 Regular Updates for all Semesters
 All Department Notes AVAILABLE
 All Lab Manuals AVAILABLE
 Handwritten Notes AVAILABLE
 Printed Notes AVAILABLE
 Past Year Question Papers AVAILABLE
 Subject wise Question Banks AVAILABLE
 Important Questions for Semesters AVAILABLE
 Various Author Books AVAILABLE
Subject Code/Title: CS 3251- Programming in C Unit:

UNIT- I
ARRA YS AND STRIMG

Tntroducion ATars Pocloclen, Inuialmalien .

Ore dimon Sora ana Tro dimensional Aray


Strng
StTn Operatons Longth ompoua Concale naBa, ory
l ocYion orl incas and binauy cas ch

Tnbvoduc ton le Aro


aCollection Sim las dalo elemen La,
An a7roy ghrma data typa The
tha
. .

have
Thasa
Thasa dala Yament in consocutive mamoy
stored
elomenb t h a onay aa UndeT albo known
ard o7 fervad by an
localen
as Sub &ipt
Do claLalhon oArrays
wd
must e daclauad bayore Ling
being wed
An a7ay

aaurame l'si]
Syntot dalo tupe
storemb, chas,fled
datalyre-whot
knd values t Can
deubla)
datalyre to derdfy tha aay
Name
maumum nume alu fhak the a ra
the
Can held
tha stao rnent declaves
a to ke an
t alo)-
xampe der
Tn tho ovray
Cenlainng to numke 1s
ay ens th n-i
m
ad
Stovb
rd ,
Flamerh>15

Downloaded from : www.eduengineering.in DEPARTMENT OF


sCiENCE ND LUA
Programming in C Unit:
Subject Code/Title: CS 3251-

the ela ments of an aTTay


Acc e6Snq
eloment the o7ay
or acc06S7ng
an iri ridual
aaoss all elomen
must be usad To
oTray
the Oray cubg ip
must ue a loop.
aYYO wc
Tha
1 0c
1OC
ench eloment The Qray
Codo
Code to thitialima
int , numTo]

0:iu it+)
or(
num J Loo

olamants
Cal caulatinq tha addrass f ra
onsocaute
dala elemonh n
An aray storos al kase addess (la)
the
the
memony lcauens Slovugju sulffcient
a7ray Canpy b alabte!
element i h o alaualal
addws h e sk elemenh Can ip
Can
data
The ocdross of othe
tha base adress
Ung 58, 311
58, 55, S0,
tmaks [J:feg,s1, address: 1oo
address io
an
a n aray maks [ 6 kae
in J marks
[6J
the address
colculala
7

58
9 57 AJ
012
ma ko foJ toh

2(5-0)
loco
[hJ)
9ddvess (ma.ls Ooo

Stor ingan nteqe Vaue regus res

he kraw tha
thcecre
,uord me bes
2-tes

Downloaded from : www.eduengineering.in 0EPARTaET


ENCE AT ;94ATYer
Programming in C Unit:
Subject Code/Title: Cs 3251-

Calalalng the Longth 6 on ar r0y


Lonqth the Qray ron b the numbo:
.

storad
e lome nb lowo bauwd t
uppo bourd
-

ongth
auts in a0y Just alloaking epoce
spece
Stor
a7ray,
uo
au
Just alloaling
tn tho a r
an
declae
aray
when
hen we
no valos
ao stor
stora J
the
elomenb.
thee Qua
a a y , thoe ao throe uays
thre
aUS
o t htha
e ava
To Storc
Valuos
element al
element
a the
te time declaual
lazali.
the array
Tribaline
Initialing koyboad
the keyhoad
the
Valuos rom elomens
elamen
Tnputng a
udrdi riiv
diu
deuY
a
Vaues to
Assqning declaralon
declaalon

during nbialrmal af
at
the avrays
also e . thitialn
IniIri tiaum
Can alsSo
When an
an
array when a n array
Elemen f the a o totho
ho
ar
V aa
rabblle
ess ..

daclatalsn
a Vaue for eveu
Value tor eveu
the me
me
hood
Provide
we

s intialmed
element
the arra
2 list Va lue
auay name Lsine7
=

tyre
, 95, 8 d.
navks öJ =so, 85, declasad, tha has
ha
it marks
declacd that
nama

an array Wth
Wth
elamenta
elameni
to store
endy
enoyh Space at the
the bme
time da
daclaxalon,
clasaluon,
at
ibiak m1nq the aay
while n auay.
Cmit the
tha
S he
the
the poramme may
()

Downloaded from : www.eduengineering.in CHENNAI


NSUIEE
DEPARTMENT OF
SCIENCE A N D HUMANITIES
Subject Code/Title: CS 3251- Programming in C Unit:

ttnauks rJ =[90, 15;

80 85 q0
t maukalhJ: /8o, 85, 40,5, 7
ttmauks6J = S 0, 757
qo 95
int maks CsJ: D3

1nputting Values Jrom tho kayboaid


Tnthe methad a /do / do Nhile or for loop
a

eloment f ke
to pu the Value tor each
bxecutad
6LLLay

tht aLh
forLi-0 1 5 i t t )
San'7d", da[i J);
values t indevidua! elamenb
*Rssgning
evauales to the daatye ef the
Vake thal
Any ndtvi dua ausay lamerb
asig nad
to tho
Crvay can e
aray ( e wiHan as
assignment Slateme nt con

A ple
Sunple
marks [aJ 98
ray to anothor a7yay,
One
hWo Cannst as ign
hare thas sama bpe ad
pe ard 9g.mp
evan f tho two arays
ndriduol elornan! lavel
n a17a aa he
nt 1, auD5], ar J
aTE5) , 2 , 3,4,5
for (i-0 i5 i#)

Downloaded from : www.eduengineering.in DEPARTRMENT OF


SCIENCE AND HUMANITIES
CS 3251- Programming in C Unit:
Subject Code/Title:

Dimen Sional
TO wsing two
uing
au Spoufod
A t w o dmensforal
A dorotes
tes rocw
rocw ard the othar
other
whoo. ono Subeipt dono
Subsc pb
dle noles cskumn
dimensional ATTOS
ikclaring two adoclared before
must ke
arrays
The
The two
two
dimensional

Stalemant
Stalemant
Cornpiler
tels the Conp
tells le
doclasalo
doclaualuon
eoch elomant
use!.T h o te
being
array,ho dala
tha dimension
the name ard the
Simeeach
each dimarsion
n the amay
Syntax
dola type yname oueSie] cslunn sye];

alJ [a]. afoJLJ


aLoJ[J LoJLU
a LiJLJ aLiJloJ

aLJ Co aL121.
al2[o
tuoo
dimensional anay
T tialining 9}
, 2, a, 4,5,6,7,
tht afaJCa] =

amensiona! array
array dene
dene
two
Tha nibialinaticn of a
witten
Con alo e
Statement
brow. The akova

5i,a,33 i4,5,13, S7,a133.


nt alaJfa]
dmensional arrouy
elomenb f 2
d ecess1ng the array
Conba
Conbauna
tuo
dimenSional
pts.
sdhecpto
tuo SdbecA
The first
A
A two the
elomenh,
bo Scan

wil use tuo tor Loeps array


tha
array ard ha
e mo h tha 2
will scan each
eveuy vs a
for lop
for oop And iridual
ndi dua csumn for
cslunn tor
san
l l
will
urd for loop
he array
N

Downloaded from : www.eduengineering.in FHNNA DEPARTMENT OF


SCIENCE AND HUMANITIES

ECRNL
Unit:
Subject Code/Title: CS 3251- Programming in C

Pocram to Print the e loman 2 J

c l u d o sldohr
tht maun ?

nt a loJl2J = } 75, 8o, 85, 98 3


tnt
or(0 i< ;1+)

println");
8
for (y:0, j<a j+)
prt 7d t ", atiGI):
3
3
Strings a nul txmunated
tearunated
a string
In
In cC langcua
anguage,
afte the last cler, a
chaa

chaocte
This means
erd the
the erd
tho ko
& stored to Sgnity
chaacte L'lo')
nul
n ll

chaa cto Quay


"
pLogam
cha str-
m
9 a
P
srIJ "
*
lo mpt string.
Chas

encoun teys a oduonce f chaocfors|


whan the Compi lau
maks 1 appands
appads aaa

tha double quotauon


,

enclasad
charote lo at Hhe od delaulk.
nell
null

Downloaded from : www.eduengineering.in SMENA DEPARTMENT ODF


SCIENCE AND HUMANITIES5
Code/Title: CS 3251- Programming in C Unit:
Subject

on sling s
operauons

Lonqth a string
Ornporo0S tuo 97ing

Concale nates tuwo


stina
String

Length o a string tha


chaacte the shring orns tudes
The num all blank spaces
longth all
longth
spacos
Jind the
tko sringTo
longth q Characle1s tho s
Qve counted as

# hcluco Kstdo.h

ro7d maus t)

len
strLso,
{zo,
Cha

Prnf [ Enter the Stn ")


gob (st);
while (st EEJ!
= "lo')

en yd ", kn);
Prontf( Length q strng

oul pd qrammi
Ente tha shrng
Lenqh strin

Downloaded from : www.eduengineering.in DEPARTMENT OF


sCIENCE AND H UMANITIES
Subject Code/Title: cs 3251- Programming in C Unit:

Comsaf nq tW Sings
SI ard S2 aua tuo slrgs then Compai two

l) gve
ett he those osults
Smnq
Shings eddal
SI and S ae S) wlCome afle 52
SI>S2 when dcon auj ocdos
procodos S2
dctionci oydes S
$I s S , when n chae
chas cBes
tte w
u
each and evey
the turo Strngs chzdocers
chalacders aa e
To Compac. tha
tho
all
Tall
oth tho Srings. Tf
Compauad m sad to
a o Caud
be edual
to be ecal
tha buwo SBings some lonqh
seme nqh
Came than tuD
tuv Strings aue
Strings
Qe

the t.
ve frst check
whethe
on morhg
ass
ahedd a
ahad
hen thea bno ppint aa a
rot
not
Same
Sane
not, too
t o Strings
Strings then wO
meanS
thaf tha
tle
sama
Sama langth
angth, Then wo
5raght toay oThe a l l the chasaders
the
chaaders

tuo Sdrings
aa
check
chock i all 1
tha chaiactes to
efual sef b
Compauoo chaacto ythan
Chaacke

than tho Vax


abla2 the
the
precodes
precedes
Qua Same , 1}es
we
check whd
chock whch srin1Correondirg
sBring
Correpndirg mess7
messa7
then then the
the
else Same
ordes ard print
ad
dctior
othe ohile (i< lani)
clude stdia.hs s t r [ ? J = :str2C .
thcludo 1sring. h>
t
o r maun () e reak
k
str2 L5o]; = = len i)
cha striC50), i
zo esual=o iedual l
loni o , lon Pquel)
i o,
tnt
"2 print" 2Strings
ae
Pri fFntes slgi lont len)
ges tslri); Staig3 ae not
pusEnta sring 3 " Pmnt tre Ecal}

geb (sr2); 3 edaal=zo


lon=Sylan (Sr1) str CU strajJ)
lon Srlan lstra) Pind sh b")
Clon== len )
Clon else
brq)
9ut nla Slr Flower PunH l" shr
Ehter 7LOneM
Downloaded from : www.eduengineering.in DEPARTMENUMANITIES
Tho 317ngs
ae
ed t SCIENCE AND HUMANITIES
Programming in C Unit:
Subject Code/Title: CS 3251-

Con cale nafng tu Stines


QLO tuo rins, then concabe natin
ard $2
Tf SSi Contains
characters
St Ag which
operaton Produces a

follaved by tho choroclors o

inc lude <std:o .h>


#nclude ondo.hs
Cf man )
str [io]
slr3 Cica
cha st [oo
tt o,j=o
'Fte the rsl orog");
Prntf
ots (sr),
tha second smn
Pronif ("Fnte
geslr2)
Ci! = "\o')
whe (str Oudput
J sBr [ I ;
Ente the fivsi String Pthon
Entes the Cocord string: Prge
co
sTi ng
Jt The
concate na ted
P 1hon puora
'lo') =

whlo Slr2[iJ!

Str3jJ str2C -

str3 LJ "lo'
Concatenated stmng 2:
print"The
puts (si73);

geth)
ne tum oD

Downloaded from : www.eduengineering.in SHENNAI


LCHN DEPARTMEHUMANITIESS
SCIENCE AND HUMANITIES
Subject Code/Title: CS 3251-Programming in C Unit:

Copying Sirg
usad
bcopy tha Contens f ono String
Stvin oPy
Cto anothes String

ihclude 1 stadio.hs

Yoid maun)
62 C50J
chas Si [5o3
nt
Print Enlo the sringIn")
geb lsi))
0
while (siL1! '\o')
Sa[ S[i3;

9 Copied Sl7 ng: 79", 62);

Oulud
Fnle tho Stnnq heltoma

Copied sfrig he lcome

Downloaded from : www.eduengineering.in CHENNAI DEPARTMENT OF


sCIENCE AND HUMANITIES
Unit:
Subject Code/Title: Cs 3251- Programming in C

Binay Seaich
Soo chin techriduo Sc0rch reduires the
Seachi the press o doleamining Binay 'ascanding
Rsd e orted
Present alaoithm
whetho on elomant Birauy
ceach

elemenb or nof
Orde. The tha eloment
|a ren st beqins by Cornparing
mudde
Present at tho
inea Saoich that s
tochndue. t h e Rst.
he
I the Sunplas!al n e end ot
tha
these iia matck then
The Seach egins Tf lecalen o
loalsn
the rduin ard the
ListSeachel for
st erds
Soanchmudde element eturned
b etumed
ntil thé eloment muddla e lament
element n e yone
with
elamantO end 1
lst s the
tho
mus
math

the end o TRe there a


a tha
ard
or
dachod eloment
found
the
miadla
loss than the
tha
eloment

clude (stdio.hs Seach then irst part


othatoise
alamant
mddle Seauchad
void mauic) ct ù seachal
fha the üst
2 a L0J, 7, I,n Geconed parB
of untFl
o n t i n u a

ln "); to
prnif ("Ente the suge famay T h s process
el
e la
omme
ennt
t
edaal
eua t
the s t
Scauch
Scarf d " dn); elemens"). the Seaich
middle
eloment or
elament that io
n)C"Ente
tha aray the elament taB
one
ony Seadh element
for (ico idn;itt) Contains
edualto the
da[rT); not
Scanfl" d ",
Prof(Entee the
elament
bseach)iclude stdio h>
Banf . d " , ar): nclude2 <wnio. h>.

for l'o ikn ; f+) Void men t)


afso
2i(at==*) int ,fnst ,
last,imid,n,z,

elemenh ")
printf elament fourd'") Print
fTenla
the

bioak Scanf(d ", dn) h ascerdirg


Priny"
Ente the elmt
n): orda"),
prnntf elemenk b not fauncd lan;it+)
for li =o
3 liD;
Outpul Suanf ["d", da Seach "):
tb each ")
elnt to
tha emt
Ente the sme 9 aay Printf("Enla
n t f ("Enl s
1o 5 35 14 R6}
Fn th elemenh Sanf yd ,x)
Seaidh
Ento the elomant b
lemen faund.

Downloaded from : www.eduengineering.in CHENNA! DEPARTMENT


SCIENCE AND
OF
HUMANITIES
ECNOLO
Programming in C Unit:
Subject Code/Title: Cs 3251-

Se lochan Sor
Je ction Gorl dolemines
Last n- The e
and
nid: (vs last)) the mi rimum of the s1
tha e lomad
a
ith
Ahile ( s l l a s ) Scoaps 1t
assumed minamurn unde
tho
ftafmid] <z) athe avray Contaun n

stmid 4 elemens b e sorted ui the


tho fns elmt
Core ct seduene
elsei(a lmid ] ==T) remainng
Compaad
with the
t h e lawes
pnf I elnt found "), elements and
whch
eloment t
thaB
break elonent, fote
Then tho
Secord elmt
irst position. and
takon
e se u
|irom the armay
Cast mid -) with the emainin n-2
Compaad elomenf sth a
mid (frst tlas+))3
zf an
olomenb.
that of ke
f(Frel >last) Value le6sthan the
elament fourd
printf' (Emt &not found ti lis2Secord ëSuappad
h-2 elamans it
second eloment
Co th the
ad 90 en. Continue
oupu the aray
may
the elmts
unti) all
Ente harnkas l e m a n
thu
hus process
ae SoTted .

an may

Ente the elameni


Sorted
clude stdio. h
DTdes Yoid maint)
1 7 3 5 57
Seasdh 56 ind a Lso3 2,n, j. bemp;
Enla tho eoment b elmb ");
Printf (Enter the no
t
P
Fle mant foud
Sonf (7d ", dn)
for (i:o inn;lt+)

Scanf (" zd' dar?])


or(io ; i«n-i; ?+)
Downloaded from : www.eduengineering.in DEPARTMENT OF
SCIENCE AND HUMANITIES
A
Subject Code/Title: CS 3251- Programming in C Unit:

xtra pro9rams
mun
Mol additon
for (i in;j<n j14) #nc ludo Astdro h>
Veid mainl)
(o[muiJ>a ji) 2 c[5JT5]
mun int a bL6J bDSJ[s3,
ht j,m,n;
(mun-i)
Pf Ente the ww col s.rge ")
temp a[AI SLan(1d7d"Am, dn)
pritf Fnl Halriz A tlmi");
for(i o 7am ;it)
a[mun : temp
Slj-o jan; j+)
3
Pnntf "sovked Elmbs \n"); Scanf(A", daCiJ1);
li-o isn, is+)
3
Print d " al:J), Prinf("Entes the B Hanx £lemenb ");
lico 1am,1++)
oulput
Ele the no tlemen h 5
Fol-on;Jr+)
63
39 2
Scanf (yd", ibE:3);
Sorte e lameni

Nan adaiion ")


Prntf (
forio,i4m it+)

fo Ljeo;jsn ir+)

3
3

Downloaded from : www.eduengineering.in SNNAIC

01U ye r DEPARTMENT OF
SCIENCE AND HUMANITIES
Subject Code/Title: cS 3251- Programming in C Unit:

Scalad maln n)
Pnlf Re cutdand maln?") Printf
6li-o iam ++ tor liro im 7tt)
pvintfn")
Pntf In")
forjo jan j++)
for ljro, jen r+)
Prntf(d", afJ[j7)
prntf (yd , ctiJ1)
3 3
Cpu
Fnto the Simp a matr
3
Scalng Halv Vaue ANal nx
ne the
2

n c lude <tonio. h 2

void mant) Fnl. the Scabrg aco


Hnt alwJ,i,j,m, n, sF
hinfEla. the Snp fa mat &alal Hati7
onf,à rd dn ,an)

PrintfFnles tho values n ma") 18


2
for (t-o iam, is 4)
b
jru)
fov (j-o jn.
Sanf7dJat.ig

PnfEnlë. the Saelra Jacda


Scan d sf)
iam i+)
fo o

Foi- jn jra)
aCEJ sf
Downloaded from : www.eduengineering.in
Subject Code/Title: CS 3251- Programming in C Unit:

Com pule mean Valun N uunbes1°jjtl,jnjt+)


oclude <stdio.h> 2 Cor>atj])
#include <conio.h> 2taCri; atiJ-a )J;a GT-t
void man()
unt n,i afanJ
median: la [Vs]1a|% -I])/a ;
loa meon, Sum 00 elne
Prinh["Enla tho no q elmb "); median a n-% I
Ganf "d", dn); Prinl f(" Median 7f ", median)
Prnlfl" Enlar the elomena" Oudul
Enl the no q Elmh b
forlizo ikn itt 7
Fnlou the elemenb 2 5 4 8
Scanf ( 7.d ", dali)
Hedian5.000
for i:o 1an it) Nos
Compde moda Valuue n
Sum Sum ta [ij;
#ncdo stelioh
mean Sun/n
Void maun (
Prinl fl" moan = 7.f )o", mean )
k-o, C=l, mat :
o

ouk puut ind i,i, n,


o;
alao]-ho3,bt2j
tht modo
Ente tho no. elomenb
45Printf ("Ena the hmit "
elemenb : s a 9
Enla the Sanf"d", dn) numkers" );
mean - 5 . 8 333 Printfl" En the
144)
for (izo; ian;
datii)D
median volua q'n numiaisscanf ["/d",
Compute for (i0 isn;i+ t)
# clude <stdio.h> 2 mode o ,

Yoid man t )
fo +jn J7)
flaL)J: =aljJ)
int atoJ, ij,t,n made t+
Lmode!-o))
F (tmoda Smar Jd&
loa me dian =mode. bCkJ : arrJ; kt
2 k=o ; ma7

the elomenh ");


Pint ( Enle (mode = =
maz)
Sanf 1d", un); elne 3
elemenh ");
bEKJ: aliJ; krt
Prnf"nl the
for li =o an; i11)
or fi o;ian14) b£7)
a l J: :

Scanf (" 7d", da[i]), Ctt


jor (i=o; ieniH) 13
Downloaded from : www.eduengineering.in CHENNA DEPARTMENT OF
SCIENCE AND HUMANITIE
Subject Code/Title: Cs 3251- Programming inC Unit:

Cc-=n)
PrntyThauo no meda ) cupu
55
else Enl the Lmu
En the humbors
pintf Nodo ")
2

for (i0 iak; 1**+


Printf" d , bti); Mode =
3

Two marks

Define frrvay
a a cstletusn Sumlas Valua
Avalue aa
stora bntinunu momarylocalusrs . l
oni ed
nane Tha elemonts e Og
Shae a lornmon
Yolues
in Sacuonial osdeo

t afa

arras tn C
Crealo an array
Can
Houw
locw
Croala an array
6fereal nkox
> Kauayname L5ieJ;
Syrtox <dala ype
Fample: iat afo]
uith the he the undox Values
Aays
are re fe7enced
tha riabla avray.
as o n
notaton to ropvesent
tha he aray Contains n tegers thon the
Assume
*
dend tthe ovue ard the
st elernent ai

Undored wth n- Value


laat eloment aue

Downloaded from : www.eduengineering.in DEPARTMENT OF


SCIENCE AND HUMANITIES
Unit:
Subject Code/Title: CS 3251- Programming in C

wrilo examplo odo o declao tuo diman Sormal are.

cha name [bJ[u


ndoxe uhsvipb Those two
HaLe Le have tuo
referved as mus and colurmna. [bJ re fors t
thdeves ave

cslumn
wws and [ 7efors

avaîlale n cC
st the
Strn9 Juncdion
String
getcha C) 2 le chara cte Junctions
Put chau c2

gebsc 1Mul chaocle unttions


pubc

?
String with example
5 Define A sring
Qray
characters callad a Sing.
null
chaacte
Chaiacte o.
lo.
tem nalad by
a
Her2
Forxaple Conside CProqrammirg
Compilo e ntanters
tanters
When, Complai en
a String. chaa cte lo
Cpr9ammir9 chaacle
nul
null
automati cally arpends a
slings,
a he end o shing.
" e Prgramming
Erampla.. Cha c LJ
re on extmple
tor hibaialion fsrin aray
b stdi o. h>
hclude
Yod maus iven Stnrg He lo.
chas str CJ: Hello "
Prinf" yiven shing : 7.s ", Str)
3
Downloaded from : www.eduengineering.in cHENNAI DEPARTMENT OF
SCIENCE AND
HUMANITIES

HNL
Subject Code/Title: CS 3251-Programming in C Unit:

tuo hau uncli ons uGed for Stving Handlin


Noume a
SHrcmpC) t Strlent)
shcac) Srncat c>
Sncmp)

whal a Sling Cbray manipulalun


Functias
Junctians ave ae

Set Sun ple shrng


A
A oon
Sorne Systema iu
Sorne System
Sing, h> fl functiond
Cnplemente ha Som2 wse

<Shings. h>. The shig rray


Stving ay
strcat, Stre
cinqs
Eke StrEI,
w bing ith
strlen sl stu etc

mean by Serting
9 hal i a is} e mens
ss arangng
the
Proe
Sorbin
Soring
aula orda There ae manuy types S o ting
c a part
techmd u o
B u b b l e Bort

Tnserken sort
Seleckon Sovt
ui ck Sovt
*Merqe sort

*Heap sovt

Downloaded from : www.eduengineering.in CHENNA


INOAOGY
DEPARTMENT OF
SCIENCE AND HUMANITIES

You might also like