Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes GET 211

Rating
-
Sold
-
Pages
32
Uploaded on
23-01-2024
Written in
2022/2023

Computer programming GET 211 covers the following topics: Introduction to Computer Programming; Program Design . Program Design . Algorithms , Pseudocode .Flowcharts . Basic Control Structures High Level Programming . Variables and Data Types . Operators .Branching and Control Statements . Loops . Arrays .High Level Programming

Show more Read less
Institution
Course

Content preview

NOTE:
Bef
orel
ooki
ngathowt
owr
it
eC++pr
ogr
amsconsi
dert
hef
oll
owi
ngsi
mpl
eexampl
e
pr
ogr
am

//myf i
rstpr
ogram i
nC++
#incl
ude<iost
ream.h>
usingnamespacestd;
i
ntmai n()
{
cout<<"Hell
oWor l
d!"
;
retur
n0;
}


/
/myf
ir
stpr
ogr
am i
nC++

Thisis a commentl ine.Al lli
nes beginning withtwo sl ash signs ( //
)ar e
consideredcomment sanddonothav eanyef fectont hebehav iouroft hepr ogram.
Linesbeginni
ngwi t
hahashsi gn( #)aredir
ecti
v esforthepreprocessor .Theyar enot
regularcodeli
neswit hexpressionsbuti ndi
cat
ionsf orthecompi l
er '
spr eprocessor.In
this case t
he di
rective #include<iost
ream> tellsthe preprocessort oi nclude the
i
ost r
eam standar
df i
le.Thi sspeci fi
cfil
e( i
ost
ream)i ncludesthedecl arati
onsoft he
basicstandar
dinput-outputli
braryinC++,andi tisincl
udedbecausei tsfunctionali
tyis
goingtobeusedlaterint heprogram.

usi
ngnamespacest
d;

Alltheel
ementsofthest andar
dC++l ibr
aryaredeclar
edwit
hinwhatiscalleda
namespace,thenamespacewi t
ht henamest d.Soinordertoaccessitsfunct
ionali
ty
wedeclarewiththi
sexpressionthatwewi llbeusingtheseent
it
ies.Thisli
neisv ery
fr
equentinC++progr
amst hatusethestandardli
brar
y.


i
ntmai
n()

Thislinecor r
espondst othebegi nningoft hedefi
niti
onoft hemai nf uncti
on.The
mainf unction i sthe poi nt by wher e allC++ pr ograms st artt heir execution,
i
ndependent l
yofi tslocationwi thinthesour cecode.I tdoesnotmat terwhet herthere
areotherf unctionswi th ot hernamesdef ined befor
eoraf t
eri t-t hei nstructi
ons
contai
nedwi thinthi
sf unction'sdefini
tionwi l
lalway sbethef i
rstonest obeexecut edin
anyC++pr ogr am.Thewor dmai nisf ollowedi nthecodebyapai rofpar entheses( ()
).
Rightaftert heseparent heseswecanf indt hebodyoft hemai nf uncti
onencl osedi n
braces( {
}).Whati scont ainedwi t
hinthesebr acesiswhatt hefunct i
ondoeswheni tis
executed.

cout<<"
Hel
l
oWor
ld!
";
Thi
sli
nei
saC++st
atement
.Ast
atementi
sasi
mpl
eorcompoundexpr
essi
ont
hat

1

,canact ual
lypr oducesomeef fect.Inf act ,thisst atementper formst heonl yactiont hat
generatesav i
sibleeffectinourf i
rstprogr am.coutr epr
esentst hestandardout putstream
i
nC++,andt hemeani ngoft heent i
rest atementi st oinsertasequenceofchar acters( i
n
thiscasetheHel loWor ldsequenceofchar act ers)intothestandardout putstream ( which
usuallyisthescr een).couti sthei ostream st andar dfil
ewi thinthestdnamespace,so
declaredinthat'swhyweneededt oincludet hatspeci f
icfil
eandt odeclarethatwewer e
goingt ousethi sspecifi
cnamespaceear lierinourcode.Not icethatthest atementends
withasemi colonchar act
er(;)
.Thischar act erisusedt omar ktheendoft hestatementand
i
nf actitmustbei ncludedattheendofal lexpr essionstatement sinallC++pr ograms( one
oft hemostcommonsy ntaxerrorsisindeedt of orgettoincludesomesemi colonaf t
era
statement).


r
etur
n0;

Theret
urnst atementcausest hemainfuncti
ont ofini
sh.Theprogr
am hasbeen
str
uct
uredi
ndiff
erentl i
nesinordertobemorereadable,butinC++,wedonothavest
ri
ct
rul
esonhowtosepar ateinst
ruct
ionsi
ndif
fer
entl
ines.Forexample,
inst
eadof

i
ntmain()
{
cout<<"Hel
l
oWor
ld!
";
ret
urn0;
}

Wecoul
dhav
ewr
it
ten:

i
ntmai
n(){cout<<"
Hel
l
oWor
ld!
";r
etur
n0;
}

Alli
njustonel ineandt hiswoul dhavehadexact l
yt hesamemeani ngast he
previ
ouscode.I nC++,t heseparati
onbet weenstatementsisspecifi
edwi t
hanendi ng
semicolon(;
)att heendofeachone,sot heseparat
ionindiff
erentcodeli
nesdoesnot
matteratallforthispurpose.Wecanwr itemanyst at
ementsperl i
neorwr it
easi ngl
e
stat
ementthattakesmanycodel ines.Thedivi
sionofcodeindiff
erentl
inesservesonl
yto
makei tmorel
egibleandschematicforthehumansthatmayr eadit.

Att
hisst
agel
etusconsi
dert
hegener
alf
ormatofaC++pr
ogr
am:

Bef
orel
ooki
ngathowt
owr
it
eC++pr
ogr
amsconsi
deranot
hersampl
epr
ogr
am.

/
/Sampleprogram
/
/Readsvaluesfort
helengthandwidt
hofarectangle
/
/andret
urnstheperimeterandar
eaofther
ectangle.

#incl
ude<iostream.h>
voidmain()
{
i
ntlength,width;
i
ntperimeter,area; //decl
arat
ions
cout<<"Length=" ;/ /promptuser
ci
n>>l ength; /
/enterl
ength

2

, cout<<"Width=" ; //promptuser
ci
n>>wi dth; //i
nputwidth
peri
meter=2* (
length+widt
h); / /comput e
peri
meter
area=length*width; /
/comput earea
cout<<endl
<<"Peri
met eri
s"<<perimeter;
cout<<endl
<<"Areais"<<area
<<endl; /
/outputresul
ts
} /
/endofmai nprogram
Thef
oll
owi
ngpoi
ntsshoul
dbenot
edi
ntheabov
epr
ogr
am:

1)Anyt extfrom thesymbol s//unti
ltheendofthelinei signoredbyt hecompiler.This
faci
lit
yallowst heprogrammert oinsertComment si nt heprogram.Ev er
ypr ogram
shouldatl easthaveacommenti ndicati
ngthepr ogrammer 'sname,wheni twas
writ
tenandwhatt hepr ogram actual
lydoes.Anypr ogram thatisnotv erysimpl e
shouldal so havef ur
thercomment si ndi
cat
ingt hemaj orstepscar ri
ed outand
explaini
nganypar ti
cularl
ycompl expi eceofprogr ammi ng.Thi sisessent i
alt he
program hast obeamendedorcor rectedatalat
erdat e.

