e-studynight

Read to lead

Menu
  • Home
  • Privacy Policy
  • Disclaimer
  • Terms and Conditions
  • About us
  • Contact US
    • Contact US
  • Courses
    • Management
  • Biology
  • Sitemap
  • Tech
    • C Programming
  • Blog
Menu
Array-2D

Array-2D

Posted on August 9, 2020August 9, 2020 by estudynight

We’ll discuss two-dimensional array. Array-2D, also known as an array of arrays, deals with the matrix related program. Let us begin with a simple example of initializing and displaying a matrix.

Initialization of Array-2D

We can initialize 2-D array using both run and compile time initialization.

Run time initialization

Let us try the problem mentioned above using run-time initialization.

#include<stdio.h>
 int main()
{
    /*  array declaration-2D*/
    int a[10][10];
 int i, j,m,n;
    printf("enter row and column size:\n");
    scanf("%d%d",&m,&n);
    printf("enter %d*%d order matrix a[][]:\n",m,n);
    for(i=0; i<m; i++) 
    {
       for(j=0;j<n;j++)
        {
          scanf("%d", &a[i][j]);
       }
    }
 printf("%d*%d order matrix a[][] is\n:", m, n);
    for(i=0; i<m; i++) 
    {
       for(j=0;j<n;j++)
        {
          printf("%d\t",a[i][j]);
      }      printf("\n"); 
  }
 return 0;
 }
Array-2D

Few points are to be remembered to initialize an array.

  • int a[5][ ]; this type of array declaration is invalid as the second dimension i.e. no. of column must be identified.
  • int a[ ][ ] ; this is also invalid. At least one dimension i.e.column must be declared.
  • int a[3][3]; this is valid declaration.
  • int a[ ][ 3]; this is also a valid declaration.

Compile-time Initialization

#include<stdio.h>
int main()
 {
   int a[3][3]={ {4, 2, 1}, {3, 5, 9}, {6, 8, 7} };
   int i,j;
 for(i=0; i<3; i++) 
    {
       for(j=0;j<3;j++)
        {
          scanf("%d",&a[i][j]);
       }
    }
 printf("matrix is:\n");
    for(i=0; i<3; i++) 
    {
       for(j=0;j<3;j++)
        {
          printf("%d\t",a[i][j]);
     }   
   printf("\n");  
 }
 return 0;
 }

We’ll try few more examples regarding matrix addition and subtraction.

Program to read two m*n order matrix and perform matrix addition and matrix subtraction.
#include<stdio.h>
 int main()
 {
     int a[10][10],b[10][10],i,j,m,n,c[10][10];
 printf("enter the row and column size of matrix :\n"); scanf("%d%d",&m,&n);
 printf("enter %d*%d order matrix a[i][j]:\n",m,n);
 for(i=0;i<=m-1;i++)
 {
     for (j=0;j<=n-1;j++)
     {
         scanf("%d",&a[i][j]);
     }
 }
 printf("the matrix a[] is:\n");
 for(i=0;i<=m-1;i++)
 {
     for (j=0;j<=n-1;j++)
     {
         printf("%d\t",a[i][j]);
     }
     printf("\n");
 }
 printf("enter %d*%d order matrix b[][]:\n",m,n);
 for(i=0;i<=m-1;i++)
 {
     for (j=0;j<=n-1;j++)
     { 
        scanf("%d",&b[i][j]);
     }
 }
 printf("the matrix b[] is:\n");
 for(i=0;i<=m-1;i++)
 {
     for (j=0;j<=n-1;j++)
     {
         printf("%d\t",b[i][j]);
     } 
    printf("\n");
 }
 printf("the resultant matrix after addition is:\n");
     for(i=0;i<=m-1;i++)
 {
     for (j=0;j<=n-1;j++)
     {
      c[i][j]=a[i][j]+b[i][j];
      printf("%d\t",c[i][j]);
     } 
    printf("\n");
 }
 printf("\nthe resultant matrix after subtraction is:\n");
     for(i=0;i<=m-1;i++)
 { 
    for (j=0;j<=n-1;j++)
     {
      c[i][j]=a[i][j]-b[i][j];
      printf("%d\t",c[i][j]);
     } 
    printf("\n");
 }
 return 0;
 }
Array-2D

Program to find the transpose of matrix.

 #include<stdio.h>
int main()
 {
   int a[3][3]={ {4, 2, 1}, {3, 5, 9}, {6, 8, 7} };
   int i,j;
 for(i=0; i<3; i++) 
    {
       for(j=0;j<3;j++)
        {
          scanf("%d",&a[i][j]);
       }
    }
 printf("matrix is:\n");
    for(i=0; i<3; i++) 
    {
       for(j=0;j<3;j++)
        {
          printf("%d\t",a[i][j]);
     }   
   printf("\n");  
 }
printf("the transpose of matrix is:\n");
     for(i=0; i<3; i++) 
    {
       for(j=0;j<3;j++)
        {
          printf("%d\t",a[j][i]);
        } 
     printf("\n");
   }
 return 0;
 } 

Program to perform matrix multiplication.

#include<stdio.h>
 int main()
 {
     int a[10][10],b[10][10],c[10][10],r1,r2,c1,c2,i,j,k;
     printf("enter the row an column size of matrix a[10][10]:\n");
     scanf("%d%d",&r1,&c1);
     printf("enter the row an column size of matrix b[10][10]:\n");
     scanf("%d%d",&r2,&c2);
     if(c1!=r2)
     printf("matrix multiplication is not possible");
     else
     {
 printf("enter %d*%d order matrix a[10][10]:\n",r1,c1);
 for(i=0;i<r1;i++)
 {
     for(j=0;j<c1;j++)
     {
         scanf("%d",&a[i][j]);
     }
 }
 printf("the first matrix is:\n");
     for(i=0;i<r1;i++)
 {
     for(j=0;j<c1;j++)
     {
         printf("%d\t",a[i][j]);
     }
     printf("\n");
 }
 printf("enter %d*%d order matrix b[10][10]:\n",r2,c2);
 for(i=0;i<r2;i++)
 {
     for(j=0;j<c2;j++)
     {
         scanf("%d",&b[i][j]);
     }
 }
 printf("the second matrix is:\n");
 for(i=0;i<r2;i++)
 {
     for(j=0;j<c2;j++)
     {
         printf("%d\t",b[i][j]);
     }
     printf("\n");
 }
     for(i=0;i<r1;i++)
 {
     for(j=0;j<c2;j++)
     {
         c[i][j]=0;
         for(k=0;k<c1;k++)
         {
             c[i][j]+=a[i][k]*b[k][j];
         }
     }
 }
 printf("resultant matrix after multiplication is:\n");
 {
     for(i=0;i<r1;i++)
     {
         for(j=0;j<c1;j++)
         {
             printf("%d\t",c[i][j]);
         }
         printf("\n");
     }
 }
 }
     return 0;
 }
Coding is easy!
Tweet
Share
Share
Pin

Related Posts:

  • ArrayArray

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Facebook Page

Recent Posts

  • Uber technologies | History | About |Know more|
  • Best national park in the USA | Top 5|
  • Top 10 richest man in the world | List of 2020s|
  • Array-2D
  • National tequila day 2020 | History| Observance|

Pages

  • About us
  • Contact US
  • Disclaimer
  • Privacy Policy
  • Sitemap
  • Sitemap estudynight
  • Terms and Conditions
©2021 e-studynight | Design: estudynight