Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 4 Array

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

Ar

ray
Anarrayisdef
inedasthecoll
ect
ionofsi
mil
art
ypeofdat
ait
emsst
oredat
cont
iguousmemor yl
ocat
ions.

Arraysarethederiveddat
at y
peinCpr ogramminglanguagewhi chcan
storethepri
miti
vetypeofdatasuchasint
,char
,double,f
loat,etc.I
tal
so
hast hecapabil
i
tytostorethecoll
ect
ionofderi
veddatat ypes,suchas
pointer
s,st
ruct
ure,
etc.

Thearrayisthesimplestdat
astruct
urewher
eeachdat
ael
ementcanbe
randomlyaccessedbyusingi
tsi
ndexnumber.

Pr
oper
ti
esofAr
ray
Thear
raycont
ainst
hef
oll
owi
ngpr
oper
ti
es.

o Eachelementofanarr
ayi
sofsamedat
aty
peandcar
ri
est
hesame
si
ze,i
.e.
,i
nt=4by t
es.
o El
ementsoft hear
rayarest
oredatcont
iguousmemoryl ocat
ions
wher
ethefi
rstel
ementi
sstor
edatt
hesmallestmemor
ylocati
on.
o Elements ofthe ar
raycan be r
andomlyaccessed si
nce we can
calcul
atetheaddressofeachelementofthearraywiththegiven
baseaddressandthesizeoft
hedatael
ement.

Adv
ant
ageofCAr
ray
1)CodeOpt
imi
zat
ion:
Lesscodet
otheaccesst
hedat
a.

2)Easeoftraver
sing:Byusi
ngt
hef
orl
oop,
wecanr
etr
iev
etheel
ement
sof
anarrayeasi
ly
.

3)Easeofsor
ti
ng:Tosor
ttheel
ement
soft
hear
ray
,weneedaf
ewl
i
nesof
codeonl
y.

4)Random Access:
Wecanaccessanyel
ementr
andoml
yusi
ngt
hear
ray
.
Di
sadv
ant
ageofCAr
ray
1)FixedSize:Whateversize,wedef ineattheti
meofdeclarat
ionofthe
ar
ray,wecan'texceedtheli
mi t
.So,itdoesn'
tgr
owt hesi
zedy
nami cal
l
yli
ke
Li
nkedListwhichwewilll
earnlater
.

Decl
arat
ionofCAr
ray
Wecandecl
areanar
rayi
nthecl
anguagei
nthef
oll
owi
ngway
.

Dat
aty
pe ar
ray
name[
arr
ay_
size]
;
Now,
letusseet
heexampl
etodecl
aret
hear
ray
.

i
ntmar
ks[
5];
Her
e,i
nti
sthedat
a_t
ype,
mar
ksar
ethear
ray
_name,
and5i
sthear
ray
_si
ze.

I
nit
ial
i
zat
ionofCAr
ray
Thesimpl
estwayt
oi ni
ti
ali
zeanarr
ayisbyusingtheindexofeachelement.Wecan
i
nit
ial
i
zeeachel
ementofthear
raybyusi
ngt
heindex.Considert
hef
oll
owingexampl
e.

1.mar
ks[
0]=80;
//i
nit
ial
i
zat
ionofar
ray
2.mar
ks[
1]=60;
3.mar
ks[
2]=70;
4.mar
ks[
3]=85;
5.mar
ks[
4]=75;
Car
rayexampl
e

1.#i
ncl
ude<st
dio.
h>
2.i
ntmai
n()
{
3.i
nti
=0;
4.i
ntmar
ks[
5];
//decl
arat
ionofar
ray
5.mar
ks[
0]=80;
//i
nit
ial
i
zat
ionofar
ray
6.mar
ks[
1]=60;
7.mar
ks[
2]=70;
8.mar
ks[
3]=85;
9.mar
ks[
4]=75;
10.
//t
rav
ersal
ofar
ray
11.
for
(i
=0;
i
<5;
i
++)
{
12.
pri
ntf
("%d\
n",
mar
ks[
i]
);
13.
}//
endoff
orl
oop
14.
ret
urn0;
15.
}

