You are on page 1of 6

LabRepor

t
St
udentName:Salem AhmedAlgassar
Depar
tment
:ComputerEngi
neer
ing
Exampl
eNo.
1:

ForLoop
CodeExpl
anat
ion:

1.I
ncl
udi
ngtheiost
ream headerf
il
einourcode.I
twi
l
lal
l
owust
oreadf
rom and
wri
tet
otheconsole.

2.Incl
udi
ngthest
dnamespacesoast
ousei
tscl
assesandf
unct
ionswi
thout
call
i
ngit.

3.Call
i
ngthemai
n()f
unct
ioni
nsi
dewhi
cht
hel
ogi
coft
hepr
ogr
am shoul
dbe
added.

4.The{mar
ksst
artofbodyoft
hemai
n()f
unct
ion.

5.Decl
ari
ngint
egervari
abl
es,
x,num,
andf
act
ori
al.Thev
ari
abl
efact
ori
alhas
beenassi
gnedavalueof1.

6.Pr
int
ingsomet
extont
heconsol
e.

7.Pr
ompt
ingusert
oent
erav
aluef
orv
ari
abl
enum.

8.Creati
ngaforl
oop.Theinit
ial
i
zationcreatesanintegerv
ari
ablexandassigns
i
tav al
ueof1.Thecondit
ionstatesthatthev al
ueofxmustbel essthanor
equaltov
alueofvari
abl
enum.Thei ncr
ementi ncr
easesthevalueofxby1
aft
erever
yiter
ati
on.The{mar ksthebeginningofthebodyoftheforloop.

9.Cal
cul
ati
ngt
hev
alueoff
act
ori
alusi
ngt
hef
ormul
afact
ori
al=f
act
ori
al*x.

10.Endoft
hel
oopbody
.

11.Toprintthev
alueofv
ari
abl
esnum andf
act
ori
alal
ongsi
deot
hert
extont
he
console.

12.Themai
n()f
unct
ionshoul
dret
urnanv
aluei
fthepr
ogr
am r
unsf
ine.

13.Endoft
hebodyoft
hemai
n()f
unct
ion.

Summar
y

 Thef
orl
oopi
ter
atesasect
ionofC++codef
oraf
ixednumberoft
imes.

 Thef
orl
oopr
unsasl
ongast
het
estcondi
ti
oni
str
ue.

 Thei
nit
ial
i
zat
ionpar
toff
orl
oopi
sfordecl
ari
ngandi
nit
ial
i
zinganyl
oop
cont
rol
var
iabl
es.

 Thecondi
ti
onpar
toff
orl
oopmustbet
ruef
orl
oopbodyt
obeexecut
ed.

 Thei
ncr
ementpar
toft
hef
orl
oopcanber
epl
acedwi
thasemi
col
on.
Exampl
eNo.
2:

Ar
ray
CodeExpl
anat
ion:

1.I
ncl
udi
ngtheiost
ream headerf
il
einourcode.I
twi
l
lal
l
owust
oreadf
rom and
wri
tet
otheconsole.

2.Incl
udi
ngthest
dnamespacesoast
ousei
tscl
assesandf
unct
ionswi
thout
call
i
ngit.

3.Cal
l
ingt
hemai
n()f
unct
ionwi
thi
nwhi
chcodeshoul
dbeadded.

4.St
artoft
hebodyoft
hemai
n()f
unct
ion.

5.Acomment
.TheC++compi
l
erwi
l
lski
pthi
s.

6.Decl
ari
nga2Dar
rayof3r
owsand2col
umns.I
temshav
eal
sobeenaddedt
o
t
hearray.

7.Acomment
.TheC++compi
l
erwi
l
lski
pthi
s.

8.Cr
eati
ngavari
abl
eiusi
ngaf
oral
oop.Thi
svar
iabl
ewi
l
lit
erat
eov
ert
her
ow
i
ndexesoft
hearr
ay.

9.Creati
ngav ar
iabl
ejusi
ngaforal
oop.Thi
svar
iabl
ewi
l
lit
erat
eov
ert
he
columnindexesofthear
ray
.

10.St
artoft
hebodyoft
hel
oops.

11.Pri
nttheval
uesofv
ari
abl
esi
andj
ont
heconsol
einsi
desquar
ebr
acket
son
theconsol
e.

12.Pr
intoutt
hev
aluest
oredati
ndex[
i]
[j
]oft
hear
raya.

13.Endoft
hebodyoft
hel
oops.

14.Themai
n()f
unct
ionshoul
dret
urnani
ntegerv
aluei
fthepr
ogr
am r
unsf
ine.

15.Endoft
hebodyoft
hemai
n()f
unct
ion.
Exampl
eNo.
3:

Funct
ion
CodeExpl
anat
ion:

1.I
ncl
udet
hei
ost
ream headerf
il
einourpr
ogr
am t
ousei
tsf
unct
ions.

2.Incl
udethecmathli
brar
ytousei
tsf
unct
ions.Wewantt
ouset
hef
unct
ion
sqrt
()defi
nedi
nit.

3.I
ncl
udet
hest
dnamespacei
nourcodet
ousei
tscl
asseswi
thoutcal
l
ingi
t.

4.Cal
lthemain()f
unct
ion.Thepr
ogr
am l
ogi
cshoul
dbeaddedwi
thi
nthebody
oft
hisfunct
ion.

5.Decl
aret
wodoubl
evar
iabl
es,
num,
andsquar
eRoot
.

6.Pr
intsomet
extont
heconsol
e.Thet
extaskst
heusert
oent
eranumber
.

7.Readuseri
nputf
rom t
hekey
boar
d.Thei
nputwi
l
lbecomet
hev
alueof
var
iabl
enum.

8.Callt
heli
braryf
unct
ionsqrt
(),
whichcalcul
atesthesquarerootofanumber.
Wepassedt hepar
ameternum tothefunct
ion,meaningitwil
lcalcul
atet
he
squarer
ootofnum.Thisfuncti
onisdefi
nedinthecmat hli
brary
.

9.Pri
ntthenumberent
eredbyt
heuser
,it
ssquar
eroot
,andsomeot
hert
exton
theconsol
e.

10.Thepr
ogr
am mustr
etur
nval
ueuponsuccessf
ulcompl
eti
on.

11.Endoft
hebodyoft
hemai
n()f
unct
ion.

You might also like