01 #include <stdio.h> 02 int main() 03 { 04 int x1[] = {1, 2, 3}, x2[3], *x3, *x4 = new int [3]; 05 x3 = x2; // x3 = &x2[0]; 06 for (int i1 = 0; i1 < 3; i1++) { 07 x2[i1] = x1[i1]; // x2 = x1; は許されない 08 x4[i1] = 10 * (i1 + 1); 09 } 10 for (int i1 = 0; i1 < 3; i1++) 11 printf("i1 %d x1 %d x2 %d x3 %d x4 %d\n", i1, x1[i1], x2[i1], x3[i1], x4[i1]); 12 x3 = x4; 13 for (int i1 = 0; i1 < 3; i1++) 14 printf("i1 %d x1 %d x2 %d x3 %d x4 %d\n", i1, x1[i1], x2[i1], x3[i1], x4[i1]); 15 return 0; 16 }
i1 0 x1 1 x2 1 x3 1 x4 10 i1 1 x1 2 x2 2 x3 2 x4 20 i1 2 x1 3 x2 3 x3 3 x4 30
i1 0 x1 1 x2 1 x3 10 x4 10 i1 1 x1 2 x2 2 x3 20 x4 20 i1 2 x1 3 x2 3 x3 30 x4 30
// int x[5000000]; // 実行できない // int *x = (int *)malloc(5000000 * sizeof(int)); // 実行可 int *x = new int [5000000]; // 実行可 x[4999999] = 100; printf("%d\n", x[4999999]);
01 #include <stdio.h> 02 int main() 03 { 04 int x1[][2] = {{1, 2}, {3, 4}, {5, 6}}, *x2, **x3, **x4; 05 x2 = &x1[0][0]; // x2 = x1[0]; でも可 06 // m 行 n 列の (i, j) 要素の位置: i * n + j 07 for (int i1 = 0; i1 < 3; i1++) { 08 for (int i2 = 0; i2 < 2; i2++) 09 printf("x1 %d x2 %d\n", x1[i1][i2], x2[i1*2+i2]); 10 } 11 x3 = new int *[3]; 12 for (int i1 = 0; i1 < 3; i1++) 13 x3[i1] = new int [2]; 14 x4 = x3; 15 x3[0][0] = 10; 16 x3[0][1] = 20; 17 x3[1][0] = 30; 18 x3[1][1] = 40; 19 x3[2][0] = 40; 20 x3[2][1] = 50; 21 for (int i1 = 0; i1 < 3; i1++) { 22 for (int i2 = 0; i2 < 2; i2++) 23 printf("x3 %d x4 %d\n", x3[i1][i2], x4[i1][i2]); 24 } 25 // 以下は許されない(x3[0][0],x3[0][1] だけは参照できる) 26 int *x5 = &x3[0][0]; // int *x5 = x3[0]; でも可 27 for (int i1 = 0; i1 < 3; i1++) { 28 for (int i2 = 0; i2 < 2; i2++) 29 printf("x3 %d x5 %d\n", x3[i1][i2], x5[i1*2+i2]); 30 } 31 return 0; 32 }
x1 1 x2 1 x1 2 x2 2 x1 3 x2 3 x1 4 x2 4 x1 5 x2 5 x1 6 x2 6
x3 10 x4 10 x3 20 x4 20 x3 30 x4 30 x3 40 x4 40 x3 40 x4 40 x3 50 x4 50
x3 10 x5 10 x3 20 x5 20 x3 30 x5 1628620015 x3 40 x5 134280023 x3 40 x5 12594745 x3 50 x5 0
001 /****************************/ 002 /* キュー */ 003 /* coded by Y.Suganuma */ 004 /****************************/ 005 #include <stdio.h> 006 #include <stdlib.h> // RAND_MAXを使用する場合も必要 007 #include <time.h> 008 #include "MT.h" 009 010 #define MAX 3 // 最大待ち行列長 011 012 int QUE[MAX], first, last; // 待ち行列,先頭,最後尾 013 014 /****************************/ 015 /* 待ち行列長の計算 */ 016 /* return : 待ち行列長 */ 017 /****************************/ 018 int que_len() 019 { 020 int len = 0; 021 if (first >= 0) { 022 if (last >= first) 023 len = last - first + 1; 024 else 025 len = MAX - first + last + 1; 026 } 027 return len; 028 } 029 030 /************************************/ 031 /* 待ち行列に追加 */ 032 /* k : 追加データ */ 033 /* return : 0 : 正常 */ 034 /* 1 : オーバーフロー */ 035 /************************************/ 036 int queue(int k) 037 { 038 int ind = 0; 039 if (first < 0) { 040 QUE[0] = k; 041 first = 0; 042 last = 0; 043 } 044 else { 045 int n = (last + 1) % MAX; 046 if (QUE[n] > 0) 047 ind = 1; 048 else { 049 QUE[n] = k; 050 last = n; 051 } 052 } 053 return ind; 054 } 055 056 /****************************/ 057 /* 待ち行列の先頭を削除 */ 058 /* return : 削除データ */ 059 /****************************/ 060 int dequeue() 061 { 062 int ind = 0; 063 if (first >= 0) { 064 ind = QUE[first]; 065 QUE[first] = 0; 066 first = (first + 1) % MAX; 067 if (QUE[first] <= 0) { 068 first = -1; 069 last = -1; 070 } 071 } 072 return ind; 073 } 074 075 /*************/ 076 /* main 関数 */ 077 /*************/ 078 int main() 079 { 080 // 初期化 081 first = -1; 082 last = -1; 083 for (int i1 = 0; i1 < MAX; i1++) 084 QUE[i1] = 0; 085 init_genrand((unsigned)time(NULL)); // init_genrand(123), C/C++: srand(seed); 086 // 実行 087 // 追加1 088 printf(" 待ち行列長 %d\n", que_len()); 089 int k = (int)(1000 * genrand_real3()); // C/C++: k = (int)(1000 * (double)rand() / RAND_MAX) 090 if (queue(k) > 0) { 091 printf("***error*** オーバーフロー\n"); 092 exit(1); 093 } 094 printf("%d を最後尾に追加\n", k); 095 printf(" 待ち行列長 %d\n", que_len()); 096 // 追加2 097 k = (int)(1000 * genrand_real3()); 098 if (queue(k) > 0) { 099 printf("***error*** オーバーフロー\n"); 100 exit(1); 101 } 102 printf("%d を最後尾に追加\n", k); 103 printf(" 待ち行列長 %d\n", que_len()); 104 // 削除1 105 int n = dequeue(); 106 if (n > 0) { 107 printf("先頭 %d を削除\n", n); 108 printf(" 待ち行列長 %d\n", que_len()); 109 } 110 // 追加3 111 k = (int)(1000 * genrand_real3()); 112 if (queue(k) > 0) { 113 printf("***error*** オーバーフロー\n"); 114 exit(1); 115 } 116 printf("%d を最後尾に追加\n", k); 117 printf(" 待ち行列長 %d\n", que_len()); 118 // 追加4 119 k = (int)(1000 * genrand_real3()); 120 if (queue(k) > 0) { 121 printf("***error*** オーバーフロー\n"); 122 exit(1); 123 } 124 printf("%d を最後尾に追加\n", k); 125 printf(" 待ち行列長 %d\n", que_len()); 126 // 削除2 127 n = dequeue(); 128 if (n > 0) { 129 printf("先頭 %d を削除\n", n); 130 printf(" 待ち行列長 %d\n", que_len()); 131 } 132 // 追加5 133 k = (int)(1000 * genrand_real3()); 134 if (queue(k) > 0) { 135 printf("***error*** オーバーフロー\n"); 136 exit(1); 137 } 138 printf("%d を最後尾に追加\n", k); 139 printf(" 待ち行列長 %d\n", que_len()); 140 // 追加6 141 k = (int)(1000 * genrand_real3()); 142 if (queue(k) > 0) { 143 printf("***error*** オーバーフロー\n"); 144 exit(1); 145 } 146 printf("%d を最後尾に追加\n", k); 147 printf(" 待ち行列長 %d\n", que_len()); 148 return 0; 149 }
待ち行列長 0 410 を最後尾に追加 待ち行列長 1 320 を最後尾に追加 待ち行列長 2 先頭 410 を削除 待ち行列長 1 137 を最後尾に追加 待ち行列長 2 241 を最後尾に追加 待ち行列長 3 先頭 320 を削除 待ち行列長 2 888 を最後尾に追加 待ち行列長 3 ***error*** オーバーフロー
メルセンヌ・ツイスタ( MT.h )
/* A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto. Before using, initialize the state by using init_genrand(seed) or init_by_array(init_key, key_length). Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Any feedback is very welcome. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) */ /* The original version of http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c was modified by Takahiro Omi as - delete line 47 "#include<stdio.h>" - delete line 174 int main(void){...} - change N -> MT_N - change N -> MT_N - change the file name "mt19937ar.c" -> "MT.h" */ /* Period parameters */ #define MT_N 624 #define MT_M 397 #define MATRIX_A 0x9908b0dfUL /* constant vector a */ #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ static unsigned long mt[MT_N]; /* the array for the state vector */ static int mti=MT_N+1; /* mti==MT_N+1 means mt[MT_N] is not initialized */ /* initializes mt[MT_N] with a seed */ void init_genrand(unsigned long s) { mt[0]= s & 0xffffffffUL; for (mti=1; mti<MT_N; mti++) { mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* In the previous versions, MSBs of the seed affect */ /* only MSBs of the array mt[]. */ /* 2002/01/09 modified by Makoto Matsumoto */ mt[mti] &= 0xffffffffUL; /* for >32 bit machines */ } } /* initialize by an array with array-length */ /* init_key is the array for initializing keys */ /* key_length is its length */ /* slight change for C++, 2004/2/26 */ void init_by_array(unsigned long init_key[], int key_length) { int i, j, k; init_genrand(19650218UL); i=1; j=0; k = (MT_N>key_length ? MT_N : key_length); for (; k; k--) { mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + init_key[j] + j; /* non linear */ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ i++; j++; if (i>=MT_N) { mt[0] = mt[MT_N-1]; i=1; } if (j>=key_length) j=0; } for (k=MT_N-1; k; k--) { mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i; /* non linear */ mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ i++; if (i>=MT_N) { mt[0] = mt[MT_N-1]; i=1; } } mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ } /* generates a random number on [0,0xffffffff]-interval */ unsigned long genrand_int32(void) { unsigned long y; static unsigned long mag01[2]={0x0UL, MATRIX_A}; /* mag01[x] = x * MATRIX_A for x=0,1 */ if (mti >= MT_N) { /* generate N words at one time */ int kk; if (mti == MT_N+1) /* if init_genrand() has not been called, */ init_genrand(5489UL); /* a default initial seed is used */ for (kk=0;kk<MT_N-MT_M;kk++) { y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); mt[kk] = mt[kk+MT_M] ^ (y >> 1) ^ mag01[y & 0x1UL]; } for (;kk<MT_N-1;kk++) { y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK); mt[kk] = mt[kk+(MT_M-MT_N)] ^ (y >> 1) ^ mag01[y & 0x1UL]; } y = (mt[MT_N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); mt[MT_N-1] = mt[MT_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; mti = 0; } y = mt[mti++]; /* Tempering */ y ^= (y >> 11); y ^= (y << 7) & 0x9d2c5680UL; y ^= (y << 15) & 0xefc60000UL; y ^= (y >> 18); return y; } /* generates a random number on [0,0x7fffffff]-interval */ long genrand_int31(void) { return (long)(genrand_int32()>>1); } /* generates a random number on [0,1]-real-interval */ double genrand_real1(void) { return genrand_int32()*(1.0/4294967295.0); /* divided by 2^32-1 */ } /* generates a random number on [0,1)-real-interval */ double genrand_real2(void) { return genrand_int32()*(1.0/4294967296.0); /* divided by 2^32 */ } /* generates a random number on (0,1)-real-interval */ double genrand_real3(void) { return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); /* divided by 2^32 */ } /* generates a random number on [0,1) with 53-bit resolution*/ double genrand_res53(void) { unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; return(a*67108864.0+b)*(1.0/9007199254740992.0); } /* These real versions are due to Isaku Wada, 2002/01/09 added */
講義内容目次 | 菅沼ホーム | 次ページ |