Out
put

80
60
70
85
75

CAr
ray
:Decl
arat
ionwi
thI
nit
ial
i
zat
ion
Wecani
nit
ial
i
zet
hecar
rayatt
het
imeofdecl
arat
ion.Let
'sseet
hecode.

1.i
ntmar
ks[
5]={
20,
30,
40,
50,
60}
;

I
nsuchcase,
ther
eisnor
equi
rementt
odef
inet
hesi
ze.Soi
tmayal
sobewr
it
tenast
he
f
oll
owi
ngcode.

1.i
ntmar
ks[
]={
20,
30,
40,
50,
60}
;

Let
'sseet
heCpr
ogr
am t
odecl
areandi
nit
ial
i
zet
hear
rayi
nC.

1.#i
ncl
ude<st
dio.
h>
2.i
ntmai
n()
{
3.i
nti
=0;
4.i
ntmar
ks[
5]={
20,
30,
40,
50,
60}
;/
/decl
arat
ionandi
nit
ial
i
zat
ionofar
ray
5. /
/tr
aver
sal
ofar
ray
6.f
or(
i=0;
i
<5;
i
++)
{
7.pr
int
f("
%d\
n",
mar
ks[
i]
);
8.}
9.r
etur
n0;
10.
}

Out
put

20
30
40
50
60

Mul
tiDi
mensi
onal
Arr
ay
AnAr
rayhav
ingmor
ethanonedi
mensi
oni
scal
l
edMul
tiDi
mensi
onal
arr
ayi
nC.

Ty
pesofCMul
ti
di
mensi
onal
Arr
ay:
-
Bel
owar
ethet
ypesofmul
ti
dimensi
onal
arr
ays:
-

1.TwoDi
mensi
onal
Arr
ay

2.Thr
eeDi
mensi
onal
Arr
ay

3.FourDi
mensi
onal
Arr
ay

Decl
ari
ngmul
ti
di
mensi
onal
arr
aysi
nC
TwoDi
mensi
onal
Arr
ay:
-
dat
aty
pename[
size_
1][
size_
2];

Exampl
e:-
i
ntnum1[
2][
3];

f
loatnum2[
5][
6];

2.Thr
eeDi
mensi
onal
Arr
ay:
-
dat
aty
pename[
size_
1][
size_
2][
size_
3];

Exampl
e:-
i
ntst
udent
s[2]
[3]
[4]
;

f
loatbi
l
ls[
3][
4][
3];

3.N-
Dimensi
onal
Arr
ay:
-
Gener
alDecl
arat
ionofmul
ti
dimensi
onal
arr
ay:
-
t
ypename[
size1]
[si
ze2]
..
.[
sizeN]
;

1.type:
–Itdenot esdataty
penameslikei
nt,f
loatetc.
2.name:–I tdenotesthenameofthemult
idimensionalar
ray
.
3.row-si
ze:–Numberofr owelements.
4.column-size:
-Numberofcolumnelements.

TwoDi
mensi
onal
Arr
ayi
nC
Thet wo-dimensi
onalar r
aycanbedef inedasanar rayofar rays.The2D arrayi
s
organizedasmatriceswhichcanber epr
esent
edasthecollect
ionofrowsandcolumns.
Howev er,2D arr
ay sarecreatedt oimplementarelati
onaldatabaselookal
i
kedata
struct
ure.I
tprovi
deseaseofhol di
ngthebulkofdat
aatoncewhi chcanbepassedto
anynumberoff unctionswher
ev err
equir
ed.

Decl
arat
ionoft
wodi
mensi
onal
Arr
ayi
nC
Thesy
ntaxt
odecl
aret
he2Dar
rayi
sgi
venbel
ow.

