Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Two Dimensional Array in C: Int Twodimen

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Two Dimensional Array in C

The two-dimensional array can be defined as an array of arrays. The 2D array is


organized as matrices which can be represented as the collection of rows and
columns. However, 2D arrays are created to implement a relational database
lookalike data structure. It provides ease of holding the bulk of data at once which
can be passed to any number of functions wherever required.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.

1. data_type array_name[rows][columns];  

Consider the following example.

1. int twodimen[4][3];  

Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D
arrays. We will have to define at least the second dimension of the array. The two-
dimensional array can be declared and defined in the following way.

1. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

Two-dimensional array example in C


1. #include<stdio.h>  
2. int main(){      
3. int i=0,j=0;    
4. int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};     
5. //traversing 2D array    
6. for(i=0;i<4;i++){    
7.  for(j=0;j<3;j++){    
8.    printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);    
9.  }//end of j    
10. }//end of i    
11. return 0;  }    
Output
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6

C 2D array example: Storing elements in a matrix and printing it.


1. #include <stdio.h>    
2. void main ()    
3. {    
4.     int arr[3][3],i,j;     
5.     for (i=0;i<3;i++)    
6.     {    
7.         for (j=0;j<3;j++)    
8.         {    
9.             printf("Enter a[%d][%d]: ",i,j);                
10.             scanf("%d",&arr[i][j]);    
11.         }    
12.     }    
13.     printf("\n printing the elements ....\n");     
14.     for(i=0;i<3;i++)    
15.     {    
16.         printf("\n");    
17.         for (j=0;j<3;j++)    
18.         {    
19.             printf("%d\t",arr[i][j]);    
20.         }    
21.     }    
22. }    
Output
Enter a[0][0]: 56
Enter a[0][1]: 10
Enter a[0][2]: 30
Enter a[1][0]: 34
Enter a[1][1]: 21
Enter a[1][2]: 34

Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78

printing the elements ....


56 10 30
34 21 34
45 56 78

Passing Array to Function in C


As we know that the array_name contains the address of the first element. Here,
we must notice that we need to pass only the name of the array in the function
which is intended to accept an array. The array defined as the formal parameter
will automatically refer to the array specified by the array name defined as an
actual parameter.

Consider the following syntax to pass an array to the function.

1. functionname(arrayname);//passing array  
Methods to declare a function that receives an array as an argument

There are 3 ways to declare the function which is intended to receive an array as an
argument.

First way:
1. return_type function(type arrayname[])  

Declaring blank subscript notation [] is the widely used technique.

Second way:
1. return_type function(type arrayname[SIZE])  

Optionally, we can define size in subscript notation [].

Third way:
1. return_type function(type *arrayname)  

C language passing an array to function example

1. #include<stdio.h>  
2. int minarray(int arr[],int size){    
3. int min=arr[0];    
4. int i=0;    
5. for(i=1;i<size;i++){    
6. if(min>arr[i]){    
7. min=arr[i];    
8. }    
9. }//end of for    
10. return min;    
11. }//end of function    
12.     
13. int main(){      
14. int i=0,min=0;    
15. int numbers[]={4,5,7,3,8,9};//declaration of array    
16.   
17. min=minarray(numbers,6);//passing array with size    
18. printf("minimum number is %d \n",min);    
19. return 0;  }    
Output
minimum number is 3

C function to sort the array


1. #include<stdio.h>   
2. void Bubble_Sort(int[]);  
3. void main ()    
4. {    
5.     int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
6.     Bubble_Sort(arr);    
7. }    
8. void Bubble_Sort(int a[]) //array a[] points to arr.   
9. {  
10. int i, j,temp;     
11.     for(i = 0; i<10; i++)    
12.     {    
13.         for(j = i+1; j<10; j++)    
14.         {    
15.             if(a[j] < a[i])    
16.             {    
17.                 temp = a[i];    
18.                 a[i] = a[j];    
19.                 a[j] = temp;     
20.             }     
21.         }     
22.     }     
23.     printf("Printing Sorted Element List ...\n");    
24.     for(i = 0; i<10; i++)    
25.     {    
26.         printf("%d\n",a[i]);    
27.     }  
28. }  
Output
Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

You might also like