oops. my fault. lack of attention, as usual.
you had
which does not overflow the array.Code:result[ires-1]++;
my apologies.
oops. my fault. lack of attention, as usual.
you had
which does not overflow the array.Code:result[ires-1]++;
my apologies.
Originally Posted by valenok
ires is decremented by one so i wouldnt write out of bounds.
Code:result[ires-1]++;
Originally Posted by valenok
NM that, thnks for the help
I am not able to reproduce your problem, since I don't have TurboC. However, why don't you try to get to the bottom of things yourself for your particular compiler? Make the program tell you what is really happining. I claim that debugging your own stuff is faster than posting a request for help and waiting for a helpful response. You also get to learn a really, really, really important part of program implementation: debugging.Originally Posted by loko
For example, you could do something like this:
Now what happens?Code:#define TOTAL_ROLL 10 .. .. .. dice1 = ( rand() % 6 ) + 1; dice2 = ( rand() % 6 ) + 1; printf("rollnum = %d: die1 = %d, die2 = %d\n", rollnum, dice1, dice2);
Regards,
Dave
Last edited by Dave Evans; 07-21-2005 at 08:32 AM.
No. You did not initialize the array properly.Originally Posted by valenok
See? They did it correctly, and when you rewrote it, you broke it:Originally Posted by valenok
That was correctly initializes the array to all zeros. An array with any members initialized at the time of declaration initializes all elements not specificly initialized to zero.Originally Posted by loko
Or just do what they did in the first place.Originally Posted by valenok
Quzah.
Hope is the first step on the road to disappointment.
Just re-read C99, section 6.7.8.12 states:
That proves that I was wrong saying that array was not initialized.If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
Still, quzah, you're wrong saying I did not initialize the array.
I did it by calling memset().
So you did.
Quzah.
Hope is the first step on the road to disappointment.
Ok, I already know I'm going to regret this for one reason or another... The initialization is OK, not that it needs initialization, at least in the code that follows. I tried to take the OP's code and minimally change it to what 'I think' the OP wanted to do. If not I hope you'll set me straight and tell me about the 36000 iteration for loop
Regards,
Brian
Code:#include<stdio.h> #include<time.h> #include<stdlib.h> #define NUM_ROLLS 12 int main() { long result[NUM_ROLLS] = { 0 }; long rollnum = 0, ipos; int dice1, dice2, ires; srand( time( 0 ) ); for( rollnum = 0; rollnum < NUM_ROLLS; rollnum++ ){ dice1 = ( rand() % 6 ) + 1; dice2 = ( rand() % 6 ) + 1; ires = dice1 + dice2; result[rollnum] = ires; } for( ipos = 0; ipos < NUM_ROLLS; ipos++ ){ printf( "(%d) Total: %d\n", ipos+1, result[ipos] ); } getchar(); return 0; }
I almost always use this:Originally Posted by loko
long array[12] = {0};
much cleaner than memset IMO.
Also if you have this:
it yields undefined behaviourCode:long *array[12]; // array of pointers memset(array, 0, sizeof array);
but long *array[12] = {0}; doesn't
Laserve, how comes that memset leads() to an undefined behaviour?
Make it clear please.
It isn't undefined.
Quzah.
Hope is the first step on the road to disappointment.
>how comes that memset leads() to ...
The all-bits-zero pointer values may not be null pointers.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
That still doesn't mean it's 'undefiend' though.
Quzah.
Hope is the first step on the road to disappointment.
note that laserve himself invented array of pointers to long.
it was array of long-s.