printing

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
 
C

Christopher Benson-Manica

(OP wants output to look like a matrix)
int x[N1][M],y[M][N2],z[N1][N2];
int n1,n2,m;
int main(void)
{
x[1][1]=1;

Why are you indexing your arrays beginning at 1? Your code is
correct, but you're wasting several elements at index 0, and it's less
readable.
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]);
/* Delete this line... */
printf( "%4d ", z[n1][n2] );
 
C

c language

Hi again,
Thank you very much for your answers.
I could use the Christopher's suggestions to fix the problem however
there is one more thing.
When I am asking the program to print on the screen, it works fine but
when I try to have the results in an output file, everything is mixed
up.
Any suggestions about this problem?
Thanks,
MJ


(OP wants output to look like a matrix)
int x[N1][M],y[M][N2],z[N1][N2];
int n1,n2,m;
int main(void)
{
x[1][1]=1;

Why are you indexing your arrays beginning at 1? Your code is
correct, but you're wasting several elements at index 0, and it's less
readable.
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]); /* Delete this line... */
} printf( "%4d ", z[n1][n2] );
} printf( "\n" );
}
return 0;
}
 
C

Christopher Benson-Manica

c language said:
Thank you very much for your answers.

You're welcome, although in the future please post your reply below
(rather than above) the text you are replying to.
When I am asking the program to print on the screen, it works fine but
when I try to have the results in an output file, everything is mixed
up.
Any suggestions about this problem?

There's not much to say without seeing your code; if you post it we
may be able to offer suggestions.
 
J

Joe Smith

c language schreef:


Use putchar('\n') in strategic places instead of in the prntf statement.
I'm happy to have an excuse to pull out the Gilbert Strang text. What is
this 3 by 3 matrix to represent? I've become mindful of the adage: Keep It
Simple Smartypants in somewhat the same context. Do your multiplication.
Then send it whereever you think you need to. Dummy variables are available
on the cheap. joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top