Thread: reading a matrix and printing out the matrix

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    reading a matrix and printing out the matrix

    Hi Im writing a function to read the matrix user enters and print it out, the main function:

    Code:
    double **read_matrix(int rows, int cols);
    void print_matrix(int rows, int cols, double **mat);
    
    
    int main(){
    
    double **matrix;
      int rows, cols;
      /* First matrix */
      printf("Matrix 1\n");
      printf("Enter # of rows and cols: ");
      scanf("%d %d",&rows,&cols);
      printf("Matrix, enter %d reals: ",rows*cols);
      matrix = read_matrix(rows,cols); 
      printf("Your Matrix\n");    /* Print the entered data */
      print_matrix(rows,cols,matrix);
      free_matrix(rows, matrix);   /* Free the matrix */
    }
    the 2 functions, read and print are what I am having problems with:

    Code:
    double **read_matrix(int rows, int cols){
    
     double matrix = (double **) malloc(sizeof(double *)*rows);
    int i=0;
      for(i=0; i<rows; i++){
        /* Allocate array, store pointer  */
        matrix2[i] = (double *) malloc(sizeof(double)*cols); 
      }
    
    
    
    
    }
    
    
    void print_matrix(int rows, int cols, double **mat){
    
    
    
     for(i=0; i<rows; i++){    /* Iterate of each row */
        for(j=0; j<cols; j++){    /* In each row, go over each col element  */
          printf("%f ",matrix2[i][j]); /* Print each row element */
        }
        printf("\n");        /* Finish a row, start a new line */
      }
    }
    any help to finish the 2 functions?

    this is what it's suppose to look like:

    Enter # of rows and cols: 4 2
    Matrix, enter 8 reals: -.4 -.3 -.2 -.1 0 .1 .2 .3
    Your Matrix -4.00e-01 -3.00e-01 -
    2.00e-01 -1.00e-01
    0.00e+00 1.00e-01
    2.00e-01 3.00e-01

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are creating a double dimension matrix in read matrix. You need to return a pointer to a double matrix. So, *** on the return.

    You shouldn't be casting the return from malloc. This is C, not C++. In print_matrix, you are using the wrong matrix name. Your parameter says "mat". Int main() requires a return 0 at the end.

    I haven't check the code all the way through, so there may be other errors, also. When you compile, you should be seeing them, or warnings, at least. If you are seeing none, be sure your errors and warnings are turned up on your compiler, and pay close attention to them.

    Also, list them here, when you need assistance. It really helps us find problems without having to use our own compilers.
    Last edited by Adak; 10-22-2012 at 08:12 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    You are creating a double dimension matrix in read matrix. You need to return a pointer to a double matrix. So, *** on the return.
    Yeah, though I suggest that you create a struct for this, e.g.,
    Code:
    typedef struct matrix
    {
        int rows;
        int cols;
        double **entries;
    } matrix_type;
    This way, you can just return a pointer to a matrix_type, and you won't also need to keep passing the number of rows and columns separately.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    by using a struct, how do i store the inputs into the array?

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    and btw, how do print out the numbers in the format of : ie. positive .2 is a 2.00e-01

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lina_inverse
    by using a struct, how do i store the inputs into the array?
    I think it would be easier to convert your print_matrix function as an example:
    Code:
    void print_matrix(const matrix_type *matrix) {
        /* Print the matrix in row major order */
        int i;
        for (i = 0; i < matrix->rows; i++) {
            int j;
            for (j = 0; j < matrix->cols; j++) {
                printf("%f ", matrix->entries[i][j]);
            }
            printf("\n");
        }
    }
    Quote Originally Posted by Lina_inverse
    how do print out the numbers in the format of : ie. positive .2 is a 2.00e-01
    Read up on the format specifiers that can be used with printf.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    For the read matrix function:
    Code:
    double **read_matrix(int rows, int cols){
    
    typedef struct matrix
    {
        int rows;
        int cols;
        double **entries;
    } matrix_type;
    
    matrix_type **mat=malloc(n*sizeof(matrix_type));
    for(i=0;i<n;i++){
    
    
    }
    
    
    
    
    
    
    
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The struct should be defined before the function, not in the function body. It may be simpler to read the number of rows and columns into the members of a matrix object, then write:
    Code:
    void read_matrix(matrix_type *matrix) {
        int i;
        matrix->entries = malloc(matrix->rows * matrix->entries[0]);
        for (i = 0; i < matrix->rows; i++) {
            /* ... */
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by laserlight View Post
    Code:
    matrix->entries = malloc(matrix->rows * matrix->entries[0]);
    You're missing the sizeof operator:
    Code:
    matrix->entries = malloc(matrix->rows * sizeof(matrix->entries[0]));
    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    It can also be done in this manner.
    Code:
    #include <stdio.h>
    
    void read_matrix(double *matrix, int rows, int cols);
    void print_matrix(double *matrix, int rows, int cols);
    
    int main(void)
    {
        int rows, cols;
    
        printf("Matrix 1\n");
        printf("Enter # of rows and cols: ");
        scanf("%d %d",&rows,&cols);
        double matrix[rows][cols];
        
        printf("Matrix, enter %d reals: ",rows*cols);
        read_matrix(&matrix[0][0],rows,cols);
    
        printf("Your Matrix\n");
        print_matrix(&matrix[0][0],rows,cols);
    
        return 0;
    }
    
    void read_matrix(double *matrix, int rows, int cols)
    {
        int i, j;
        for (i=0; i<rows; ++i)
            for (j=0; j<cols; ++j)
                scanf("%lf", (matrix+i*cols+j));
    }
    
    void print_matrix(double *matrix, int rows, int cols)
    {
        int i, j;
        for (i=0; i<rows; ++i)
            for (j=0; j<cols; ++j)
                printf("%lf\n", *(matrix+i*cols+j));
        printf("/n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out a matrix of characters
    By Y2R in forum C Programming
    Replies: 7
    Last Post: 04-14-2012, 11:23 PM
  2. Repeated printing of a specific elements-sparse matrix addition?
    By black_stallion in forum C Programming
    Replies: 0
    Last Post: 11-05-2011, 03:32 AM
  3. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  4. Printing a Matrix?
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2005, 08:11 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM