Help Me Please!!!

This is a discussion on Help Me Please!!! within the C Programming forums, part of the General Programming Boards category; here is my code for program where i have to roll a pair of dice 36000 times randomly here is ...

  1. #1
    Unregistered
    Guest

    Question Help Me Please!!!

    here is my code for program where i have to roll a pair of dice 36000 times randomly here is the code and when i compile it it will give me random rolls for even faces of the dice only please tell me what is wrong.


    include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int frequency[13];

    int main()
    {
    int x,sum,dice_array[1,36000],count_array[13];
    srand(time(NULL));
    sum=0;

    for (x=2; x<=13; x++)
    {
    count_array[x]=0;
    }
    for (x=0; x<=36000; x++)
    {
    dice_array[0,x] = 1 + rand() % 6;
    dice_array[1,x] = 1 + rand() % 6;
    }
    for (x=0; x<=36000; x++)
    {
    sum=(dice_array[0,x]+dice_array[1,x]);
    count_array[sum]++;
    sum=0;
    }
    printf("\n Number Frequency\n");

    for (x=2; x<=12; x++)
    {
    printf("%d%10d\n",x,count_array[sum]);
    }
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > int x,sum,dice_array[1,36000],count_array[13];

    I'd be interested in seeing a "sizeof(dice_array)" here. I doubt it is what you expect. In any case, why do you have it this way?

    Quzah.

  3. #3
    Unregistered
    Guest
    Im trying to figure out why when i run my program its giving me values only on even numbers. Its giving me the right about of rolls.

    here is the assignment im working on www.cs.ucf.edu/~riqbal/assign4.html

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int x, array[DSIZE];
    srand(time(0));
    for( x=0; x < DSIZE; x++ ) array[x] = rand()%6;

    Where's the problem?

  5. #5
    Unregistered
    Guest
    i cant find the problem either but just run the code and look at the output, it will only give values on even numbered dice faces i am totally confused. here is the output:


    Number Frequency
    2 6037
    3 0
    4 5979
    5 0
    6 6038
    7 0
    8 6029
    9 0
    10 5994
    11 0
    12 5923

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    umm, you DO know your array is one dimensional, not 2? You declare an array of only one dimension. And the reason you get only even output is because you add the number to itself:

    array[0,x] is the same as array[x];
    array[1000,x] is the same as array[x];

    The comma operator returns the value of the rightmost operand (the comma operator is really only used in the case of a for statement, BTW).

    You also violate your array boundaries -- array[1,36000] really means array[36000] which means the indices go from 0 to 35999.

    You probably want to declare the array like THIS:

    dice_array[2][36001].

    Also, you need to init your count array with x < 13, not x <= 13 because the indices go from 0 to 12 -- count_array[13] is reading beyond your array.

  7. #7
    Unregistered
    Guest

    Talking

    You dont need the limits to be in an array. ie. a[36000]. Just use a for loop, (x=2; x <=36000; x++) to increment up to 36000. Define SIZE 13 in preprocessor assignment.

    # include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SIZE 13



    int main()
    {
    int sum,diceone,dicetwo,count_array[];


    srand(time(NULL));
    sum=0;

    for (x=2; x<=13; x++)
    {

    diceone = rand() % 6;
    dicetwo= rand () %6;
    sum = diceone + dicetwo;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21