Two Dimensional Array

The most simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in simple words, a list of one-dimensional arrays.

The declaration of a two-dimensional integer array of size x,y is done as follows:

type arrayName [ a ][ b ];

Where type can be any valid C data type and arrayName should be a valid C identifier. A twodimensional array can be thought as a table which will have 'a' number of rows and 'b' number of columns.

Initializing Two-Dimensional Arrays

We can initialize multidimensional arrays by specifying bracketed values for each row.

The nested braces, which indicate the intended row, are optional.An array with 3 rows and 4 columns is as follows:

int array[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements:

Any element in 2-dimensional array is accessed by using the row index and column index of the array.

For example:

int size = a[2][3];

NOTE: you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.

Sorting Techniques

1.Bubble Sort

Bubble sort algorithm begins by comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order .Also, if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element. Similarly, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped.

Bubble Sort Program in c

``` #include<stdio.h>

void main()

{

int a[100];

int i,num,step,temp;

printf("Enter the size of array: ");

scanf("%d", &num);

printf("Enter the unsorted array elements:\n");

for(i=0;i<num;i++)

{

scanf("%d",&a[i]);

}

for(step=0;step<num-1;++step)

for(i=0;i<num-step-1;++i)

{

if(a[i]>a[i+1])

{

temp=a[i];

a[i]=a[i+1];

a[i+1]=temp;

}
}

printf("Sorted array is: \n");

for(i=0;i<num;++i)

{

printf("%d  ", a[i]);

}
}
```

Output:
Enter the size of array: 5
Enter the unsorted array of elements:
4
2
1
7
5
Sorted array is: 1 2 4 5 7

Linear Search

It searches an array by comparing the value of a search element with each element in the array one-by-one.

Linear Search Program in c

``` #include <stdio.h>

void main()

{

int a[100];
int search, i, n;

printf("Enter the size of array: \n");

scanf("%d", &n);

printf("Enter the elements of the array:\n");

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

printf("Enter the search element\n");

scanf("%d", &search);

for (i = 0; i < n; i++)

{

if (a[i] == search)

{

printf("The element %d is present at %d location.\n", search, i+1);

break;

}

}

if (i == n)

}

```

OUTPUT:Enter the size of array:
5

Enter the elements of the array:
4
2
1
6
9

Enter the search element
1
The element 1 is present at 3 location.

Binary Search

Binary Search searches the key element in a sorted array by repeatedly dividing the array in half. Begin with an interval covering the whole array. If the value of the key element is less than the item in the middle element of the array, the array is narrowed to the lower half.Otherwise,the array is narrowed to the upper half. This is repeatedly done until the key element is found.

``` #include <stdio.h>

void main()

{

int i,n,lower,upper,key;

int a[10],c=0,mid;

printf("Enter the size of the array: ");

scanf("%d", &n);

printf("Enter the elements of the array in ascending order: ");

for(i=0; i<n; i++)

{

scanf("%d", &a[i]);

}

printf("Enter the search element: ");

scanf("%d", &key);

lower=0,upper=n-1;

while(lower<=upper)

{

mid=(lower+upper)/2;

if(key==a[mid])
{

c=1;

break;

}
else if(key<a[mid])

{
upper=mid-1;

}

else

lower=mid+1;

}

if(c==0)

else

printf("The number is found.");

}
```

OUTPUT:
Enter the size of the array: 5
Enter the elements in ascending order: 4
6
8
9
10
Enter the number to be search:
8
The number is found.

C programs using One-dimensional and Two-dimensional arrays:

C program for one-dimensional array

``` #include<stdio.h>

void main()

{

int i;

int array[5] = {5,10,15,20,25};

for (i=0;i<5;i++)

{

printf("Value of array[%d] is %d\n", i, array[i]);

}

}

```

OUTPUT:
value of array[0] is 5
value of array[1] is 10
value of array[2] is 15
value of array[3] is 20
value of array[4] is 25

C Program to find greatest number from one dimensional array

``` #include<stdio.h>

void main()

{

int a[20],num,i;
int max=0;

printf("\n Enter the size of the array : \n");

scanf("%d",&num);

printf("Enter the elements of the array: \n");

for(i=0;i<num;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<num;i++)

{

if(max<a[i])

max=a[i];

}

printf("\n Greatest element is : %d",max);

}

```

OUTPUT:
Enter the size of the array: 5
Enter the elements of the array:
1
2
3
4
5
Greatest element is : 5

C program for two dimensional array

``` #include<stdio.h>

void main()

{

int i,j;

int a[2][2] = {10,20,30,40};

for (i=0;i<2;i++)

{

for (j=0;j<2;j++)

{

printf("Array element [%d] [%d] is : %d\n",i,j,a[i][j]);

}

}

}

```

OUTPUT:
Array element [0] [0] is : 10
Array element [0] [1] is : 20
Array element [1] [0] is : 30
Array element [1] [1] is : 40

<< Back Next >>