C
c language
Hello Everybody,
Basically C program prints the array elements in a column however I
would like to print them in a matrix shape. For example see the
following code which multiplies two matrices.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N1 4
#define N2 4
#define M 3
int x[N1][M],y[M][N2],z[N1][N2];
int n1,n2,m;
int main(void)
{
x[1][1]=1;
x[1][2]=2;
x[2][1]=3;
x[2][2]=4;
x[3][1]=5;
x[3][2]=6;
y[1][1]=10;
y[1][2]=20;
y[1][3]=30;
y[2][1]=40;
y[2][2]=50;
y[2][3]=60;
z[1][1]=0;
for (n1=1;n1<N1;++n1)
{
for (n2=1;n2<N2;++n2)
{
for(m=1;m<M;++m)
{
z[n1][n2]+=x[n1][m]*y[m][n2];
printf("[n1:%2d][n2:%2d][z:%4d]\n",n1,n2,z[n1][n2]);
}
}
}
return 0;
}
How I can have the output in a 3*3 matrix?
Thanks,
MJ
Basically C program prints the array elements in a column however I
would like to print them in a matrix shape. For example see the
following code which multiplies two matrices.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N1 4
#define N2 4
#define M 3
int x[N1][M],y[M][N2],z[N1][N2];
int n1,n2,m;
int main(void)
{
x[1][1]=1;
x[1][2]=2;
x[2][1]=3;
x[2][2]=4;
x[3][1]=5;
x[3][2]=6;
y[1][1]=10;
y[1][2]=20;
y[1][3]=30;
y[2][1]=40;
y[2][2]=50;
y[2][3]=60;
z[1][1]=0;
for (n1=1;n1<N1;++n1)
{
for (n2=1;n2<N2;++n2)
{
for(m=1;m<M;++m)
{
z[n1][n2]+=x[n1][m]*y[m][n2];
printf("[n1:%2d][n2:%2d][z:%4d]\n",n1,n2,z[n1][n2]);
}
}
}
return 0;
}
How I can have the output in a 3*3 matrix?
Thanks,
MJ