skip to Main Content

I am trying to take the two matrices as input from the keyboard and then trying to print out their sum on screen using C functions. I have been mind-numbing with this code for many hours and still not getting where am I going wrong. It is printing the two matrices all perfect but it’s not summing up them perfectly. Although it sums up the first row perfectly but doesn’t sum correctly for the rest.

Output is attached. In the screenshot, the real output is shown plus the desired output which i want is photoshopped with red.

#include <stdio.h>
#include <conio.h>
void main(void)
{
    int i,j,order,a[5][5],b[5][5],sum[5][5];
    clrscr();
    printf("Enter the order of matrix: ");
    scanf("%d",&order);
    inputmatrixA(&a[0][0],order);
    printmatrixA(&a[0][0],order);
    inputmatrixB(&b[0][0],order);
    printmatrixB(&b[0][0],order);
    for (i=0;i<order;i++)
    {
        for(j=0;j<order;j++)
        {
            sum[i][j]=a[i][j]+b[i][j];
        }
    }
    printf("Sum of A & Bn");
    for (i=0;i<order;i++)
    {
        for(j=0;j<order;j++)
        {
            printf("%dt",sum[i][j]);

        }
        printf("n");
    }
    getch();
}


inputmatrixA(int *p,int order)
{
    int i,j;
    for (i=0;i<order;i++)
    {
        for (j=0;j<order;j++)
        {
            printf("Enter a[%d][%d]: ",i+1,j+1);
            scanf("%d",p);
            p++;
        }
    }
}


printmatrixA(int *p,int order)
{
    int i,j;
    printf("Matrix An");
    for (i=0;i<order;i++)
    {
        for (j=0;j<order;j++)
        {
            printf("%dt",*p);
            p++;
        }
        printf("n");
    }
}


inputmatrixB(int *q,int order)
{
    int i,j;
    for (i=0;i<order;i++)
    {
        for (j=0;j<order;j++)
        {
            printf("Enter b[%d][%d]: ",i+1,j+1);
            scanf("%d",q);
            q++;
        }
    }
}


printmatrixB(int *q,int order)
{
    int i,j;
    printf("Matrix Bn");
    for (i=0;i<order;i++)
    {
        for (j=0;j<order;j++)
        {
            printf("%dt",*q);
            q++;
        }
        printf("n");
    }
}

3

Answers


  1. In these lines you fill the first 4 elements (for order 2) with values:

    scanf("%d",p);
    p++;
    

    The first 4 elements are int line 0 of your 2D array.

    In this line you print elements 0 and 1 in line 0 and elements 0 and 1 in line 1.

    printf("%dt",sum[i][j]);
    

    Choose one of the two ways to access your array.
    I think you fare better with using the two-index way everywhere.

    Login or Signup to reply.
  2. To explain let’s see what happened.

    Here when you said order is 2x2 and then entered 1 2 3 4

    1 2 3 4 x
    x x x x x
    x x x x x
    x x x x x
    x x x x x
    

    And when you are doing the addition you expected them to stay in this order which it isn’t

    1 2 x x x
    3 4 x x x
    x x x x x
    x x x x x
    x x x x x
    

    This is how it is stored in the 2d array. So rest of the positions contain garbage value and you add them. And prints those garbage values.

    The correct way to do it would be (You would have to make these changes for other functions also).

    void inputmatrixA(int (*p)[5],int order)
    {
        for (int i=0;i<order;i++)
        {
            for (int j=0;j<order;j++)
            {
                printf("Enter a[%d][%d]: ",i+1,j+1);
                scanf("%d",&p[i][j]);
            }
        }
    }
    

    And call it like this:-

      inputmatrixA( a , order);
    

    Note one thing – 2d array which is array of arrays decays into pointer to first element when it is passed to the function. Being said that – that is why we can make changes to the passed array and retain those changes.

    This also explains why those second number inside the bracket is needed. Because that will let the compiler calculate the correct position you want to access when you say a[2][2].

    You made use of the fact that array elements stay in memory contagiously but you didn’t correlate it with the fact that a[2][2] is not the 6th element as you thought it to be if the original array is of size 5x5 because a[2][2] is really the 2*5+2th element (if you consider the contagious layout).

    Also you have to declare those functions (and also provide the definition somewhere) before you use them or else you can put their definition before you use them.

    Login or Signup to reply.
  3. You have an input that permits selecting the size of your matrix, but your matrices laid out in your software are fixed in size.

    Thus, if you input a 2×2 matrix through the UI, it gets stored in a hard-coded 5×5 matrix, and you will not get the input you desire.

    I suggest you rework your matrix code to assume a different data structure. One that looks like

    int rows;
    int columns;
    int* cells;
    

    where the cells are initialized like so

    cells = (int*) malloc(sizeof(int)*rows*columns);
    

    Then to look up cell [4][3], use a transformation function

    int linear_index(int column, int row, int columns, int rows);
    

    such that it returns the desired index in a 1-d matrix

    int linear_index(int column, int row, int columns, int rows) {
        return column + row*columns;
    }
    

    Note that the above function is just a start, there’s no overflow checking on it. You would be wise to come up with some sort of boundary checking code.

    How it might be used would then be

    int cell_value = cells[linear_index(3, 4)];
    

    Finally, don’t forget to free the items you malloc allocated.

    free cells;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search