1.dat
a_t
ypear
ray
_name[
rows]
[col
umns]
;

Consi
dert
hef
oll
owi
ngexampl
e.

1.i
ntt
wodi
men[
4][
3];

Her
e,4i
sthenumberofr
ows,
and3i
sthenumberofcol
umns.

25.
2M
488

I
nit
ial
i
zat
ionof2DAr
rayi
nC
I
nt he1Dar r
ay,wedon'tneedt ospecifythesizeoft hearr
ayifthedeclar
ati
onand
i
nit
ial
izati
onarebeingdonesimul t
aneously
.Howev er
,thiswil
lnotworkwit
h2Dar r
ays.
Wewi llhavetodef
ineatleasttheseconddimensionoft hear
ray.Thetwo-
dimensi
onal
ar
raycanbedecl ar
edanddef i
nedinthefoll
owingway .

1.i
ntar
r[
4][
3]={
{1,
2,
3},
{2,
3,
4},
{3,
4,
5},
{4,
5,
6}}
;

Two-
dimensi
onal
arr
ayex
ampl
einC
1.#i
ncl
ude<st
dio.
h>
2.i
ntmai
n()
{
3.i
nti
=0,
j
=0;
4.i
ntar
r[
4][
3]={
{1,
2,
3},
{2,
3,
4},
{3,
4,
5},
{4,
5,
6}}
;
5./
/tr
aver
sing2Dar
ray
6.f
or(
i=0;
i
<4;
i
++)
{
7. f
or(
j=0;
j
<3;
j
++)
{
8. pr
int
f("
arr
[%d][
%d]=%d\
n",
i
,j
,ar
r[
i]
[j
])
;
9. }
//endofj
10.
}//
endofi
11.
ret
urn0;
12.
}

Out
put

ar
r[
0][
0]=1
ar
r[
0][
1]=2
ar
r[
0][
2]=3
ar
r[
1][
0]=2
ar
r[
1][
1]=3
ar
r[
1][
2]=4
ar
r[
2][
0]=3
ar
r[
2][
1]=4
ar
r[
2][
2]=5
ar
r[
3][
0]=4
ar
r[
3][
1]=5
ar
r[
3][
2]=6

I
mpl
ement
ati
onofMul
ti
di
mensi
onal
Arr
ayi
n
C
1.TwoDi
mensi
onal
Arr
ayi
nC
TwoDimensi
onal ar
raysarei
mplementedusi
ngrowsandcol
umns.A2-d
ar
rayisacol
lect
ionof1-darr
ays.Def
aultf
ormati
srow-
major
.Iti
sthe
si
mplestf
orm ofmulti
dimensi
onalar
ray.

Sy
ntax:
-
dat
aty
pename[
size_
1][
size_
2];

Exampl
e:-
i
ntnum1[
2][
3];
Int
heaboveexampl
e,2i
sthenumberofr
owsand3i
sthenumberof
col
umns.

I
nit
ial
i
zat
ionoft
wodi
mensi
onal
arr
ayi
nC:
-
Ther
ear
etwoway
sinwhi
chy
oucani
nit
ial
i
zeat
wodi
mensi
onal
arr
ay:
-

Met
hod1:
-
i
ntx[
2][
4]={
1,2,
3,4,
5,6,
7,8}

Abovearr
ayhas2r
owsand4col
umns.Andt
heel
ement
swi
l
lbest
oredf
rom
l
efttor
ight
.
Method2:-
i
ntx[
2][
4]={
{1,
2,
3,
4},
{5,
6,
7,
8}}
;

Thi
smethodi sabet t
erone.Becausei
tusesnest
edbraces.I
nthi
s,eachset
ofi
nnerbracesr
epr esent
soner ow.I
ntheabovear
ray
,thereare2r
owst hat
’s
whyther
ear e2innerbraces.
Memor
yal
l
ocat
ionof2DAr
ray
:-
Col
umn0 Col
umn1 Col
umn2
Row0 [
0][
0] [
0][
1] [
0][
2]
Row1 [
1][
0] [
1][
1] [
1][
2]
Row2 [
2][
0] [
2][
1] [
2][
2]

