乱数プログラム 使用例5



[ 簡単な説明 ]

データ攪拌関数の使用例です。
それぞれ25個の整列した int 型、double 型、char型データ配列を攪拌し、結果を表示します。

プログラム・ソース("test5.c")           top (先頭に戻る)
/*		test5.c		配列撹拌ルーチンテスト	*/
#include <stdio.h>
#include <stdlib.h>
#include "random.h"

main()
{
	int i, j, len, n, *data_i, *ip;
	double *data_d, *dp;
	unsigned char **data_c, **sp, *spp;

	n = 25;
	len = 2;
	data_i = (int *)malloc(n * sizeof(int));
	data_d = (double *)malloc(n * sizeof(double));
	data_c = (unsigned char **)malloc(n * sizeof(unsigned char *));
	for(i = 0, sp = data_c; i < n; i++)
		*sp++ = (unsigned char *)malloc((len + 1) * sizeof(unsigned char));

	for(i = 0, ip = data_i, dp = data_d, sp = data_c; i < n; i++)
	{
		*ip++ = *dp++ = i + 1;
		for(j = 0, spp = *sp++; j < len; j++)	*spp++ = 'a' + i;
		*spp = '\0';
	}

	printf("*** before shuffle ***\n");
	printf("* data_i\n");
	for(i = 0, ip = data_i; i < n; i++)	printf("%3d", *ip++);
	printf("\n* data_d\n");
	for(i = 0, dp = data_d; i < n; i++)	printf("%3.0f", *dp++);
	printf("\n* data_c\n");
	for(i = 0, sp = data_c; i < n; i++)	printf("%3s", *sp++);
	putchar('\n');

	shuffle_i(data_i, n, 1L);
	shuffle_d(data_d, n, 2L);
	shuffle_c(data_c, n, 3L);

	printf("\n*** after shuffle ***\n");
	printf("* data_i\n");
	for(i = 0, ip = data_i; i < n; i++)	printf("%3d", *ip++);
	printf("\n* data_d\n");
	for(i = 0, dp = data_d; i < n; i++)	printf("%3.0f", *dp++);
	printf("\n* data_c\n");
	for(i = 0, sp = data_c; i < n; i++)	printf("%3s", *sp++);
	putchar('\n');
	return 1;
}

出力結果           top (先頭に戻る)
*** before shuffle ***
* data_i
  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
* data_d
  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
* data_c
 aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy

*** after shuffle ***
* data_i
 15  5  8 16 12 17 19 23  3  1  2 18  6 11 20 10 22 14 21  7 13  9 24  4 25
* data_d
 24 18  3 23 13 25 15 22  2  5 19  7 12 16 21 20  4  1  8 11  9 10 17 14  6
* data_c
 mm gg aa dd qq jj ww nn uu kk ff ss vv rr ee yy pp ii xx oo tt bb ll hh cc