What is array in c-programming language?
![]() |
Array in c |
An array is a collection of one or more values of same data-type. Each values is called an element of the array. The elements of array share the same variable name, but each element has it's own unique index number( also known as a subscript ).
An array can be of any type, for example : (int, float, char, double, etc..).
If an array is of type int then it's element must be of type int only.
Arrays can be single or multi-dimensional. The number of subscript or index determines the dimensions of array.
Row Major Order :
In row major order, the element of a particular row are stored at adjacent memory locations.
The first element of the array arr[0][0] is stored at the first location followed by the arr[0][1] and so on.
After the first row elements, the next row's are stored next.
Column Major Order :
In the column major order, The elements of a column are stored adjacent to each other in the memory, the first element of the array arr[0][0] is stored at the first location following by the arr[0][1] and so on.
Multi-Dimensional Array :
C-programming language allows multi-dimensional arrays.
The general form of multi-dimensional array is :
data_type array_name[size_1][size_2]...[size_n];
For example :
int arr[2][2][2];
This declaration will creates a three dimensional array.
The simplest form of multi-dimensional array is two dimensional array, To declare a two dimensional array of int data type with the size [m][n] we can write :
int arr[m][n];
Here, [m][n] belongs to positive integer. m is row number and n is column number.
Example :
A two dimensional array with 3 row's and 3 column's.
Row/Column |
Col-0 |
Col-1 |
Col-2 |
---|---|---|---|
row-0 |
arr[0][0] |
arr[0][[1] |
arr[0][2] |
row-1 |
arr[1][0] |
arr[1][1] |
arr[1][2] |
row-2 |
arr[2][0] |
arr[2][1] |
arr[2][2] |
Example with program :
#include<stdio.h>
int main()
{
int i,j;
int arr[2][2];
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("Enter element arr[%d][%d] :",i,j);
scanf("%d",&arr[i][j]);
}
}
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d",arr[i][j]);
if(j == 2){
printf("\n");
}
}
}
return 0;
}
0 Comments
Post a Comment