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