/* Problem: We want [4][4], [4][5], [5][4], and [5][5] to all be four, and the rest to be generated between one and three */
/* snippet the first */
int arrays[10][10];
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
arrays[i][j] = /* randomly generate a number between one and three */;
}
}
for(int i = 4; i < 6; i++){
for(int j = 4; j < 6; j++){
arrays[i][j] = 4;
}
}
/* snippet the second */
int arrays[10][10];
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
if(/* */){
arrays[i][j] = 4;
} else {
arrays[i][j] = /* randomly generate a number between one and three */;
}
}
}
Which snippet would you use?
Excuse the pseudo-ish code.
Name:
Anonymous2011-05-31 4:10
I prefer the first. Would rather do 4 extra assignments than 200 unnecessary comparisons.
>>16
you could just bitwise and it by 3. it obviously won't be the same, but it will have the same effect.
Name:
Anonymous2011-06-03 15:15
rand() sucks. I once used it to fill a square of monochrome pixels and there was an outstanding Moire pattern. Since then I've always used Mersenne Twister.
>>25
Out of date. Things have changed since 2002. OTOH, all rand()%n is bad where n isn't a power of 2 (similar story for &).
Name:
Anonymous2011-06-04 16:07
The C-style comments (/* ... */) are often preferred over the C++-style comments (// ...\n) because they do not introduce ``significant whitespaces'', and they're claimed to be more elegant and readable. Now, observe:
int a = 10, b = 0, c = 30;
int *p = b;