Suppose,y
ouhaveatwodimensi
onalar
rayl
i
ket
hisx[
i]
[j
].Her
e,ii
sther
ow
numberandji
sthecol
umnnumber.

Thr
eeDi
mensi
onal
Arr
ay:
-
I
tisdefi
nedasanarr
ayofar r
ays.Youneedmor ethanthreedi
mensi
onsto
i
mplementa3darray
.Ifyouareabegi nnert
henitwil
ll
ookmessytoyou.But
onceyoukeeppr
acti
cingthenitwil
lgeteasi
ertounderst
and.

Sy
ntax:
-
dat
aty
pename[
size_
1][
size_
2][
size_
3];

Exampl
e:-
i
ntmar
ks[
2][
3][
2];
I
nit
ial
i
zat
ionof3di
mensi
onal
arr
ayi
nC
3darrayini
ti
ali
zat
ionist hesamewayastheini
ti
ali
zat
ionoftwodimensional
ar
rays.Theonlydiff
erenceisthati
na3darray,
thenumberofdimensions
i
ncreases.Duetothat,thenumberofnest
edbraceswill
alsoi
ncrease.

Met
hod1:
-
i
ntx[
2][
2][
2]={
9,10,
5,6,
7,9,
2,2}
;

Met
hod2:
-(ABet
terone)
i
ntx[
2][
2][
2]={
{{9,
10}
,{
5,6}
},
{{
7,9}
,{
2,2}
}};

Accessi
ngel
ement
sint
hreedi
mensi
onal
arr
ay:
-
Accessi
ngelementsinathr
eedimensionalarr
ayispret
tymuchthesameasa
twodimensi
onalarr
ay.Theonl
ydiff
erenceisthatyouhavetousethr
eel
oops
fort
heextr
aadditi
onofadimensioninat hr
eedimensional
arr
ay.

Four
-Di
mensi
onal
Arr
ayi
nC
I
tisver
ydif
fi
cul
ttoi
mpl
ement
.Iti
sanar
rayoft
hreedi
mensi
onal
arr
ays.I
t’
sa
messyone.

Sy
ntax:
-
dat
aty
pename[
size_
1][
size_
2][
size_
3][
size_
4];

STRINGSAst ri
ngisasequenceofcharacterenclosedwithindoubl
equotes(“”
)butendswith
\0.Thecompi l
erput
s\ 0attheendofstri
ngt ospecifytheendofthestr
ing.Togetavalueof
str
ingv ar
iabl
ewecanuset hetwo dif
ferenttypesoff or
mats.Usi
ngscanf()funct
ionas:
scanf(
“%s”,st
ri
ngvari
able)
;Usi
ngget
s()functionas: get
s(str
ingv
ari
abl
e);

STRI
NGHANDLI
NGFUNCTI
ONS

l
i
brar
ysupport
sal ar
genumberofst
ri
nghandl
i
ngf
unct
ions.Thosef
unct
ionsar
est
oredunder
t
heheaderf
il
estr
ing.
hint
heprogr
am.
St
ri
ngFunct
ions
Ther
ear
emanyi
mpor
tantst
ri
ngf
unct
ionsdef
inedi
n"st
ri
ng.
h"l
i
brar
y.

No. Funct
ion Descr
ipt
ion

1) st
rl
en(
str
ing_
name) r
etur
nst
hel
engt
hofst
ri
ngname.

2) st
rcpy
(dest
inat
ion,
sour
ce) copi
esthecontent
sofsour
cest
ri
ngt
o
dest
inat
ionst
ri
ng.

