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
In these lines you fill the first 4 elements (for order 2) with values:
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.
Choose one of the two ways to access your array.
I think you fare better with using the two-index way everywhere.
To explain let’s see what happened.
Here when you said order is
2x2
and then entered1 2 3 4
And when you are doing the addition you expected them to stay in this order which it isn’t
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).
And call it like this:-
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 size5x5
becausea[2][2]
is really the2*5+2
th 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.
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
where the cells are initialized like so
Then to look up cell [4][3], use a transformation function
such that it returns the desired index in a 1-d matrix
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
Finally, don’t forget to free the items you
malloc
allocated.