2)Thel
i
ne

#i
ncl
ude<i
ost
ream.
h>
causest hecompi l
ert oi
ncludethet extofthenamedf ile(i
nt scasei
hi ostr
eam.h)in
theprogr am atthispoint.Thef iei
l ostr
eam. hisasy st
em suppl
i
edfilewhichhas
defi
nit
ionsi nitwhi charer equi
redi fthepr ogr
am isgoingtousestream inputor
output
.Al lyourprogramswi lli
ncludethisfil
e.Thisst
atementisacompilerdi
recti
ve
--t
hatisi tgi
vesinformati
ont othecompi l
erbutdoesnotcauseanyexecutablecode
tobepr oduced.

3)Theact
ual
progr
am consi
stsoft
hef
unct
ionmai
nwhi
chcommencesatt
hel
i
ne

v
oidmai
n()
Allprogramsmusthav eaf uncti
onmai n.Notethattheopeni
ngbrace({
)marksthe
beginningofthebodyoft hef unct
ion,whi l
etheclosi
ngbrace(}
)indi
cat
estheendof
thebodyoft hef unct
ion.Thewor dv oi
di ndicat
esthatmaindoesnotret
urnavalue.
Runningt heprogram consi
stsofobey ingt hestat
ementsinthebodyofthef
unct
ion
main.