3) st
rcat
(fi
rst
_st
ri
ng,
second_
str
ing) concatsorjoi
nsfi
rstst
ri
ngwithsecond
str
ing.Theresul
tofthestr
ingi
sstored
i
nf i
rstst
ri
ng.

4) st
rcmp(
fi
rst
_st
ri
ng,
second_
str
ing) comparest hefi
rststr
ingwit
hsecond
str
ing.If both st
ri
ngs ar
e same,it
ret
urns0.

5) st
rrev
(st
ri
ng) r
etur
nsr
ever
sest
ri
ng.

6) st
rl
wr(
str
ing) r
etur
nsst
ri
ngchar
act
ersi
nlower
case.

7) st
rupr
(st
ri
ng) r
etur
nsst
ri
ngchar
act
ersi
nupper
case.

St
ri
ngLengt
h:st
rl
en(
)funct
ion
Thest
rl
en(
)funct
ionr
etur
nst
hel
engt
hoft
hegi
venst
ri
ng.I
tdoesn'
tcountnul
lchar
act
er
'
\0'
.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4.charch[
20]
={'
j
','
a'
,'
v'
,'
a'
,'
t'
,'
p'
,'
o'
,'
i'
,'
n'
,'
t'
,'
\0'
};
5. pr
int
f("
Lengt
hofst
ri
ngi
s:%d"
,st
rl
en(
ch)
);
6. r
etur
n0;
7.}

Out
put
:

Lengt
hofst
ri
ngi
s:10

CopySt
ri
ng:
str
cpy
()
Thest
rcpy
(dest
inat
ion,
sour
ce)f
unct
ioncopi
est
hesour
cest
ri
ngi
ndest
inat
ion.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charch[
20]
={'
j
','
a'
,'
v'
,'
a'
,'
t'
,'
p'
,'
o'
,'
i'
,'
n'
,'
t'
,'
\0'
};
5. charch2[
20]
;
6. st
rcpy
(ch2,
ch)
;
7. pr
int
f("
Val
ueofsecondst
ri
ngi
s:%s"
,ch2)
;
8. r
etur
n0;
9.}

Out
put
:

Val
ueofsecondst
ri
ngi
s:j
avat
poi
nt

St
ri
ngConcat
enat
ion:
str
cat
()
Thest r
cat
(fi
rst
_st
ri
ng,second_
str
ing)f
unct
ionconcat
enat
est
wost
ri
ngsandr
esul
tis
ret
urnedtofi
rst
_st
ring.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charch[
10]
={'
h'
,'
e'
,'
l'
,'
l'
,'
o'
,'
\0'
};
5. charch2[
10]
={'
c'
,'
\0'
};
6. st
rcat
(ch,
ch2)
;
7. pr
int
f("
Val
ueoff
ir
stst
ri
ngi
s:%s"
,ch)
;
8. r
etur
n0;
9.}

Out
put
:

Val
ueoff
ir
stst
ri
ngi
s:hel
l
oc

Compar
eSt
ri
ng:
str
cmp(
)
Thest
rcmp(fi
rst
_str
ing,second_
str
ing)f
unct
ioncompar
est
wost
ri
ngandr
etur
ns0i
f
bot
hstr
ingsareequal.

