Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Arrays and Strings
  5. Declaration of two-dimensional array

Declaration of two-dimensional array

Declaration of two-dimensional array

data_type arr_name [num_of_rows][num_of_column];

Example

int c[2][3];

Initialization of two-dimensional array

Multidimensional arrays may be initialized by specifying bracketed values for each row.

int c[2][3]={{1,3,0}, {-1,5,9}};

Accessing Array elements in two-dimensional array

arr_name[r_index][c_index];

Example

c[0][1] ; // the value in 0th row and 1st column

Matrix operations

  • Matrix Addition
  • Matrix Subtraction
  • Matrix Multiplication

Program: Implementation of Matrix addition

#include<stdio.h>
int main()
{
	int a[10][10],b[10][10],i,j,r,c,res[10][10];
	printf("Enter row and column of matrix\n");
	scanf("%d%d",&r,&c);
	//loop to get array elements
	printf("Enter Matrix a : \n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	printf("Enter Matrix b : \n");
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			scanf("%d",&b[i][j]);
		}
	}
	//loop to add two matrix
	for(i=0;i<r;i++)
	{
		for(j=0;j<c;j++)
		{
			res[i][j] = a[i][j] + b[i][j];
		}
	}
	//loop to print result
	printf("Resultant Matrix res: \n");
	for(i=0;i<r;i++)
	{
		printf("\n");
		for(j=0;j<c;j++)
		{
			printf("%d\t",res[i][j]);
		}
	}

	return 0;
}

Output

Enter row and column of matrix
3 3
Enter Matrix a :
1 2 3 4 5 6 7 8 9
Enter Matrix b :
2 2 2 5 5 5 6 6 6
Resultant Matrix res:
3       4       5
9       10      11
13      14      15

Program: Implementation of Matrix Multiplication

#include<stdio.h>
int main()
{
	int a[10][10],b[10][10],i,j,k,r1,c1,r2,c2,res[10][10];
	printf("Enter row and column of matrix a\n");
	scanf("%d%d",&r1,&c1);
	printf("Enter row and column of matrix b\n");
	scanf("%d%d",&r2,&c2);
	if(c1 != r2)
	{
		printf("Matrix multiplication is not possible");
	}
	else
	{
		printf("Enter matrix a: \n ");
		for(i=0;i<r1;i++)
		{
			for(j=0;j<c1;j++)
			{
				scanf("%d",&a[i][j]);
			}
		}
		printf("Enter matrix b: \n ");
		for(i=0;i<r2;i++)
		{
			for(j=0;j<c2;j++)
			{
				scanf("%d",&b[i][j]);
			}
		}
		//loop to multiply
		for(i=0;i<r1;i++)
		{
			for(j=0;j<c2;j++)
			{
				res[i][j] = 0;
				for(k=0;k<c1;k++)
				{
					res[i][j] = res[i][j] + a[i][k] * b[k][j];
				}
			}
		}
		// loop to print result
		printf("Resultant matrix:  \n ");
		for(i=0;i<r1;i++)
		{
			printf("\n");
			for(j=0;j<c2;j++)
			{
				printf("%d\t",res[i][j]);
			}
		}
	}		
	return 0;
}

Output

Enter row and column of matrix a
2 3
Enter row and column of matrix b
3 2
Enter matrix a:
 1 1 1 1 1 1
Enter matrix b:
 2 2 2 2 2 2
Resultant matrix:
6       6
6       6

Program: Scaling of a matrix

/*  C Program for scalar multiplication of matrix  */
#include <stdio.h>
int main()
{
    int A[10][10],i,j,m,n;
    int row, col, num;

    printf("Enter no. of rows :: ");
    scanf("%d", &m);
    printf("\nEnter no. of cols :: ");
    scanf("%d",&n);
    printf("\nEnter values to the matrix :: \n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
                 printf("\nEnter a[%d][%d] value :: ",i,j);
                 scanf("%d", &A[i][j]);
        }
    }

    printf("\nThe given matrix is :: \n\n");

    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf("\t%d", A[i][j]);
        }
        printf("\n\n");
    }

    /* Reads number to perform scalar multiplication from user */
    printf("\nEnter any number to multiply with matrix :: ");
    scanf("%d", &num);

    /*
     * Performs scalar multiplication of matrix
     */
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            A[row][col] = num * A[row][col];
        }
    }

    /*
     * Prints the result of scalar multiplication of matrix
     */
    printf("\nScalar multiplication of matrix = \n\n");
    for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
                printf("\t%d", A[i][j]);
            }
            printf("\n\n");
        }

    return 0;
}

