Help with matrices

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

    Help with matrices

    Can anyone tell me why the mult_matrix isnt working correctly? ignore extra text, this is a small section of a larger assignment... I'm stuck and any help would be appreciated thanks

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define ROWA 4
    #define COLA 4
    
    void prob1(void);
    void prob2(void);
    double unit_norm(double vec[4], int cols);
    void matrix_mult(double a[4][4],double b[4], double c[4], int rows);
    double unit_vec(double vec[4], int cols);
    double pmatrix(double **A, double **B, double *a, double *b, int n);
    
    
    
    
    double a[4][4], b[4], c[4];
    int n=4,t,  row, rows, col, cols, k;
    
    main()
    {
            int menu;
            printf("Enter the function number to execute (1-2):");
            scanf("%d", &menu);
            switch(menu){
             case 1:
                prob1();
                break;
             case 2:
                prob2();
                break;
             default:
                printf("prob%d() does not exist.\n", menu);
            }
            exit(0);
    }
    
    
    void prob1(void)
    {
    double K[4][4] = { { 2., -4.,  1.,  0.},
                        {-4.,  5., -2.,  1.},
                        { 1., -2.,  6., -3.},
                        { 0.,  1., -3.,  7.}};
    double y[4], x[4] = { 1./3., 2./3., 1./3., pow(3.,0.5)/3.};
    int k, rows, i;
    double dot_p;
    
    
    printf("\nInitial vector:\n");
    printf("x[0] = [" );
    for(rows=0; rows<4; rows++){
            printf(" %f", x[rows]);
            }
    printf("]\n");
    printf("\nNew Vector:\n");
    for(t=0; t<10; t++){
            printf("y[%d] = [", t);
            for(rows = 0; rows<4; rows++){
                   // printf(" %g", matrix_mult(double a[4][4], double b[4], double c[4], int row));
            }
            printf("]\n");
    }
    }
    
    
    void matrix_mult(double a[4][4], double b[4], double c[4], int row)
    {
    /* compute c[]=a[][]*b[]*/
    int col;
    double sum;
    for(row=0;row<4; row++){
            for(col=0, sum=0.0;col<4; col++){
                    sum +=a[row][col]*b[col];
                    }
            c[row]=sum;
            }
    return;
    }
    
    double unit_norm(double vec[4], int cols)
    {
    double sum=0;
    /* normalize a vector */
    int i;
    for(i=0; i<cols; i++)
            sum += vec[i] * vec[i];
    sum = sqrt(sum);
    for(i=0; i<cols; i++)
    vec[i]=vec[i]/sum;
    return sum;
    }
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Before we can help you, you need to tell us what and where the problem is.

    If your problem is in the printf statement (line 60) with matrix_mult, then that is not going to work. You need to pass existing arrays to matrix_mult, and then you can print out the result (i.e. c array) in the loop with the printf statement. Since matrix_mult does not return anything, you can't use it in the printf statement.

    Comment

    • craziileeboi
      New Member
      • Dec 2007
      • 6

      #3
      its okay i got THIS program to work but if you can check my other thread or post, that would be great.

      Comment

      Working...