Need help!!!

This is a discussion on Need help!!! within the C Programming forums, part of the General Programming Boards category; so i was given the assignment where i am suppose to use the random number generator to simulate dice throws ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    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 */
    Code:
    #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");
     scanf("%d",&run);
    
     faceValue = rand() % 6 + 1;
    
     switch(faceValue){
        case 1:
           faceValue == 1;
           break;
        case 2:
           faceValue == 2;
           break;
        case 3:
           faceValue == 3;
           break;
        case 4:
           faceValue == 4;
           break;
        case 5:
           faceValue == 5;
           break;
        case 6:
           faceValue == 6;
           break;
         }
    
       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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Code:
    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
    Posts
    8,868
    1)
    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?"


    Code:
    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

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