By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

printing

P: n/a
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

Jun 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
c language <ja*******@gmail.com> wrote:

(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;
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.



Jun 14 '06 #2

P: n/a

c language schreef:
How I can have the output in a 3*3 matrix?


Use putchar('\n') in strategic places instead of in the prntf statement.

Jun 14 '06 #3

P: n/a
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

Christopher Benson-Manica wrote:
c language <ja*******@gmail.com> wrote:

(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;
}


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


Jun 14 '06 #4

P: n/a
c language <ja*******@gmail.com> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jun 15 '06 #5

P: n/a

<kl*****@xs4all.nl> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...

c language schreef:
How I can have the output in a 3*3 matrix?


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
Jun 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.