-
Notifications
You must be signed in to change notification settings - Fork 17
/
matrix.c
120 lines (83 loc) · 3.41 KB
/
matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ------- file: -------------------------- matrix.c ----------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Wed Dec 29 14:15:27 1999 --
-------------------------- ----------RH-- */
/* --- Routines to create 2-dimensional arrays that are contiguous in
memory. Therefore the whole array pointed to by pointer **matrix_??
can be addressed as either 1-dimensional array with pointer
matrix_??[0], or as 2-dimensional array as matrix_??[i][j].
Note: space is initialized to zeros by using calloc rather than malloc.
-- -------------- */
#include <stdlib.h>
#include "rh.h"
#include "error.h"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
extern char messageStr[];
/* ------- begin -------------------------- matrix_char.c ----------- */
char **matrix_char(int Nrow, int Ncol)
{
register int i;
char *theMatrix, **Matrix;
int typeSize = sizeof(char), pointerSize = sizeof(char *);
theMatrix = (char *) calloc(Nrow * Ncol, typeSize);
Matrix = (char **) malloc(Nrow * pointerSize);
for (i = 0; i < Nrow; i++, theMatrix += Ncol)
Matrix[i] = theMatrix;
return Matrix;
}
/* ------- end ---------------------------- matrix_char.c ----------- */
/* ------- begin -------------------------- matrix_int.c ------------ */
int **matrix_int(int Nrow, int Ncol)
{
register int i;
int *theMatrix, **Matrix, typeSize = sizeof(int),
pointerSize = sizeof(int *);
theMatrix = (int *) calloc(Nrow * Ncol, typeSize);
Matrix = (int **) malloc(Nrow * pointerSize);
for (i = 0; i < Nrow; i++, theMatrix += Ncol)
Matrix[i] = theMatrix;
return Matrix;
}
/* ------- end ---------------------------- matrix_int.c ------------ */
/* ------- begin -------------------------- matrix_double.c --------- */
double **matrix_double(int Nrow, int Ncol)
{
register int i;
int typeSize = sizeof(double), pointerSize = sizeof(double *);
double *theMatrix, **Matrix;
theMatrix = (double *) calloc(Nrow * Ncol, typeSize);
Matrix = (double **) malloc(Nrow * pointerSize);
for (i = 0; i < Nrow; i++, theMatrix += Ncol)
Matrix[i] = theMatrix;
return Matrix;
}
/* ------- end ---------------------------- matrix_double.c --------- */
/* ------- begin -------------------------- matrix_float.c --------- */
float **matrix_float(int Nrow, int Ncol)
{
register int i;
int typeSize = sizeof(float), pointerSize = sizeof(float *);
float *theMatrix, **Matrix;
theMatrix = (float *) calloc(Nrow * Ncol, typeSize);
Matrix = (float **) malloc(Nrow * pointerSize);
for (i = 0; i < Nrow; i++, theMatrix += Ncol)
Matrix[i] = theMatrix;
return Matrix;
}
/* ------- end ---------------------------- matrix_float.c --------- */
/* ------- begin -------------------------- freeMatrix.c ------------ */
void freeMatrix(void **matrix)
{
const char routineName[] = "freeMatrix";
if (matrix == NULL || matrix[0] == NULL) {
sprintf(messageStr, "Trying to free NULL pointer");
Error(ERROR_LEVEL_2, routineName, messageStr);
} else {
/* --- Free the memory allocated for matrix matrix -- ----------- */
free(matrix[0]);
free(matrix);
}
}
/* ------- end ---------------------------- freeMatrix.c ------------ */