Her
e,wear ngget
eusi s()f
unct
ionwhi
chr
eadsst
ri
ngf
rom t
heconsol
e.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charst
r1[
20]
,
str
2[20]
;
5. pr
int
f("
Ent
er1stst
ri
ng:
");
6. get
s(st
r1)
;/
/readsst
ri
ngf
rom consol
e
7. pr
int
f("
Ent
er2ndst
ri
ng:
");
8. get
s(st
r2)
;
9. i
f(
str
cmp(
str
1,st
r2)
==0)
10. pr
int
f("
Str
ingsar
eequal
")
;
11.el
se
12. pr
int
f("
Str
ingsar
enotequal
")
;
13.r
etur
n0;
14.
}

Out
put
:

Enter1stst
ri
ng:hel
l
o
Enter2ndstr
ing:
hell
o
Str
ingsareequal

Rev
erseSt
ri
ng:
str
rev
()
Thestr
rev
(st
ri
ng)funct
ion r
etur
nsr
ever
seoft
hegi
ven st
ri
ng.Let
'sseea si
mpl
e
exampl
eofst
rrev
()f
unct
ion.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charst
r[
20]
;
5. pr
int
f("
Ent
erst
ri
ng:
");
6. get
s(st
r);
//r
eadsst
ri
ngf
rom consol
e
7. pr
int
f("
Str
ingi
s:%s"
,st
r);
8. pr
int
f("
\nRev
erseSt
ri
ngi
s:%s"
,st
rrev
(st
r))
;
9. r
etur
n0;
10.
}

Out
put
:

Enterstr
ing: j
avat
point
Str
ingis:javatpoi
nt
ReverseSt r
ingis:t
niopt
avaj

St
ri
ngLower
case:
str
lwr
()
Thestr
lwr
(st
ri
ng)functi
onretur
nsst
ri
ngchar
act
ersi
nlower
case.Let
'sseeasi
mpl
e
exampl
eofstr
lwr
()funct
ion.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charst
r[
20]
;
5. pr
int
f("
Ent
erst
ri
ng:
");
6. get
s(st
r);
//r
eadsst
ri
ngf
rom consol
e
7. pr
int
f("
Str
ingi
s:%s"
,st
r);
8. pr
int
f("
\nLowerSt
ri
ngi
s:%s"
,st
rl
wr(
str
));
9. r
etur
n0;
10.
}

Out
put
:
Enterstr
ing:JAVATpoi
nt
Str
ingis:JAVATpoint
LowerStringis:
jav
atpoi
nt

St
ri
ngUpper
case:
str
upr
()
Thestr
upr
(st
ri
ng)functi
onretur
nsst
ri
ngchar
act
ersi
nupper
case.Let
'sseeasi
mpl
e
exampl
eofst
rupr
()funct
ion.

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charst
r[
20]
;
5. pr
int
f("
Ent
erst
ri
ng:
");
6. get
s(st
r);
//r
eadsst
ri
ngf
rom consol
e
7. pr
int
f("
Str
ingi
s:%s"
,st
r);
8. pr
int
f("
\nUpperSt
ri
ngi
s:%s"
,st
rupr
(st
r))
;
9. r
etur
n0;
10.
}

Out
put
:

Enterstr
ing:javat
poi
nt
Str
ingis:javatpoi
nt
UpperStringis:JAVATPOI
NT

St
ri
ngst
rst
r()
Thest
rstr(
)funct
ionret
urnspoi
ntert
othefi
rstoccur
renceofthemat chedstri
ngi
nthe
gi
venstr
ing.I
tisusedtoret
urnsubst
ri
ngfr
om fi
rstmatchti
l
lthelastcharact
er.

Sy
ntax:

1.char*
str
str
(constchar*
str
ing,
constchar*
mat
ch)

St
ri
ngst
rst
r()par
amet
ers
st
ri
ng:I
trepr
esent
sthef
ull
str
ingf
rom wher
esubst
ri
ngwi
l
lbesear
ched.
mat
ch:I
trepr
esent
sthesubst
ri
ngt
obesear
chedi
nthef
ull
str
ing.

St
ri
ngst
rst
r()exampl
e

1.#i
ncl
ude<st
dio.
h>
2.#i
ncl
ude<st
ri
ng.
h>
3.i
ntmai
n()
{
4. charst
r[
100]
="t
hisi
sjav
atpoi
ntwi
thcandj
ava"
;
5. char*
sub;
6. sub=st
rst
r(st
r,
"j
ava"
);
7. pr
int
f("
\nSubst
ri
ngi
s:%s"
,sub)
;
8. r
etur
n0;
9.}

Out
put
:

j
avat
poi
ntwi
thcandj
ava

You might also like