Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
a rand array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="RoSsIaCrIiLoIA, post: 1711742"] [i][i][i] thank you but seems to me there are odd results e.g. if rand_array is like you suggest => inserisci il massimo[0 finisce]> 888888888 inserisci il numero di elementi> 20 312557105 548251158 760618515 876091816 886173925 888503572 888804673 888826177 888871515 888882642 888884513 888885725 888887085 888888029 888888553 888888700 888888768 888888843 888888873 888888877 inserisci il massimo[0 finisce]> 0 rand_array1 seems 'ok' inserisci il massimo[0 finisce]> 99 inserisci il numero di elementi> 98 0 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 5 6 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 inserisci il massimo[0 finisce]> 300 inserisci il numero di elementi> 50 4 5 6 11 17 24 25 29 44 47 58 60 63 70 82 85 94 98 99 103 108 120 127 130 132 1 43 150 151 165 166 167 176 191 192 195 201 210 214 216 230 233 245 246 248 263 2 64 272 274 275 277 inserisci il massimo[0 finisce]> 300 inserisci il numero di elementi> 50 1 4 8 10 22 25 31 40 47 49 52 53 54 67 77 86 87 92 105 113 114 119 128 133 136 142 146 147 163 166 173 184 191 197 203 207 215 218 223 232 234 241 242 254 256 263 269 274 280 281 inserisci il massimo[0 finisce]> 8888888 inserisci il numero di elementi> 10 539728 635645 1238316 1973567 2832166 2844598 4184902 5806132 6061761 7364582 inserisci il massimo[0 finisce]> 0 /* C++ and C but you use a C++ compiler and try ... */ #include <iostream.h> #include <stdlib.h> #include <limits.h> #include <time.h> #define U unsigned #define F for #define W while #define R return #define os cout << #define is cin >> int BogusRandArray( U* a, U size, U max_val) {static int i = 1; unsigned j, t, m1; double m, u; /*---------------------*/ if( a==0 || size<2 || max_val<=size ) return 0; if(i) {i = 0; srand(time(0));} m = (double) UINT_MAX/(size+1); m1 = m; a[0] = (U) _lrand() % m1; for( j = 1; j<size; ++j) a[j] = a[j - 1] + 1 + (U) _lrand() % m1; if(a[size - 1] > max_val) { m = (double) max_val/ size; m1 = m; u = (double)(size - 1) * m + (double)((U) _lrand() % m1); /* os "u==" << u << "(size)*m=" << (size)*m; */ u = ( (double) a[size - 1] / u ); for( j = 0, t = 0; j<size; ++j) { a[j] = (double) a[j] / u; if(a[j] <= t && j!=0) ++a[j]; t = a[j]; } } return 1; } U rand_array( U* a, U size, U sup_val ) {static int i = 1; unsigned j, k, h; /*---------------------*/ if( a==0 || size<2 || sup_val<=size ) return 0; if(i) {i = 0; srand(time(0));} h = sup_val - size; a[0] = (U) _lrand() % h; for( j = 1; j<size; ++j) a[j] = a[j-1] + 1 + (U) _lrand() % (h - a[j-1]-1 + j); R 1; } U intervallo(U a, U b) {R a + (U) _lrand() % (b-a);} U rand_array1( U* a, U size, U sup_val ) {static int i = 1; unsigned j, k, h; /*---------------------*/ if( a==0 || size<2 || sup_val<=size ) return 0; if(i) {i = 0; srand(time(0));} k = sup_val / size; a[0] = (k<=1 ? 0 : (U) _lrand() % k); for( j = 1; j<size; ++j) a[j] = (a[j-1]+1 >= (h=j*k) ? a[j-1] + 1 : intervallo( a[j-1] + 1, h) ); R 1; } /* it is a c++ main */ int main(void) {U a[100] = {0}, i, j=1, k, z; /*--------------------------------*/ W(j!=0) { os "inserisci il massimo[0 finisce]> "; is j; if(j==0) break; os "inserisci il numero di elementi> "; is k; if(k>100) {os "Troppi elementi\n";continue;} z = rand_array1( a, k, j); if(z) { F( i = 0; i<k; ++i) os " " << a[i]; os "\n"; } else os "Errore\n"; } R 0; }[/i][/i][/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
a rand array
Top