3x3 Matrix printout

This is a discussion on 3x3 Matrix printout within the C Programming forums, part of the General Programming Boards category; I'm reading from a file that has a 3x3 matrix such as: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    3x3 Matrix printout

    I'm reading from a file that has a 3x3 matrix such as:

    1.0 2.0 3.0
    4.0 5.0 6.0
    7.0 8.0 9.0

    and I'm trying to allocate memory and re-print the same matrix as it is given back to the prompt? I'm not sure exactly how to do that. The numbers can hold negative values as well. I know memory is laid out linearly so even a 2D matrix can still be viewed as a 1D. I need to store it so that later I can actually call specific i,j elements. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If it's only a 3x3 matrix, are you simply using a 2D automatic array of doubles? Could you post the code you have?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    This is what I have so far, I kind of have an idea on how to attack this, but my actual coding isn't going as smoothly. Thanks.

    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            FILE *infile;
    
            while(fgets(line, sizeof(line), infile) != NULL)
            {
                    malloc(3*sizeOf(double *);
                    //allocate memory of a 3x3 doubles
                   //somehow copy the invidivual vales to the correct location.
                  //will probably be in a double for loop with i,j locations
             }
              return(0);
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    By automatic, I meant not dynamic. I picture something like this.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          double matrix[3][3];
          size_t i, j;
          for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                if ( fscanf(file, "%lf", &matrix[i][j]) != 1 )
                {
                   puts("input error");
                   return 0;
                }
             }
          }
          fclose(file);
          for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                printf("%g ", matrix[i][j]);
             }
             putchar('\n');
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I see what you mean now with automatic 2D array. This makes more sense. I'm given different files to run the test and I know you can specify that by the params main calls. I'm also given the task to have two flags a -f1 and -f2. They can be called in these two fashions:

    prog fname -f1 param1 param2 param3
    prog -f1 param1 param2 param3 fname
    prog fname -f1 -f2 param1 param2 param3
    prog -f1 -f2 param1 param2 param3 fname

    I'm not sure whats the best way to check all these cases and not having to actually specifying the static const char filename[] = "file.txt"; Since it can be at various locations.
    Thank you for your help.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I was actually able to figure out the problem for the param name, just have a pointer to char point to the file.

    static const char *filename;
    filename = argv[1];

    Problem I'm having now is, I wanted to make sure that matrix, is actually storing the values. So I ran a very simple test trying to get the matrix[1][2] with the given example of

    1 5 9 13
    2 6 10 14
    3 7 11 15
    4 8 12 16

    positioin (1,2) should equate to 7 but its really equalling 10? Am I looking at this the wrong way or is there something I'm not doing right? I always looked at the ith value as the rows and jth as the columns, I mean the print out is correct from the file but just getting the value at matrix isn't correct?

    printf("value of (1,2) is %f\n",matrix[1][2]);

    This is assumed using the portion of code you have provided up there.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    can u post u'r code. so that we can find out where u are going wrong or what has been misplaced

    s.s.harish

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
       static const char *filename[];
       filename = argv[1];
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          double matrix[3][3];
          size_t i, j;
          for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                if ( fscanf(file, "%lf", &matrix[i][j]) != 1 )
                {
                   puts("input error");
                   return 0;
                }
             }
          }
          fclose(file);
          for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                printf("%g ", matrix[i][j]);
             }
             putchar('\n');
          }
       }
       //print out of value at location (1,2)
       printf("value of (1,2) is %g\n",matrix[1][2]);
       return 0;
    }
    The program so far will accept a file that has a 3x3 matrix form such as:

    1 2 3
    4 5 6
    7 8 9

    When I try to print out matrix[1][2] I would assume the value would be 8 but it actually prints out 6. The printout of the matrix is correct but I'm wondering why the actual ith,jth element isn't coming out correctly?

    Another issue that I'm sure it might be format, is if I have a 3x3 matrix that consist of

    1.0 0.0 0.0
    0.0 1.0 0.0
    0.0 0.0 1.0

    It outputs:

    1 0 0
    0 1 0
    0 0 1

    It seems to truncate all the zeros and that is actually required for output.


    Very side note, I'm also required to have extra flags to perform certain task, If these flags are allowed either in the front before calling the file or after. What is the best way of checking where these flags are called and making sure enough params are given? Thank you all for your time.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by cisokay
    The program so far will accept a file that has a 3x3 matrix form such as:

    1 2 3
    4 5 6
    7 8 9

    When I try to print out matrix[1][2] I would assume the value would be 8 but it actually prints out 6. The printout of the matrix is correct but I'm wondering why the actual ith,jth element isn't coming out correctly?
    It is correct. You're not understanding how two dimensional arrays work apparently.

    An array:
    Code:
    char array[3] = { 1, 2, 3 };
    Print array[2] and you get '3' displayed.

    Code:
    for each column
        print array[ column ]

    An array of arrays:
    Code:
    char array[3][3] = 
    {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 2 }
    };
    So why on earth should this print 8 instead of 6?

    Code:
    for each row
         for each column
            print array[ row ][ column ]
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    So if I misunderstood how to call a specific array. If I wanted to get the value at (2,1) from a 3x3 matrix consisting of :

    1 2 3
    4 5 6
    7 8 9

    It would be 8 ?

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Another issue that I'm sure it might be format, is if I have a 3x3 matrix that consist of

    1.0 0.0 0.0
    0.0 1.0 0.0
    0.0 0.0 1.0

    It outputs:

    1 0 0
    0 1 0
    0 0 1

    It seems to truncate all the zeros and that is actually required for output.

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    Actually I just figured out this issue, I'm suppose to be using %lf.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    So far I have this to work with, I'm suppose to read in a file that has a 3x3 matrix of values such as:

    1 2 3
    4 5 6
    7 8 9

    The program will prompt to input a 3 numbers such as a vector to do vector multiplication. So if that matrice was given along with vector 1,2,3 the output should be

    (1*1) + (1*2) + (1*3) = row1 value
    (2*4) + (2*5) + (2*6) = row2 value
    (3*7) + (3*8) + (3*9) = row3 value

    The individual multiplications should be resaved into the matrix for further use later on, so the values should be modifying. I've gotten some of the output but I don't think the loop is working 100%. Any suggestions ?

    Code:
    #include <stdio.h>
    #define MAX 256
    
    
    int main(int argc, char **argv)
    {
       static const char *filename;
       filename = argv[1];
       FILE *file = fopen(filename, "r");
       double matrix[3][3];
       char input[MAX];
       double val0,val1,val2,row0,row1,row2;
       int i,j;
    
       //checks the file to make sure its no empty and copy the values to a 1D array
       if ( file != NULL )
       {
          int i, j;
          for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                if ( fscanf(file, "%lf", &matrix[i][j]) != 1 )
                {
                   puts("Input Error");
                }
             }
          }
          fclose(file);
          //prints out the matrix that was read from the file
          for(i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for(j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                printf("%g ", matrix[i][j]);
             }
             putchar('\n');
          }
       }
    
    
       printf("\n");
       printf("Input Values to Multiply ");
       fflush(stdout);
       fgets(input,sizeof(input),stdin);
       sscanf(input,"%lf %lf %lf",&val0, &val1, &val2);
    
       //multiplying the rows and summing them up
       for(i = 0; i <3 ; i++)
         for(j = 0; j < 3; j++) {
            matrix[j][i] = matrix[j][i] * val0;
            matrix[j+1][i+1] = matrix[j][i] * val1;
            matrix[j+2][i+2] = matrix[j][i] * val2;
          }
       printf("matrix after multiplication\n");
             //prints out the matrix that was read from the file
          for(i = 0; i < sizeof matrix / sizeof *matrix; ++i )
          {
             for(j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
             {
                printf("%g ", matrix[i][j]);
             }
             putchar('\n');
          }
    
    
    
    
       printf("\n");
       printf("%1.1f %1.1f %1.1f %\n",row0,row1,row2);
    
    
    
            return(0);
    }

  14. #14
    Registered User
    Join Date
    Apr 2005
    Posts
    53
    I'm getting values truncated as they are read into the matrix. I'm suppose to preserve the actual values of the file, but when outputting it to the console, I should only have it be 2 decimal places.

    Example Matrix:

    1.222 1.333 1.444
    1.555 1.666 1.777
    1.888 1.999 1.000

    should output to console like
    1.2 1.3 1.4
    1.5 1.6 1.7
    1.8 1.9 1.0

    but the matrix itself should still retain the values. I seem to get an overflow of values somewhere when I don't restrict the copying of the values from the file to the double arr? Any suggestion to fix this?

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("%1.1f %1.1f %1.1f %\n",row0,row1,row2);
    I believe you want:
    Code:
       printf("%3.1f %3.1f %3.1f \n",row0,row1,row2);
    I think the first number is the width of the total field, not the number of digits before the decimal.

    Or you can leave it blank:
    Code:
       printf("%.1f %.1f %.1f \n",row0,row1,row2);

    Also you had an extra % at the end.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21