how to display a matrix ?
hello everybody!
I have a problem which i need some help to solve. Suppose i have a matric with char matrix[3][3] element. i want to write a function which take this matrix as argument and display it in this way written below (with an empty matrix)
| | | |
| | | |
| | | |
and for example if matrix[1][2] is 'o' and matrix [0][1] is '+' (the rest is still empty) i want to display
| | +| |
| | |o|
| | | |
can somebody tell me/give me some hint how i should design/implement this function?
ok but how do i solve this
i can't do it with a for sats like th example this example (se below not complete code just example to show )
Code:
int i, j;
for (i=0;i <3; i++)
for (j=0;j<3; j++)
printf("| %c",putchar(matrix[i][j]));
because i will lose the position in x direction and always write it in the same position. how can i do so i can move the position for both | and the character when i want to write a new character?
ok here is another attempt
sorry i am quite tired and therefore the formulation in english is not the best you can see from me this time :(
As i mention before i want to display the content of a char matrix (3*3 in this example to simplify things) so i can display the content in this way (assume that matrix[0][1] contain '+'
the rest is empty
| |+| |
| | | |
| | | |
if i do it with a for statement
for (j=0; j<3;j++)
for(i=0; i<3; i++)
{
printf("| %c",putchar(matrix[i][j]));
if (i==2)
printf("| \n");
}
i will never move from the first positon and i will only write on the first position
se below
|char|
|char|
|char|
and char will be the content of the element matrix[i][2];
so my question is how do i move the position so i can write | and the character on the right position
i hope you understund my question better now
ok SlyMaelstrom you are right
but can somebody explain/tell me why i get a run time crash when i call this
Code:
void test(char matrix[][3]) {
int i, j;
for (j=0; j<3;j++)
for(i=0; i<3; i++) {
printf("|%c",matrix[j][i]);
if (i==2)
printf("| \n");
}
}
int main () {
char ma[3][3];
int i, j;
for(i=0; i<3; i++)
for(j=0;j<3;j++)
ma[i][j]=' ';
ma[0][0]='*';
ma[0][1]='+';
ma[2][0]='i';
ma[1][0]='k';
test(ma[3][3]);
but it doesn't when i do like this
Code:
int main () {
char ma[3][3];
int i, j;
for(i=0; i<3; i++)
for(j=0;j<3;j++)
ma[i][j]=' ';
ma[0][0]='*';
ma[0][1]='+';
ma[2][0]='i';
ma[1][0]='k';
for (j=0; j<3;j++)
for(i=0; i<3; i++) {
printf("|%c",ma[j][i]);
if (i==2)
printf("| \n");