int init_random();
int **init_mat(int);
int mat_mul(int **, int **, int **, int);
int mat_print(int **A, int n);
int main(int argc, char **argv)
{
int **A,**B,**C;
int n;
/* check the command line argument */
if (argc > 2)
{
printf("Error, usage: %s [matrix size].\n",argv[0]);
return -1;
}
else if (argc == 2)
{
if ((n=atoi(argv[1])) < 1)
{
printf("Error, the argument must be a positive integer.\n");
return -2;
}
}
else n=MAT_SIZE;
init_random(); /* initialize the random */
if ((A=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix A */
if ((B=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix B */
if ((C=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix C */
if (mat_mul(A,B,C,n) == -1) exit(-2); /* multiply A and B into C */
/*
init_random:
This function initializes the pseudo-random stream used with the random
function. These are standard unix supplied values.
In order to assure a new random set every time, I used the system time
as a seed to initialize the pseudo random generator.
*/
int init_random()
{
static long state1[32] = {
3,
0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
0xf5ad9d0e, 0x8999220b, 0x27fb47b9
};
unsigned seed = time(NULL); /* time based random seed */
int n = 128; /* must use the state size */
initstate(seed, (char*)state1, n); /* initialize the state for random */
setstate((char*)state1);
return 0;
};
/*
init_mat:
This function input is an integer - the row/col size of the new matrix.
It allocates memory for the new matrix and returns a pointer to it.
If it fails it returns -1.
The newly allocated matrix is filled with random values.
*/
int **init_mat(int n)
{
int i,j;
int **ptr = malloc(n*sizeof(int*)); /* allocate rows */
if (!ptr) return (int**)-1;
for (i=0 ; i<n ; i++)
{
ptr[i] = malloc(sizeof(int)*n); /* allocate columns */
if (!ptr) return (int**)-1;
};
for (i=0 ; i<n ; i++)
for (j=0 ; j<n ; j++)
ptr[i][j]=random()&100; /* fill it with numbers from 0 to 100 */
return (int**) ptr;
};
/*
mat_mul:
Inputs: pointers to three matrices and int N, all are assumed to be N*N.
Output: -1 for failure, else 0 and the third matrix C is set to A*B.
*/
int mat_mul(int **A, int **B, int **C, int n)
{
int i,j,k;
if (!A || !B || !C || n<1) return -1;
for (i=0 ; i<n ; i++)
{
for (j=0 ; j<n ; j++)
{
C[i][j]=0;
for (k=0 ; k<n ; k++)
C[i][j]+=(A[i][k]*B[k][j]);
};
};
return 0;
};
/*
mat_print:
A matrix output function. for debugging.
*/
int mat_print(int **A , int n)
{
int i,j,k;
if (!A || n<1) return -1;
for (i=0 ; i<n ; i++)
{
for (j=0 ; j<n ; j++)
printf(" %d ",A[i][j]);
printf("\n");
}
return 0;
};
static long int seed = 1L;
static long int const a =16807L, m = 2147483647L, q = 127773L, r = 2836L;
double Rondom()
{
seed = a*(seed%q) - r*(seed/q);
if( seed < 0 )
seed += m;
return (double) seed / (double) m;
}