4)Thebodyofthefuncti
onmai
ncontai
nstheactualcodewhi
chi
sexecut
edbyt
he
comput
erandisencl
osed,
asnot
edabove,
inbr
aces{}.

5)Ever
ystatementwhichinst
ruct
sthecomputert
odosomethi
ngister
minat
edbya
semi-
col
on.Symbolssuchasmai n()
,{}etc.ar
enoti
nst
ruct
ionst
odosomethi
ng
andhencearenotf
ollowedbyasemi-
col
on.

6)Sequencesofcharact
ersencl
osedi
ndoubl
equot
esar
eli
ter
alst
ri
ngs.Thus
i
nstruct
ionssuchas
3

, cout<<"
Lengt
h="
sendt
hequotedchar
acter
stotheoutputst
ream cout.Thespeci
ali
denti
ferendl
i
whensentt
oanoutputstr
eam wi
llcauseanewli
net obetakenonoutput.

7)Allv
ari
ablesthatareusedi
naprogram mustbedeclar
edandgiv
enat ype.I
nthi
s
caseal
lthevari
ablesar
eoft
ypei
nt,i
.e.whol
enumbers.Thust
hestat
ement

i
ntl
engt
h,wi
dth;
declar
estothecompil
erthatintegerv
ari
ableslengt
handwi dtharegoingtobeused
bytheprogr
am.Thecompi lerreser
vesspaceinmemor yforthesevar
iabl
es.
8)Valuescanbegivent
ov ar
iablesbytheassignmentstat
ement ,e.
g.t
hestatement
ar
ea=l
engt
h*wi
dth;
eval
uat
estheexpr
essi
onontheright-
handsideoftheequal
ssignusingthecur
rent
val
uesofl
engt
handwidthandassignsther
esult
ingval
uetothevar
iablear
ea.

9)Layoutofthepr ogram i
squi tearbit
rary
,i.
e.new li
nes,spacesetc.canbei nsert
ed
whereverdesiredandwi llbeignoredbyt hecompi l
er.Thepr i
meai m ofaddit
ional
spaces,newlines,etc.i
st omaket heprogram morereadable.Howeversuperfl
uous
spacesornewl i
nesmustnotbei nsert
edinwordslikemain, cout
,invar
iabl
enames
orinstr
ings(unlessyouact ual
lywantthem pr
inted)
.


VARIABLES
Avari
ablei
sasy mboli
cnameforamemoryl
ocati
oni
nwhi
chdat
acanbest
oredand
subsequent
lyrecal
led.Var
iabl
esareusedforhol
dingdat
aval
uessothatt
heycanbe
uti
li
zedinvar
iouscomputat
ionsinapr
ogram.

Al
lvar
iabl
eshav
etwoi
mpor
tantat
tri
but
es:

1)Atypewhichi
sest
abl
ishedwhent
hevari
ablei
sdefi
ned(e.g.
,i
nteger
,real
,char
act
er)
.
Oncedef
ined,
thet
ypeofaC++var
iabl
ecannotbechanged.

2)Avaluewhichcanbechangedbyassigni
nganewvaluetothevari
able.Thekindof
val
uesav ari
abl
ecanassumedependsoni t
sty
pe.Forexample,anintegerv
ariabl
e
canonlyt
akeint
egerv
alues(e.
g.,
2,100,-
12)
.


Avariabl
ei susedf ort
hequant
it
ieswhichar emani
pulatedbyacomputerpr
ogram.For
exampleapr ogram thatreadsaseri
esofnumber sandsumst hem wil
lhavetohavea
vari
abl
et orepresenteachnumberasiti
senteredandav ari
ablet
orepr
esentt
hesum ofthe
numbers.
Letusil
lustr
atetheusesofsomesi mplevari
able.

#i
ncl
ude<iostr
eam.
h>
i
ntmain(voi
d)
{
i
ntworkDays;

4

Written for

Institution
Course

Document information

Uploaded on
January 23, 2024
Number of pages
32
Written in
2022/2023
Type
Class notes
Professor(s)
Professor emmanuel
Contains
All classes

Subjects

$18.89
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
kayodeoluwarotimirotman

Get to know the seller

Seller avatar
kayodeoluwarotimirotman
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions