⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rand.cpp

📁 我刚才已经上载了一份
💻 CPP
字号:

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

#define MAT_SIZE (16)

#define DEBUG

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 */

#ifdef DEBUG
  printf("A:\n");
  mat_print(A,n);
  printf("B:\n");
  mat_print(B,n);
  printf("C:\n");
  mat_print(C,n);
#endif

    exit(0);
};

/*
  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 = (int **)malloc(n*sizeof(int*));	/* allocate rows */
    if (!ptr) return (int**)-1;
    for (i=0 ; i<n ; i++)
    {
	ptr[i] =(int *)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;
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -