printf on matrix...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bilbo

    #1

    printf on matrix...

    Hello!

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

    Thanks



  • jota

    #2
    Re: printf on matrix...

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

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

    //jota


    Comment

    • Lewis Bowers

      #3
      Re: printf on matrix...



      Bilbo wrote:
      [color=blue]
      > Hello!
      >
      > I have a matrix type of variable, lets call it matrix[2][3]...can I see on
      > screen it via printf?? how??
      >[/color]

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

      Comment

      • E. Robert Tisdale

        #4
        Re: printf on matrix...

        Lewis Bowers wrote:
        [color=blue]
        > Bilbo wrote:
        >[color=green]
        >>I have a matrix type of variable, lets call it matrix[2][3]...
        >>can I see on screen it via printf? How?[/color]
        >
        > 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;
        > }[/color]

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

        Comment

        Working...