ENGG1002ComputerProgrammingand
Applica9ons
Dr.HaydenKwokHaySo
Week1
BasicCArrays
Declara9on:
int anarray[5];
datatype
ofeachelement
variable
name
size
Ini9aliza9on(op9onal)
int anarray[5] = {2,3,5,7,11};
Accessinganelement
Remember:C/C++arraysstartcoun9ngfrom0!
Validforindexesfrom0tosize1
anarray[0] = 31;
anarray[i+3] = anarray[4]+7;
Whathappenswhenyouaccessanarray[6]?
ArrayvsMemory
Themainmemoryofacomputercanbethoughtasahuge
arrayofmemoryloca9ons.
Eachbasicmemoryloca9onconsumes1BYTE.
E.g.The1GBofmainmemorycanbethoughtasanarraywith
1,073,741,824elements,eachelement=1BYTE.
InC/C++,anarrayisacon(nuousblockofmemory.
1,073,741,823
1,073,741,822
char vowels[5]={a,e,i,o,u};
u
o
i
e
a
1
0
1bytes
vowels
ArrayofIntegers
1
0
11
108
107
106
105
104
103
102
101
100
13
int primes[3]={11,13,17};
4bytes
primes
Q:Ifprimesisstoredatloca9on
100,thenhowcanyound
primes[1], primes[2]?
A:primes[1]storedatloca9on
100+4=104
primes[2] atloca9on
100+2*4=108
Elementkat100+k*4
Thesizeofanintegeris4bytes
InC,youcanusethefunc9on
sizeof(),e.g.
sizeof(int), sizeof(i);
ArrayAddressing
Ingeneral,givenyouknowthestar9ngaddress
(A)ofanarrayoftypeT,youcanndthekth
elementataddress
A+k*sizeof(T)
Tobeabletondtheloca9onofanarray
elementV[k],thecompileronlyneedstoknow
thetypeoftheelementandthestar(ng
address.
C/C++compilersdonotknowaboutthelength
ofanarray.
Itonlygeneratescodetocalculatetheaddressofan
elementbasedontheaboveequa9on
Basic2DArrays
Declara9on:
int a2Darray[5][7];
datatype
ofeachelement
variable
name
Sizesofthe2
dimensions
Ini9aliza9on(op9onal)
int a2Darray[2][3] = {{2,3,5},{7,11,13}};
Accessinganelement
a2Darray[0][2] = 31;
cout << a2Darray[i+3][j+2];
2DArrays
Howtostorea2Darray(matrix)inmemorythatisa
linear(1D)array?
Twomethods:
Rowmajorstoresa2Darray(matrix)rowbyrow,
star9ngfromtoptobolom.
Columnmajorstoresamatrixcolumnbycolumn,
star9ngfromlemtoright.
10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
C/C++storestheminrowmajor
Matlabstoresthemincolumnmajor
2DarraysinC/C++
136
132
128
124
120
116
112
108
104
100
4
0
10
9
8
7
6
5
4
3
2
1
V =
4bytes
10 11 12
Toaccessanelementina2DarrayV
V[0][1] =>2, V[2][1] =>10
Whatdoyouneedtondtheaddress?
V[0][1]@address(V)+1*sizeof(int)
V[2][1]@address(V)+2*4*sizeof(int)+
1*sizeof(int)
Weneedthreepiecesofinforma9on:
address(V)
Sizeoftheelement
Thelengthofarow
GerngMemoryforArrays
Sta9c:int primes[5] = {2,3,5,7,11};
Dynamic:
Whenyoudontknowthesizewhenyouwriteyourprogram
E.g.Toputallthecustomerordersinanarray
Usemalloc()
int num_orders;
int orders[];
orders = (int*) malloc(num_orders * sizeof(int));
C++hassomeextrasyntax
Dangerous,notrecommended!
int
int
cin
for
num_orders;
orders[num_orders];
>> num_orders;
(int i = 0; i < num_orders; i++)
orders[0] = . . .
ArraysasParameters
Toacceptan1Darrayasparameter:
int func(int a[], int length);
Notethatwehavenoideaofthelengthofa[].
Therefore,wemust,somehow,sendthelengthtothe
func9onaswell.
Onewayistopassthelengthaswell.
Anotherwayistouseastruct.
Toaccepta2Darrayasparameter:
int func(int b[][10], int length);
Notethatalldimensions,excepttherst,MUSTbe
dened.
Why?(Recallhowyoucangettheaddressofanelementin
amul9dimensionalarray.)
2DArrayfortheProject
Func9onsforreadingimagesfromyourle
systemwillbeprovided
Imagestoredasa2Darray
Memorywillbeallocateddynamicallyforyou
already.
Youneedtoimplementtheimageprocessing
rou9nesonthe2Darray.