printf on matrix...

B

Bilbo

Hello!

I have a matrix type of variable, lets call it matrix[2][3]...can I see on
screen it via printf?? how??

Thanks
 
J

jota

I have a matrix type of variable, lets call it matrix[2][3]...can I see on
screen it via printf?? how??

for(int n=0;n<2;n++)
for(int m=0;m<3;m++)
printf("matrix[%d][%d]=%d\n",n,m,matrix[n][m]);

//jota
 
L

Lewis Bowers

Bilbo said:
Hello!

I have a matrix type of variable, lets call it matrix[2][3]...can I see on
screen it via printf?? how??

I would put the printf in nested for loops.
Here is an example of type integer.

#include <stdio.h>

int main(void)
{
int i,j,matrix[2][3] = {{1,2,3},{4,5,6}};
for(i = 0; i < 2; i++)
{
for(j = 0;j < 3; j++)
printf("%-6d",matrix[j]);
putchar('\n');
}
return 0;
}
 
E

E. Robert Tisdale

Lewis said:
Bilbo said:
I have a matrix type of variable, lets call it matrix[2][3]...
can I see on screen it via printf? How?

I would put the printf in nested for loops.
Here is an example of type integer.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
for(size_t i = 0; i < 2; ++i) {
for(size_t j = 0; j < 3; ++j)
printf("%-6d", matrix[j]);
putchar('\n');
}
return EXIT_SUCCESS;
}


#include <stdlib.h>
#include <stdio.h>

int matrix_fprintf(FILE* fp, const char* format,
size_t m, size_t n, int matrix[m][n]) {
int characters = 0;
for(size_t i = 0; i < m; ++i) {
for(size_t j = 0; j < n; ++j)
characters += fprintf(fp, format, matrix[j]);
characters += fprintf(fp, "\n");
}
return characters;
}

int main(int argc, char* argv[]) {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
matrix_fprintf(stdout, " %6d", 2, 3, matrix);
return EXIT_SUCCESS;
}
 

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
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top