#include <stdio.h>
#include <stdlib.h>

double **matrix_alloc(unsigned, unsigned);
void matrix_free(double **, unsigned, unsigned);

void matrix_print(double **, unsigned, unsigned);
void matrix_fill(double **, unsigned, unsigned);

#define LEN 3
#define WID 3

int
main()
{
	double **matrix = matrix_alloc(LEN, WID);
	matrix_fill(matrix, LEN, WID);
	matrix_print(matrix, LEN, WID);
	matrix_free(matrix, LEN, WID);
	return 0;
}

void matrix_fill(double **matrix, unsigned len, unsigned wid)
{
	for (int i = 0; i < len; i++){
        for (int j = 0; j < wid; j++){
			if (i == j)
				matrix[i][j] = 1;
			else
				matrix[i][j] = 0;
		}
	}
}

double **matrix_alloc(unsigned len, unsigned wid)
{
	double **matrix = NULL;
	matrix = (double **)(malloc(wid*sizeof(double *)));
	for(int i = 0; i < wid; i++){
			matrix[i] = malloc(sizeof(double));
	}
	return matrix;
}

void matrix_free(double ** matrix, unsigned len, unsigned wid)
{
	for(int i = 0; i < wid; i++)
			free(matrix[i]);
	free(matrix);
}


void 
matrix_print(double **matrix, unsigned len, unsigned wid)
{
	for (int i = 0; i < len; i++){
        for (int j = 0; j < wid; j++){
			printf("%lf ", matrix[i][j]);
		}
		putchar('\n');
	}
}
