#include #include #include "matrix.h" /* * This function allocates space for an n x m matrix, placing an array * of n pointers to arrays of m floats into the pointer v. * The number of rows and columns are stored in the matrix structure, * which is returned. * * In an object oriented environment, this function would be known as * the constructor. */ matrix construct_matrix (int n, int m) { matrix A; int i; A.nrows = n; A.ncolumns = m; A.v = (float **) malloc (sizeof (float *) * A.nrows); for (i=0; i= A.nrows || j >= A.ncolumns || i < 0 || j < 0) { fprintf (stderr, "access violation!\n"); return 0; } return 1; } /* This function sets a matrix element equal to c */ void set_matrix (matrix A, int i, int j, float c) { if (check_bounds (A, i, j)) A.v[i][j] = c; } /* This function returns the value of a matrix element */ float get_matrix (matrix A, int i, int j) { if (check_bounds (A, i, j)) return A.v[i][j]; return 0.0; } /* This function prints the matrix in a nice format */ void print_matrix (matrix A) { int i, j; /* for each row... */ for (i=0; i