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

printf on matrix...

P: n/a
Hello!

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

Thanks

Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
Nov 14 '05 #2

P: n/a


Bilbo wrote:
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[i][j]);
putchar('\n');
}
return 0;
}

Nov 14 '05 #3

P: n/a
Lewis Bowers wrote:
Bilbo wrote:
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[i][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[i][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;
}
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.