Thread: Need help!!!

  1. #1
    Registered User
    Join Date
    May 2008

    Need help!!!

    so i was given the assignment where i am suppose to use the random number generator to simulate dice throws and also calculate the probability of the dice landing on a specific facevalue.

    Here is my program....

    /* put this in a program */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define MAX 6 /* symbolic constant */
    int main()
     int faceValue = 0 ;
     float probability[MAX] = {0} ;
     int run = 0 ;
     int counter = 0 ;
     srand(time(0));/*random number generator*/
     printf("How many times do you want to throw the die? \n");
     faceValue = rand() % 6 + 1;
        case 1:
           faceValue == 1;
        case 2:
           faceValue == 2;
        case 3:
           faceValue == 3;
        case 4:
           faceValue == 4;
        case 5:
           faceValue == 5;
        case 6:
           faceValue == 6;
       for (counter =1; counter <= MAX; counter++)
           printf(" The probability for number %d is %0.2f %\n", counter, (probability[counter]/12)* 100);
          return 0;

    the problem is when i compile i don't get the calculations correct for the probability....could someone please point me to the write direction cause i do not no where to go from here

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    faceValue == 6;
    This is comparison. Use = for assignment.
    And array indexes run from 0 to elements - 1 (so your array is 0...5, not 1...6).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Your switch statement makes no sense. The variable being tested is faceValue, so let's say faceValue is 5:

    Now at case 5, you compare faceValue to 5 ?? Of course it's five, that's how your code got to this line!

    And please DON'T assign faceValue to 5 either, because faceValue ALREADY IS five (or whatever number).

    You need another variable to be altered in the switch statement, not faceValue.

    2) You need a for loop wrapped around the switch statement, starting right after the prompt "How many times do you want to throw the dice?"

    for(i = 0; i < throws_wanted; i++)   {
       //other code for the throw here
       //switch statement here
    } //end of for 
    //print up probabilities for all the throws here.

Popular pages Recent additions subscribe to a feed