A beginner question [ C ]

Posts 1–10 of 10 · Page 1 of 1
A beginner question [ C ]
Hey guys I jst strted learning C and was studying arrays, in which i got this program regarding dice roll probabilities ( 2 dices)
It was like this

#include <stdio.h>
#include <stdlib.h>

main()
{
int i;
int d1, d2;
int a[13]; /* uses [2..12] */

for(i = 2; i <= 12; i = i + 1)
a[i] = 0;

for(i = 0; i < 100; i = i + 1)
{
d1 = rand() % 6 + 1;
d2 = rand() % 6 + 1;
a[d1 + d2] = a[d1 + d2] + 1;
}

for(i = 2; i <= 12; i = i + 1)
printf("%d: %d\n", i, a[i]);

return 0;
}


I got everything, except the 3rd for loop used
now my questions r
1. Why did we use the for loop for the third time to print??Why didnt we printf in the 2nd loop (which we used fr the iterations)??
2. Why do we even printf there? I mean wont this print i = 2 -12 instead of the 1-100 matrix we want to print?
How r we relating the 2nd loop with the printf statement with another for loop between them (with totally different conditions)

It would be nice if u cud explain clearly.. coz i m jst strted into this language
thnx
Quote Originally Posted by xicensayijan View Post
1. Why did we use the for loop for the third time to print??Why didnt we printf in the 2nd loop (which we used fr the iterations)??
The seconds loop goes till 100... your array has only 12 positions, it will give a crash if you try to print it there. Basically cause you're trying to access a memory region that does not belong to your program.

Quote Originally Posted by xicensayijan View Post
2. Why do we even printf there? I mean wont this print i = 2 -12 instead of the 1-100 matrix we want to print?
:S you don't have a 100 matrix.. you have a array with 12 positions. The second loop is only setting the values on your array using some (weird) math.

PS: Don't just copy&paste those codes. You'll end up like this all the time... without understanding what the code does. Try to do them by yourself Get a book, read it
k
tnx dere

well dis ws frm a online course i found
i'll try doing it myself den
Quote Originally Posted by MarkHC View Post
The second loop is only setting the values on your array using some (weird) math.

The maths aren't THAT weird.
The % operator is modulo(integer remainder):
Code:
0 % 6 = 0
1 % 6 = 1
2 % 6 = 2
3 % 6 = 3
4 % 6 = 4
5 % 6 = 5
6 % 6 = 0
7 % 6 = 1
... etc
As you can see this will only allow for the number 0, 1, 2, 3, 4 and 5
Since the eyes on a dice have the numbers 1, 2, 3, 4, 5 and 6 we simply add 1 to the modulo result.
Quote Originally Posted by Hell_Demon View Post
The maths aren't THAT weird.
The % operator is modulo(integer remainder):
Code:
0 % 6 = 0
1 % 6 = 1
2 % 6 = 2
3 % 6 = 3
4 % 6 = 4
5 % 6 = 5
6 % 6 = 0
7 % 6 = 1
... etc
As you can see this will only allow for the number 0, 1, 2, 3, 4 and 5
Since the eyes on a dice have the numbers 1, 2, 3, 4, 5 and 6 we simply add 1 to the modulo result.
I know... I don't even know why I said weird anyways... :P
Quote Originally Posted by MarkHC View Post
The seconds loop goes till 100... your array has only 12 positions, it will give a crash if you try to print it there. Basically cause you're trying to access a memory region that does not belong to your program.
This is false. It most likely won't crash. The virtual memory for the program is allocated by pages and you would have to be very unlucky to be so close to the boundary.
Quote Originally Posted by graced View Post
This is false. It most likely won't crash. The virtual memory for the program is allocated by pages and you would have to be very unlucky to be so close to the boundary.
If you try to access a index outside the array, the program WILL crash. Giving you a Segmentation Fault error. Test it yourself if you don't believe. On OO languages, it will just give you an exception saying that you tried to access a value outside the array like ArrayIndexOutOfBoundsException on Java, but on C, it will just crash your program.

PS: Good job bumping this old thread.
Quote Originally Posted by MarkHC View Post
If you try to access a index outside the array, the program WILL crash. Giving you a Segmentation Fault error. Test it yourself if you don't believe.
I actually tried it before posting, just to be sure. It'll crash if you access index WAY (edit: well, it worked for +200, but crashed +400 outside for me) outside of the array, but that's not the case in this example.

Quote Originally Posted by MarkHC View Post
On OO languages, it will just give you an exception saying that you tried to access a value outside the array like ArrayIndexOutOfBoundsException on Java, but on C, it will just crash your program.
The question was about the C language. Arrays are not objects in C/C++ and a[i] is just a short construct for *(a+i). There are no array bound checks in C/C++, like in Java. Java is based on different principles.

Quote Originally Posted by MarkHC View Post
PS: Good job bumping this old thread.
I'm sorry, but I couldn't ignore it. I actually created an account just for this.
Quote Originally Posted by graced View Post
I actually tried it before posting, just to be sure. It'll crash if you access index WAY (edit: well, it worked for +200, but crashed +400 outside for me) outside of the array, but that's not the case in this example.
If you say so...

Quote Originally Posted by graced View Post
The question was about the C language. Arrays are not objects in C/C++ and a[i] is just a short construct for *(a+i). There are no array bound checks in C/C++, like in Java. Java is based on different principles.
That's exactly what I said.

Quote Originally Posted by graced View Post
I'm sorry, but I couldn't ignore it. I actually created an account just for this.
LOL
(BECAUSE OF the modulo, index will NEVER be out of range. d1 and d2 will always be in range 1-6 !! d1+d2 ALWAYS 2-12)

it's weird because, the author doesn't "like" the random function...so he implemented his own...kind of. But it's very weird because you never know which index will be set, or how many times --- but because of how rand() works, and the fact that it calls it 100 times: each item in the array should get set at least once, most of them being set lots of times.
However, because d1 will always be at least 1 (same with d2), the index will never be 0 or 1 so you're never setting the first two items.

It's not lol when [someone] doesn't read the code carefully, yet still spouts off, get's called out on it, and spouts some more.

"If you try to access a index outside the array, the program WILL crash." -- So logically, buffer-overflow exploits can't exist? **** shit.
Even if that was correct, it'd still be irrelevant :/
LOL

@OP - If you still need help, try re-asking a question, we can't really explain "why" you use a for loop. Are you trying to create a deck of cards, or dice game? why are the first 2 items never being set?? You have to define the problems/questions better

1. You could have! doing it how you posted results in, only 10 calls to Print()
- if you put it inside the 2nd for loop, that'd be 100 Print()s. -- of all the values, not just the final 12.

2. Yes, it will only print items 2-12. (it's not 100 long...you declared it a[13] !! not a[100] :/ ) -- the 100 is there to "increase randomness"
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us