Stuck on allocating a matrix from a file and reusing that to compute another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • craziileeboi
    New Member
    • Dec 2007
    • 6

    Stuck on allocating a matrix from a file and reusing that to compute another

    Hi

    I have been pulling my hair out trying to figure this out. Please help!!!

    Here is my project description:

    By using a pointer to pointers **A and **B and the function calloc()
    allocate the memory for the 4x4 matrices A[][] and B[][].
    By using the pointers *a and *b and the function malloc()
    allocate the memory for the 4-dimensional vectors a[] and b[].
    Read the components of a and A from the given input file matrix.dat
    (which is in the public/final directory; you need to copy it into your directory).
    The first line of the input file contains the components of a. The rows of
    matrix A are the last four lines of the input file. Print the components
    of a[] and A[][]. By calling the function pmatrix(A,B,a,b ,n), n=4,
    which you will construct, determine the components of the matrix B=1.5*A*A, and the
    vector b=(A*a)+0.5*a. Print the components of the matrix B and the vector b in the main.
    Free dinamically allocated memory.
    ............... ............... ............... ..... ............... ...............
    Output should look like:


    Vector a is:
    a[] = -1.200 1.200 -2.000 -0.500
    Matrix A is:
    2.700 1.500 3.200 2.400
    -3.200 0.700 -2.600 4.300
    1.400 0.600 2.100 -1.800
    1.500 1.700 -2.600 -0.700
    Matrix B is:
    ..... ...... ...... ......
    ..... ...... ...... ......
    ..... ...... ...... ......
    ..... ...... ...... ......
    Vector b is:
    b[] = ..... ..... ..... .....


    Code:
    void prob2(void)
    {
    FILE *matrix;
    int k, z, c;
    double **A, **B, *a, *b;
    
    
    matrix=fopen("matrix.dat", "r");
    if(matrix == NULL){
            printf("Error in file opening for *matrix.\n");
            exit(1);
            }
    
    
    A= (double **)calloc((size_t)4, sizeof(double *));
    B= (double **)calloc((size_t)4, sizeof(double *));
    a= (double *)malloc((size_t) 4 * sizeof(double));
    b= (double *)malloc((size_t) 4 * sizeof(double));
    
    for(z=0; z<4; z++){
            A[z] = (double *)calloc((size_t)4, sizeof(double *));
    }
    for(z=0; z<4; z++)
            for(c=0; c<4;c++) A[z][c] = (double)z * (double)c;
    printf("\n");
    printf("Matrix A is:\n\n");
    
            for(row=0; row<4; row++){
                    for(col=0; col<4; col++){
                    fscanf(matrix, "%lf", A+col);
                    }
            for(col=0; col<4; col++)
                    printf(" %7.3f", A[col]);
            printf("\n");
            }
    
    printf("\nVector a is:\n\n");
            for(row=0; row<5; row++){
                    for(col=0; col<4; col++){
                    fscanf(matrix, "%lf", A+col);
                    }
            }
    printf("a[] = ");
            for(col=0; col<4; col++)
                    printf(" %7.3f", A[col]);
            printf("\n");
    
    printf("\n");
    printf("Matrix B is: \n\n");
    
    }
  • craziileeboi
    New Member
    • Dec 2007
    • 6

    #2
    I can get the program to scan the file and print out the correct information but from there to the next part of computing B i am lost and cant seem to find how to multiply matrices etc

    Comment

    • craziileeboi
      New Member
      • Dec 2007
      • 6

      #3
      My current problem is the fact taht i keep getting Segmentation Faults and its right when i try to execute pmatrix in void prob2().

      Comment

      • craziileeboi
        New Member
        • Dec 2007
        • 6

        #4
        NEWLY UPDATED PROGRAM

        Code:
        void prob2(void)
        {
        
        
        FILE *matrix;
        int k, z, c;
        double **A,**B, *a, *b, a_norm;
        
        
        matrix=fopen("matrix.dat", "r");
        if(matrix == NULL){
                printf("Error in file opening for *matrix.\n");
                exit(1);
                }
        
        
        A= (double **)calloc((size_t)4, sizeof(double *));
        a= (double *)malloc((size_t) 4 * sizeof(double));
        b= (double *)malloc((size_t) 4 * sizeof(double));
        
        
        
        printf("Matrix A is:\n\n");
        
        A = (double **)calloc((size_t)4, sizeof(double *));
                for(row=0; row<4; row++){
                        for(col=0; col<4; col++)
                                fscanf(matrix, "%lf", A+col);
                        for(col=0; col<4; col++) printf(" %7.2f", A[col]);
                printf("\n");
                }
        
        
        
        
        
        
        printf("\nVector a is:\n\n");
                for(row=0; row<5; row++){
                        for(col=0; col<4; col++){
                        fscanf(matrix, "%lf", A+col);
                        }
                }
        printf("a[] = ");
                for(col=0; col<4; col++)
                        printf(" %7.3f", A[col]);
                printf("\n");
        
        printf("\n");
        printf("Matrix B is: \n\n");
        
        
        
        pmatrix(A, B, a, b, n);
        
        
        }
        
        void pmatrix(double **A, double **B, double *a, double *b, int n)
        {
        int k, row, col;
        double c[ROWA][COLB], dot_p;
        
        
        for(row=0; row<ROWA; row++){
                for(k=0, dot_p=0.; k<COLA; k++){
                        dot_p += A[row][k] * A[k][col];}
                c[row][col] = dot_p;
        }
                printf("c[%2d][]={", col);
                for(row=0; row<ROWA; row++){
                        printf("  %.2f", c[row][col]);
                        if((row+1)%5 == 0) printf("\n");
        }
        
                printf("}\n");
                return;
        }

        Comment

        Working...