Arrays
The values are initially stored in a primary data type variable. The downside is that a variable can only hold one value at a time. As a result, if you want to store several values, you’ll need to declare multiple variables. Two or three values can be stored or accessed using an extra variable. Consider storing the register numbers of students at a college in multiple variables with different names. The program becomes illegible and complex as a result. To make things easier, you can use an array by declaring only one variable of type array.
Consider the data type intreg[1000], which indicates that the variable’reg’ can store up to 1000 values. Element by element, the values can be read. As a result, the choice is to store a big set of data of the same type array. Array is a derived data type. It’s especially useful when dealing with big amounts of data of the same or different categories.
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.
Consider the following example:
Int x,y,z; X=20;y=30;z=40;
The above statements can be rewritten using array form
int x[3]; x[0]=20; x[1]=30; x[2]=40;
When the number of variable is less the statement may not look confusing or difficult. Now consider the following example.
int y[100];
The array y can store up to 100 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 100 variables or array y.
Arrays are classified as follows:
1. One dimensional array
2. Two dimensional array
3. Multidimensional array
4. Dynamic array
1. One Dimensional Array
It is similar to matrix with only one column but multiple rows. This type of array is useful when working with only set of data at a time.
Declaring One Dimensional Array
It is similar to declaring any other primary data type except, the number element must specified or an array in bracket.
int z[5]; float s[10]; double w[6]; 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.
Assigning One Dimensional Array:
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.
The array element start with 0 NOT 1
Initializing One Dimensional Array
Method 1;
int z[5] = {11,22,33,44,55}
char t[6] = {‘k’,’i’,’n’,’g’}
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:
int z[] = {11,22,33};
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};
Arrays can be read/written by any of the looping statements.
Assigning value to array by program:
int x[5],i;
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
Reading value to array by program:
int x[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.
2. Two Dimensional Arrays
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.
Declaring Two Dimensional Array:
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.
Assigning values to Two Dimensional Arrays
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[1][0]=44; re[1][1]=2; re[1][2]=4; re[2][0]=0;re[2][1]=2;re[2][2]=4;
Initializing Two Dimensional Arrays
Method 1;
Consider the following example for the matrix to be initialized:
int re[3][3] = {11,22,33,44,0,55,0,2,3};
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:
The method 1 can be written as follows to get the same result.
int re[3][3] = {{11,22,33},{44,0,55},{0,2,4}};
Here each braces {} represents each row, starting from the first row. The method two can be written to look like matrix without any change in the syntax;
intr e[3][3]={{11,22,33},{44,0,55},{0,2,4}};
Method 3:
The method 1 can be written as follows to get the same result.
int re[][3] = {{11,22,33},{44,0,55},{0,2,4}};
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:
int re[3][4] = {{33},{0,55,0},{0}}
In this method the value in the array will be as follows
re[0][0]=33; re[0][1]=0; re[0][2]=0;
re[1][0]=0; re[1][1]=55; re[1][2]=0;
re[2][0]=0; re[2][1]=0; re[2][2]=0;
Method 5:
int rc[3][3]={{0},{0},{0}};
int rc[3][3]= {0,0,0};
In this method the value in the entire array element will be zero.
re[0][0]=0; re[0][1]=0; re[0][2]=0;
re[1][0]=0; re[1][1]=0; re[1][2]=0;
re[2][0]=0; re[2][1]=0; re[2][2]=0;
Assigning value to array elements by program:
int re[3][3],i,j;
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
scanf(“%d”,&re[i][j]);
}
}
Reading value from array elements by program:
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:
1. It reduces the programming length significantly.
2. It has better control over program for modification.
3. Coding is simple, efficient, and clear.
4. It can handle large set of data with simple declaration.
Rules of Array:
1. It does not check the boundary.
2. Processing time will increase when working with large data because of increased memory.
3. The array element start with zero not 1.
4. Character array size must be one element greater than data for NULL value.
5. One variable for control structure is required to assign and read value to one dimensional array.
6. Two variable for control structures are required to assign and read value to two dimensional arrays.
7. No two arrays can have the same name but arrays and ordinary variable can be assigned the same name.