Output

Enter no. of rows :: 2
Enter no. of cols :: 3
Enter values to the matrix ::
Enter a[0][0] value :: 1
Enter a[0][1] value :: 2
Enter a[0][2] value :: 3
Enter a[1][0] value :: 4
Enter a[1][1] value :: 5
Enter a[1][2] value :: 6
The given matrix is ::

        1       2       3

        4       5       6
Enter any number to multiply with matrix :: 2
Scalar multiplication of matrix =

        2       4       6

        8       10      12

Program: Determinant of a matrix

#include <stdio.h>

int main()
{
	int arr1[10][10],i,j,n;
	int det=0;
	printf("\n\nCalculate the determinant of a 3 x 3 matrix :\n");
    printf("-------------------------------------------------\n");  
	printf("Input elements in the first matrix :\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
	        printf("element - [%d],[%d] : ",i,j);
	        scanf("%d",&arr1[i][j]);
        }
    }  
	printf("The matrix is :\n");
	for(i=0;i<3;i++)
	{
	   for(j=0;j<3 ;j++)
	   {
	     printf("%4d",arr1[i][j]);
	   }
	   printf("\n");
	}

    for(i=0;i<3;i++)
	{
      det = det + (arr1[0][i]*(arr1[1][(i+1)%3]*arr1[2][(i+2)%3] - arr1[1][(i+2)%3]*arr1[2][(i+1)%3]));
	}
	printf("\nThe Determinant of the matrix is: %d\n\n",det);
}

Output

Calculate the determinant of a 3 x 3 matrix :
-------------------------------------------------
Input elements in the first matrix :
element - [0],[0] : 2
element - [0],[1] : 4
element - [0],[2] : 5
element - [1],[0] : 2
element - [1],[1] : 1
element - [1],[2] : 3
element - [2],[0] : 4
element - [2],[1] : 1
element - [2],[2] : -1
The matrix is :
   2   4   5
   2   1   3
   4   1  -1

The Determinant of the matrix is: 38

Program: Transpose of a matrix

#include <stdio.h>
int main() 
{
  int a[10][10], transpose[10][10], r, c;
  printf("Enter rows and columns: ");
  scanf("%d %d", &r, &c);

  // asssigning elements to the matrix
  printf("\nEnter matrix elements:\n");
  for (int i = 0; i < r; i++)
  {
	for (int j = 0; j < c; j++) 
	{
		printf("Enter element a%d%d: ", i, j);
		scanf("%d", &a[i][j]);
	}
  }

  // printing the matrix a[][]
  printf("\nEntered matrix: \n");
  for (int i = 0; i < r; i++)
  {
	printf("\n");
	for (int j = 0; j < c; j++) 
	{
		printf("%d\t", a[i][j]);
	}
  }

  // computing the transpose
  for (int i = 0; i < r; i++)
  {
	for (int j = 0; j < c; j++) 
	{
		transpose[j][i] = a[i][j];
	}
  }

  // printing the transpose
  printf("\nTranspose of the matrix:\n");
  for (int i = 0; i < c; i++)
  {
	printf("\n");
	for (int j = 0; j < r; j++) 
	{
		printf("%d  ", transpose[i][j]);
	}
  }
  return 0;
}

Output

Enter rows and columns: 2
3

Enter matrix elements:
Enter element a00: 1
Enter element a01: 2
Enter element a02: 3
Enter element a10: 4
Enter element a11: 5
Enter element a12: 6

Entered matrix:

1       2       3
4       5       6
Transpose of the matrix:

1  4
2  5
3  6

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments