Week 9-10 Arrays
Week 9-10 Arrays
In the early the values are stored in a variable of primary data types. The disadvantage
is only one value can be stored in a variable at a time. Therefore to store more than one
value, then more than one variable have to be declared. An additional variable can be
declared to store or read two or three values. Imagine storing the register numbers of
students of a college in several variable names to store the register number of the
students. Then the program becomes unreadable and complex. To simplify that array
can be used by just declaring one variables of type array.
Consider the following example of data type intreg[1000], this means the variable name
‘reg’ can store up to 1000 values. The values can be read, element by element. Therefore
to store a large set of data of same type array is the choice. Array is a derived data type.
It is very effective when working with large number of data of same data type or
different data types.
Definition:
Array is a collection of same data type elements under the same variable identifier
referenced by index number. Arrays allow you to store group of data of a single type.
Intx,y,z;
X=10;y=20;z=30;
int x[3];
When the number of variable is less the statement may not look confusing or difficult.
Now consider the ollowing example.
int y[60];
The array y can store up to 60 values in its elements. The structure and location value in
the array is similar to matrix. Now it is clear that it is not good practice to declare 60
variables or array y.
Arrays are classified as follows:
It is similar to matrix with only one column but multiple rows. This type o array is
useful when working with only set of data at a time.
It is similar to declaring any other primary data type except, the number element must
specified or an array in bracket.
char t[4];
The arrays declared as shown. The numbers inside the bracket is the number of
elements for that variable. There ore z can store 5 values, s 10 values, w 6 values. The
only difference for character array t; is that the last element in the array must be allowed
for NULL value, therefore remaining 3 elements of t can be any character including
NULL.
int z[5]
z[0]=10;
z[1]=20;
z[2]=30;
z[3]=40;
z[4]=50;
The values are assigned elements by element in the array.
float z[10];
z[2]=11.11;
z[5]=66.66;
z[8]=45.90;
In the above the element 2, 5, and 8 are assigned values but the other elements are not
assigned any values. In such case the remaining element will be assigned zero.
Therefore for numeric array, When the value is assigned to fewer elements the
remaining elements will be assigned zero by the compiler.
Char t[4];
t[0]=’a’;
t[1]=’b’;
t[2]=’c’;
t[3]=’d’;
The last element t[3] is assigned ‘d’, as we discussed earlier the element or character
array must be provided for NULL value. Here ‘d’ is assigned to the element the
compiler will not give error during compilation but there will be error in the program
during run time.
Char t[4];
t[0]=’a’;
t[1]=’b’;
In this example the 3rd element and 4th element will be NULL. Therefore when the value
is assigned to fewer elements the element will be assigned NULL by the compiler or
character array whereas or numeric or numeric array zero will be assigned.
All the array elements are assigned value. The last element or the character arrays must
be provided for NULL character.
Method 2:
intz[5]={11,22,33};
char t[6]={‘a’,’b’.’p’};
Only first three elements are assigned values the remaining elements will be assigned
zero or integer data type. The remaining element or character array will be assigned
NULL.
Method 3:
int z[5]={0};
char t[6]=”apple”;
All the array elements are assigned zero. The character array can also be initialized as
above.
Method 4:
char t[]=’apple’;
Since array size is not defined. The array must be initialized, therefore the size of the
array will be same the number of values assigned. Therefore the integer array size for
this example in three since three value are provided in the initialization. The array size
for character array t is 6, five for the character and one for the NULL terminator.
To assign zero to a specific element the location of the element must be specified. Thus
the array values are assigned sequentially. To assign value element by element then
assigning method must be adopted.
z[5]={0,22,33,44,55};
z[5]={0,22,33,0,55};
intx[5],i;
for(i=0;i<5;i++)
scanf(“%d”,&x[i]);
intx[5],i;
for(i=0;i<5;i++)
printf(“%d”,x[i]);
Remember, the initial value or array must start with zero and the condition part must
be (<) less than sign, and the value must be the size of array, and the increment must by
one, to assign value element by element.
It is similar to matrix with multiple rows and columns. This types of array is useful
when working with more than one set o data at a time.
Syntax:
Data_typevar_name[r_s][e_s];
Eg: intmarks[10][3];
The array has 10 rows and 3 columns. The values in the array are located by row and
column, just like in matrix. Similar to one dimensional array the row and column
elements start from zero not one.
C0 C1 C2
R0 11 22 33
R1 44 0 55
R3 0 2 4
The above example has 3 rows and 3 columns. The value of the matrix can be assigned
to the array as follows.
int re[3][3];
re[0][0]=11;re[0][1]=22;re[0][2]=33;
re[2][0]=0;re[2][1]=2;re[2][2]=4;
Methods 1;
In this example the first row will be 11, 22, and 33; the second row 44, 0, and 55; and the
third row 0, 2, and 4. Therefore it is clear that the values will be assigned in row wise.
Method 2:
intre[3][3]={{11,22,33},{44,0,55},{0,2,4}};
Method 3:
In this example the row will be set to 3 since row size is not specified the number of
rows will be decided by the initial values.
Method 4:
Method 5:
intrc[3][3]={{0},{0},{0}};
intrc[3][3]= {0,0,0};
In this method the value in the entire array element will be zero.
intre[3][3],i,j;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
scanf(“%d”,&re[i][j]);
int re[3][3],i,j;
for(i=0;i<3;++i)
for(j=0;j<3;++j)
printf(“%d”,re[i][j])
Remember for both row and column, the initial value or array must start with zero and
the condition part must be less than sign(<) and value must be the size of array, and the
increment must by one, to assign value element by element.
Advantages of Array: