Practice Program







  • Program of Subtraction of two Matrix.
  • Solution:
    void main()
    {
        int i,j,a[10][10],b[10][10],c[10][10],n;
        int m;
        printf("Enter number of rows: ");
        scanf("%d",&n);
    
        printf("Enter Number of columns: ");
        scanf("%d",&m);
    
    //taking first array element as input 
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("Enter the element of first array: ");
                scanf("%d",&a[i][j]);
            }
        }
    
    
    //printing first array elements
        printf("First Matrix is: \n");
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("%d\t",a[i][j]);
            }
            printf("\n");
        }
    
    //taking second array element as input 
    
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("Enter the element of Second array: ");
                scanf("%d",&b[i][j]);
            }
        }
    //printing second array elements
    
        printf("Second Matrix is: \n");
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("%d\t",b[i][j]);
            }
            printf("\n");
        }
    //subtracting first array element from second array elements
    for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                c[i][j] = a[i][j] - b[i][j];
            }
        }
    
    //printing the difference
        printf("Differnce of two matrix is :\n");
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("%d\t",c[i][j]);
            }
        printf("\n");
        }
    
     
    

    Output1
    Enter number of rows: 3
    Enter Number of columns:3

    Enter the Element:1
    Enter the Element:2
    Enter the Element: 3
    Enter the Element: 4
    Enter the Element:5
    Enter the Element:6
    Enter the Element:7
    Enter the Element: 8
    Enter the Element: 9

    First Matrix is:
    1      2      3    
    4      5      6   
    7      8      9
    Enter the Element of Second array:1
    Enter the Element of Second array:2
    Enter the Element of Second array :3
    Enter the Element of Second array : 4
    Enter the Element of Second array:5
    Enter the Element of Second array:6
    Enter the Element of Second array:7
    Enter the Element of Second array: 8
    Enter the Element of Second array: 9

    Second Matrix is:
    2      4      5    
    4      8      2   
    1      2      3

    difference of two matrix is :
    -1     -2     -2    
    0       -3      4  
    6        6      6 

    Output2
    Enter number of rows: 2
    Enter Number of columns:2

    Enter the Element:10
    Enter the Element:20
    Enter the Element: 30
    Enter the Element:40

    First Matrix is:
    10      20    
    30     40   

    Enter the Element of Second array:10
    Enter the Element of Second array:20

    Enter the Element of Second array : 30
    Enter the Element of Second array:40
    Second Matrix is:
    20     30    
    40     50

    difference of two matrix is :
    -10     -10    
    -10     -10










    defining a function 'give_sub' which will print the difference of two matrix
    void give_sub(int a[10][10],int b[10][10],int n,int m)
    {   int i,j;
        int c[10][10];
    //subtracting first array element from second array elements
    
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                c[i][j] = a[i][j] - b[i][j];
            }
        }
    //printing the difference
        printf("Difference of two matrix is :\n");
        for(i=0;i<i++)
        {
            for(j=0;j<j++)
            {
                printf("%d\t",c[i][j]);
            }
        printf("\n");
        }
    }
    

    Using above define function 'give_sub()':
    void main()
    {
        int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
        
        int a[3][3] = {{2,3,5}{6,7,8}{9,10,11}}
            printf("Difference of two matrix is :");
        give_sub(a,b,3,3)
    }
    

    Output1
    Sum of two matrix is :
    -1     -1     -2    
    -2     -2     -2  
    -2     -2      -2

    Using above define function 'give_sub()':
    void main()
    {
        int a[2][2] = {{2,4,5},{6,8,10},{9,12,12}};
        
        int a[3][3] = {{1,2,3}{4,5,6}{7,8,9}};
        
        give_sub(a,b,3,3)
    }
    

    Output2
    Sum of two matrix is :
    1     2     2
    2     3     4
    2     4     3

    Using above define function 'give_sub()':
    void main()
    {
        int a[2][2] = {{10,20},{40,50}};
        
        int a[2][2] = {{5,5},{10,10}};
        
        
        
        give_sub(a,b,2,2)
    }
    

    Output3
    Sum of two matrix is :
    5     15
    30    40