You are on page 1of 5

Chapter 4

Arrays
Arrays allow you to store data items of a similar type. You can picture an array in the computer's memory as a row of consecutive spaces, each of which can store a data item, known as an ELEMENT.

Declaring Arrays
To declare an array you need to specify its data type, its name and, in most cases, its si e. Make sure the array has a valid name. !ere's an e"ample#
int arrayOfInts[5];

This reserves memory for an array that is to hold five inte$er values. The num%er of elements should %e enclosed in the s&uare %rackets. !owever, if you don't specify the num%er of elements, when you initiali e the array, the computer will work out how many elements it holds.

Initializing Arrays
You can assi$n values to the array in several ways. 'or e"ample, if you want an array to hold the num%ers ( throu$h to (), you could do either of these#
int arrayOfInts[10] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }; arrayOfInts[0] = 1; arrayOfInts[1] = 2; arrayOfInts[2] = 3; array-f.nts/01 2 34 array-f.nts/31 2 54 array-f.nts/51 2 64 array-f.nts/61 2 74 array-f.nts/71 2 84 array-f.nts/81 2 94 array-f.nts/91 2 ()4

*+ Assume that the array has %een declared, +*

int array-f.nts/1 2 :(,;,0,3,5, 6,7,8,9,()<4 *+ an unsi ed array +* int array-f.nts/()14 int i4 for=i2) 4 i>() 4 i??@ array-f.nts/i1 2 i ? (4
Aopyri$ht Lo$ic -ption Bvt. Ltd.

You may have noticed that the first element of an array is inde"ed ) rather than (. This is a simple yet important idea you need to take into account. . mean, once you've initiali ed () or so arrays it %ecomes a lesser pro%lem,

Printing Out Arrays


-ne of the commonest ways to print out the contents of the array is to use a for loop like this#
#inc !"# $st"io%&' int (ain)* { int anot&#rInt+rray[5] = {1,2,3,4,5}; int i; for)i=0 ; i$5 ; i,,* -rintf).anot&#rInt+rray[/"] &as a 0a !# of /"1n., i, anot&#rInt+rray[i]*; r#t!rn 0; }

Enterin$ arrays elements can also %e achieved#


#inc !"# $st"io%&' int (ain)* { int anot&#rInt+rray[5]; int i; -rintf).2nt#r 5 int#3#rs on# 4y on#, -r#ssin3 r#t!rn aft#r #ac& on#51n.*; for)i=0 ; i$5 ; i,,* scanf)./"., 6anot&#rInt+rray[i]*; for)i=0 ; i$5 ; i,,* -rintf).anot&#rInt+rray[/"] &as a 0a !# of /"1n., i, anot&#rInt+rray[i]*; } r#t!rn 0;

Character Arrays
Co far included are the e"amples of arrays of inte$ers. You can use arrays for f oat s and "o!4 # s as well as c&ar s. Each element of the array can hold one character. Dut if you end the array with the NELL A!AFAATEF, denoted %y 10 =that is, %ackslash and ero@, you'll have what is known as a CTF.NG A-NCTANT. The null character marks the end of a strin$ H useful for functions like -rintf. Time for an e"ample.....
#inc !"# $st"io%&'
Aopyri$ht Lo$ic -ption Bvt. Ltd.

int (ain)* { c&ar c&ar+rray[8] = {787,7r7,7i7,7#7,7n7,7"7,7s7,7107}; int i; for)i=0 ; i$8 ; i,,* -rintf).c&ar+rray[/"] &as a 0a !# of /c1n., i, c&ar+rray[i]*; *+ Alternative way +* printf=IMy favourite comedy is JsKnI, charArray@4 return )4 <

Notice each of the characters with sin$le &uote marks are enclosed H dou%le &uote marks are reserved for strin$s. Also used are the character and strin$ format specifiers =/c and /s respectively@.

Multidimensional Arrays
Co far, e"amples are which contain one dimensional arrays. Dut sometimes, usin$ multidimensional arrays is more convenient, like when usin$ coordinates for e"ample. This mi$ht sound silly, %ut focus is on ;L arrays for now. You already know how to declare a (L array, %ut what a%out ;LM Cimple. Take this e"ample#
int array29[3][5];

This tells the computer to reserve enou$h memory space for an array with (5, that is, 0 " 5 elements. -ne way to picture these (5 elements is an array with 0 rows and 5 columns. Dut there are times when you don't know the si e, so you'd want to use an unsi ed array. !owever, you must specify every dimension si e e"cept the left one. Like this#
int array29[][5];

Initialization
Methods used are similar to those of the (L arrays#
#inc !"# $st"io%&' int (ain)*
Aopyri$ht Lo$ic -ption Bvt. Ltd.

{ int first[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11}; int s#con"[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11}; *+ a clearer definition than the first +* int third/1/51 2 :),(,;,0,3<4 int fourth/1/61 2 :),(,;,0,3,5,6,7,8,9,(),((<4 int i,N4 int fifth/51/314 for=i2) 4 i>5 4 i??@ : for=N2) 4 N>3 4 N??@ fifth/i1/N1 2 i + 3 ? N4 < int si"th/;1/614 for=i2) 4 i>6 4 i??@ : printf=IEnter 6 inte$ers separated %y spaces# I@4 for=N2) 4 N>; 4 N??@ scanf=IJdI , Osi"th/i1/N1@ printf=IKnI@4 < int seventh/;1/014 seventh/)1/)1 2 )4 seventh/)1/(1 2 (4 seventh/)1/;1 2 ;4 seventh/(1/)1 2 04 seventh/(1/(1 2 34 seventh/(1/;1 2 54 return )4 <

'or loops are dead handy. And not Nust for initiali ation H they are commonly used for printin$ out arrays. Pe tend to iterate the rows in the first loop, then the columns. Phichever way round you chose, keep it consistent,

Array Sizes
.f you ever wanted to find out how much memory your arrays =(L or multidimensional@, you can use the si:#of operator like this#
Aopyri$ht Lo$ic -ption Bvt. Ltd.

#inc !"# $st"io%&' int (ain)* { c&ar array;&ar[] = {7+7,7r7,7r7,7a7,7y7,7107}; int arrayInt[5] = {1,2,4,8,16}; f oat array8 oat[3] = { 1%24 , 2 , 4%68756 }; "o!4 # array9o!4 #[2]; array9o!4 #[0] = 23%23456532; array9o!4 #[1] = 2%3422267; int arrayInt29[][4] = {1,6,3,7, 6,3,8,9, 2,5,2,3}; -rintf).<&# si:# of array;&ar[] is /"1n., si:#of)array;&ar**; -rintf).<&# si:# of arrayInt[] is /"1n., si:#of)arrayInt**; -rintf).<&# si:# of array8 oat[] is /"1n., si:#of)array8 oat**; *+ Alternative way +* printf=IThe si e of arrayLou%le/1 is JdKnI, si eof=dou%le@ + ;@4 printf=IThe si e of array.nt;L/1 is JdKnI, si eof=array.nt;L@@4 return )4 <

Aopyri$ht Lo$ic -ption Bvt. Ltd.

You might also like