-->

Array

Array
C Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.
Array are classified into
Single dimensional & Multi dimensional array.
Single dimensional array
Single or One Dimensional array is used to represent and store data in a linear form.
Syntax : datatype array_name [ array_size ] ;
For example, take an integer array 'n'.
int n[6];
n[ ] is used to denote an array 'n'.
So, n[6] means that 'n' is an array of 6 integers. Here, 6 is the size of the array i.e. there are 6 elements of the array 'n'.
Giving array size i.e. 6 is necessary because compiler needs to allocate space to that many integers. Compiler determines the size of array by calculating the number of elements of an array.
Here 'int n[6]' will allocate space to 6 integers.
We can also declare an array by another method.
int n[ ] = { 2,3,15,8,48,13 };
In this case, we are declaring and assigning values to the array at the same time. Here, no need to specify the array size because compiler gets it from { 2,3,15,8,48,13 }.
Pictorial view of this:
  element 2 3 15 8 48 13    index 0 1 2 3 4 5
0,1,2,3,4 and 5 are indices. It is like they are identity of 6 different elements of an array. Index starts from 0. So, the first element has a index of 0.
Array of index starts with 0
Here, n[0] is 2 n[1] is 3 n[2] is 15 n[3] is 8 n[4] is 48 n[5] is 13
It's possible to initialize an array during declaration. For example,
int mark[5] = {19, 10, 8, 17, 9};
Another method to initialize array during declaration:
int mark[] = {19, 10, 8, 17, 9};

Here,
mark[0] is equal to 19 mark[1] is equal to 10
mark[2] is equal to 8 mark[3] is equal to 17 mark[4] is equal to 9
#include<stdio.h>int main(){ int i;  int arr[5] = {10,20,30,40,50}; // declaring and Initializing array in C //To initialize all array elements to 0, use int arr[5]={0};  /* Above array can be initialized as below also   arr[0] = 10;  arr[1] = 20;      arr[2] = 30;   arr[3] = 40; arr[4] = 50; */  for (i=0;i<5;i++) {   // Accessing each variableprintf("value of arr[%d] is %d \n", i, arr[i]);  }}
Output
value of arr[0] is 10 value of arr[1] is 20
value of arr[2] is 30 value of arr[3] is 40
value of arr[4] is 50
Multi-dimensional array
Array having more than one subscript variable is called Multi-Dimensional array.Multi Dimensional Array is also called as Matrix.
These consist of rows and columns.
Syntax : datatype array_name [ row-size ] [Col-size];
Example
float x[3][4];
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as table with 3 row and each row has 4 column.






Initialization of a two dimensional array
// Different ways to initialize two dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
int a[3][3] = { 1,2,3 5,6,7 8,9,0 };
int a[2][3] = { 1, 2, 3, 4, 5, 6 };
Here, value of a[0][0] is 1, a[0][1] is 2, a[0][2] is 3, a[1][0] is 4, a[1][1] is 5 and a[1][2] is 6.
int a[2][2] = { 1, 2, 3, 4 }; /* valid */ int a[ ][2] = { 1, 2, 3, 4 }; /* valid */ int a[2][ ] = { 1, 2, 3, 4 }; /* invalid */ int a[ ][ ] = { 1, 2, 3, 4 }; /* invalid */
#include<stdio.h>int main(){  int i,j; // declaring and Initializing array  int arr[2][2] = {10,20,30,40};  /* Above array can be initialized as below also     arr[0][0] = 10; // Initializing array     arr[0][1] = 20;      arr[1][0] = 30;      arr[1][1] = 40; */  for (i=0;i<2;i++)  {     for (j=0;j<2;j++)     {        // Accessing variables        printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);     }  }

1 comment:

Post a Comment

Basic structure of c program