Here's the actual codes, we are asked to implement the functions, that's why i have to stuck with what is given below.
=============================================================================
#include <stdlib.h>
#include <stdio.h>
/* This header file contains the function prototypes;
you will have to provide the function(s) itself in
matrix.c.
*/
#include "matrix.h"
void print_matrix(MATRIX *mat);
#ifndef _MATRIX_H_
#define _MATRIX_H_
typedef struct Matrix {
int Col, Row; /* number of columns/rows */
double *vals; /* values of the matrix */
} MATRIX;
/* returns the value at ith row and jth column
We assume that the left-most element in the
top row is in the first row and the first column. */
double get_val(MATRIX *mat, int i, int j);
/* sets the value at (row)th row and (col)th column to be val
We assume that the left-most element in the
top row is in the first row and the first column. */
void set_val(MATRIX *mat, int row, int col, double val)
/* allocates the memory for a matrix with 'row' rows and 'col' columns */
MATRIX *make_a_Matrix(int row, int col) <=== My problem here
/* frees the memory */
void free_Matrix(MATRIX *mat);
/* finds the LU factorization of A.
returns 1 if A is factorizable; otherwise,
returns 0 */
int LU_factorize(MATRIX *A, MATRIX *L, MATRIX *U)
/* solves a LU factorized equation LUx=B;
accepts L, U and B as arguments; returns x, which is dynamically
allocated in the subroutine. NOTE: You can assume that L and U are proper lower/upper triangular
matrices and the equation LUx=B always has a solution. */
MATRIX *solve_eqn(MATRIX *L, MATRIX *U, MATRIX *B);
/* Do not add any more code here. You can add your own function
declarations at the beginning of the matrix.c file.
*/
#endif
/////////////////////////////////
//
////////////////////////////////
//
//
////////////////////////////////
int main()
{
MATRIX *A, *B, *X;
MATRIX *L, *U;
int row, col; //num of rows and columns for A
int i,j;
double temp;
char temp_str[257];
while(gets(temp_str)) {
sscanf(temp_str, "%d%d", &row, &col);
/* allocate the matrices */
A=make_a_Matrix(row, col); <== alllocation function
B=make_a_Matrix(row, 1);
L=make_a_Matrix(row,col);
U=make_a_Matrix(row,col);
/* X is not allocated here */
for (i=1; i<=row; i++)
for (j=1; j<=col; j++) {
gets(temp_str);
sscanf(temp_str, "%lg", &temp);
set_val(A, i, j, temp);
};
for (i=1; i<=row; i++) {
gets(temp_str);
sscanf(temp_str, "%lg", &temp);
set_val(B, i, 1, temp);
};
if (LU_factorize(A, L, U)==0) {
/* failed to LU factorize */
printf("Can't factorize\n");
} else {
/* solves the equation and prints out the values */
p
printf("U is:\n");
print_matrix(U);
X=solve_eqn(L, U, B);
printf("X is:\n");
print_matrix(X);
/* collects memory */
free_Matrix(X);
};
/* collects memory */
free_Matrix(A); free_Matrix(B);
free_Matrix(L); free_Matrix(U);
};
exit(0);
};
/* prints a matrix to stdout */
void print_matrix(MATRIX *mat)
{
int i,j;
/* does nothing if the pointer is NULL */
if (mat==NULL)
return;
for (i=1; i<=mat->Row; i++) {
for (j=1; j<=mat->Col; j++)
printf("%10.2f",get_val(mat, i, j));
printf("\n");
};
return;
};