Results 1 to 10 of 10
  1. #1
    xicensayijan's Avatar
    Join Date
    May 2012
    Gender
    male
    Location
    Hell
    Posts
    46
    Reputation
    10
    Thanks
    2
    My Mood
    Tired

    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

  2. #2
          ( ° ͜ʖ͡°)╭∩╮
    Former Staff
    MarkHC's Avatar
    Join Date
    Nov 2011
    Gender
    male
    Location
    127.0.0.1
    Posts
    2,750
    Reputation
    66
    Thanks
    14,529
    My Mood
    Angelic
    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


    CoD Minion from 09/19/2012 to 01/10/2013

  3. #3
    xicensayijan's Avatar
    Join Date
    May 2012
    Gender
    male
    Location
    Hell
    Posts
    46
    Reputation
    10
    Thanks
    2
    My Mood
    Tired
    k
    tnx dere

    well dis ws frm a online course i found
    i'll try doing it myself den

  4. #4
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Quote Originally Posted by -InSaNe- 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.
    Ah we-a blaze the fyah, make it bun dem!

  5. #5
          ( ° ͜ʖ͡°)╭∩╮
    Former Staff
    MarkHC's Avatar
    Join Date
    Nov 2011
    Gender
    male
    Location
    127.0.0.1
    Posts
    2,750
    Reputation
    66
    Thanks
    14,529
    My Mood
    Angelic
    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


    CoD Minion from 09/19/2012 to 01/10/2013

  6. #6
    graced's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0
    Quote Originally Posted by -InSaNe- 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.

  7. #7
          ( ° ͜ʖ͡°)╭∩╮
    Former Staff
    MarkHC's Avatar
    Join Date
    Nov 2011
    Gender
    male
    Location
    127.0.0.1
    Posts
    2,750
    Reputation
    66
    Thanks
    14,529
    My Mood
    Angelic
    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.


    CoD Minion from 09/19/2012 to 01/10/2013

  8. #8
    graced's Avatar
    Join Date
    Jan 2013
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0
    Quote Originally Posted by -InSaNe- 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 -InSaNe- 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 -InSaNe- 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.
    Last edited by graced; 01-19-2013 at 07:09 AM.

  9. #9
          ( ° ͜ʖ͡°)╭∩╮
    Former Staff
    MarkHC's Avatar
    Join Date
    Nov 2011
    Gender
    male
    Location
    127.0.0.1
    Posts
    2,750
    Reputation
    66
    Thanks
    14,529
    My Mood
    Angelic
    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


    CoD Minion from 09/19/2012 to 01/10/2013

  10. #10
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    (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"
    Last edited by abuckau907; 01-19-2013 at 01:51 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

Similar Threads

  1. A question from a beginner
    By igorchete in forum Vindictus Help
    Replies: 3
    Last Post: 02-13-2012, 07:53 AM
  2. [Help] beginner question: hack detection homemade privat hack
    By Roflathome in forum General Game Hacking
    Replies: 0
    Last Post: 11-06-2011, 05:36 AM
  3. [Question]Coders [Beginners <.....]
    By [gS]-Rojeh in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 15
    Last Post: 02-09-2011, 12:54 PM
  4. Few questions from an inspired beginner
    By BloodSkin in forum Medal of Honor (MOH) Hacks
    Replies: 1
    Last Post: 08-04-2010, 10:05 AM
  5. Beginner Question
    By RecoveryOne in forum C++/C Programming
    Replies: 0
    Last Post: 04-09-2009, 01:34 PM