4.1 Array
4.1 Array
Arrays
1. Rules for arrays
2. Multiple subscripts in arrays
3. Multi-dimensional arrays
4. For loop with arrays
5. Simple programs
ARRAY
An array is a group of related data items that share a common
name.
An array is defined as the collection of similar type of data
items stored at contiguous memory locations.
Arrays are the derived data type in C programming language
which can store the primitive type of data such as int, char,
double, float, etc.
It also has the capability to store the collection of derived data
types, such as pointers, structure, etc.
The array is the simplest data structure where each data
element can be randomly accessed by using its index number.
PROPERTIES OF ARRAY
The array contains the following properties.
Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes.
Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
Elements of the array can be randomly accessed since we
can calculate the address of each element of the array
with the given base address and the size of the data
element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the
elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a
few lines of code only.
4) Random Access: We can access any element randomly using
the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.
Declaration of C Array
We can declare an array in the c language in the following
way.
Syntax:
data_type array_name[array_size];
Example for declare the array.
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is
the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of
each element. We can initialize each element of the array by
using the index. Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
RULES FOR ARRAY DECLARATION
The data type can be any valid C data types including structure
and union.
The array name has to follow the rule of variable and the size
of array has to be a positive constant integer.
We can access array elements via indexes array_name[index].
Indexes of array starts from 0 not 1 so the highest elements of
an array is array_name[size-1].
C array example
#include<stdio.h>
void main()
Output
{
80
int i=0;
60
int marks[5];//declaration of array
70
marks[0]=80;//initialization of array
85
marks[1]=60; 75
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
getch();
}
C Array: Declaration with Initialization
We can initialize the c array at the time of
declaration. Let's see the code.
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the
size. So it may also be written as the following code.
int marks[]={20,30,40,50,60};
Example
#include<stdio.h>
void main(){
int i=0;
int marks[5]={20,30,40,50,60};
//declaration and initialization of array
//traversal of array Output
for(i=0;i<5;i++) 20
{ 30
printf("%d \n",marks[i]); 40
} 50
60
getch();
}
MULTIPLE SUBSCRIPTS IN ARRAYS
Arrays in C can have multiple subscripts.
• A common use of multiple-subscripted arrays (also called
multidimensional arrays) is to represent tables of values consisting of
information arranged in rows and columns.
• To identify a particular table element, we must specify two subscripts:
The first (by convention) identifies the element’s row and the second
(by convention) identifies the element’s column.
• Tables or arrays that require two subscripts to identify a particular
element are called double- subscripted arrays.
Multiple-subscripted arrays can have more than two subscripts.
• The array contains three rows and four columns, so it’s said to be a 3-
by-4 array.
• In general, an array with m rows and n columns is called an
m-by-n array
Arrays with 2 subscripts are often used to represent tables of
values.
To identify a particular element in the table, we specify:
its row (by convention, the 1st subscript), and
its column (by convention, the 2nd subscript). // Draw a
picture
Arrays in Java can have more than 2 subscripts.
In Java, multiple-subscripted arrays are implemented as arrays
of arrays:
A 2-dimensional array of int is implemented as an array of
an array of int.
There really are only single-subscripted arrays, but an
array's elements can be anything, including arrays.
This simplifies the language.
A good illustration of this uniform treatment is the inner
loop of the printArray method: Clearly, element a[i] is itself
an array; it has a length attribute.
MULTI-DIMENSIONAL ARRAYS
C programming language allows multidimensional arrays. Here is
the general form of a multidimensional array declaration −
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional
integer array −
int threedim[5][10][4];
Similarly, you can declare a three-dimensional (3d) array. For
example,
float y[2][4][3];
Here, the array y can hold 24 elements
Two-dimensional Arrays
The simplest form of multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a
list of one-dimensional arrays.
To declare a two-dimensional integer array of size [x][y], you
would write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will
be a valid C identifier.
A two-dimensional array can be considered as a table which will
have x number of rows and y number of columns.
A two-dimensional array a, which contains three rows and four
columns can be shown as follows −
int main ()
{
int index, my_array[ARRAY_SIZE];
return 0;
}
Output:
my_array[0] = 0
my_array[1] = 0
my_array[2] = 0
my_array[3] = 0
my_array[4] = 0
my_array[5] = 0
my_array[6] = 0
my_array[7] = 0
my_array[8] = 0
my_array[9] = 0
You can use similar code to fill the array with different values.
The following code example is nearly identical to the one
above, but the line my_array[index] = index; fills each element
of the array with its own index:
Example:
#include <stdio.h>
#define ARRAY_SIZE 5
Output:
int main ()
my_array[0] = 0
{
int index, my_array[ARRAY_SIZE];
my_array[1] = 1
my_array[2] = 2
for (index = 0; index < ARRAY_SIZE; index++) my_array[3] = 3
{ my_array[4] = 4
my_array[index] = index;
printf ("my_array[%d] = %d\n", index, my_array[index]);
}
printf("\n");
return 0;
}