Dice Program

This is a discussion on Dice Program within the C Programming forums, part of the General Programming Boards category; Hello I am trying to make a program that "rolls" 5 dice randomly and computes the sum and mean. I ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Dice Program

    Hello I am trying to make a program that "rolls" 5 dice randomly and computes the sum and mean. I have the program running, but it is not working the way I want it to.

    The problem I am guessing is in the printf statements where I want to assign that value that is randomly chosen to a variable and then do the math with the variables. Even though it still runs, I can not get the right sum and mean.

    Is it possible to do the calculations I want the program to do with the way I am specifying them; or do I need to write a separate function that assigns the random values and then proceeds from there? Thanks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define MAX_VAL 6
    #define MIN_VAL 1
    
    
    int
    main(void)
    {
          srand(time(NULL));
          printf("Here are five rolled dice:");
    
          int a,b,c,d,e;
    
          printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, a);
    
          printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, b);
    
          printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, c);
    
          printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, d);
    
          printf("%i \n", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, e);
    
          int sum;
          double average;
    
          sum = a + b + c + d + e;
    
          average = (a + b + c + d + e)/5;
    
          printf("The sum of the dice is %i, and their average is %d.1", sum, average);
    Last edited by M_A_T_T; 01-29-2013 at 10:10 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, a);
    How does this assign a value to a?

    > printf("The sum of the dice is %i, and their average is %d.1", sum, average);
    Read the printf manual page on what format is used to print a double.

    What compiler are you using?
    Try using one which can warn you about a load of dumb stuff.
    Code:
    $ gcc -W -Wall -Wextra bar.c
    bar.c: In function ‘main’:
    bar.c:17:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c:19:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c:21:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c:23:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c:25:7: warning: too many arguments for format [-Wformat-extra-args]
    bar.c:34:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
    bar.c:17:13: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
    bar.c:19:13: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]
    bar.c:21:13: warning: ‘c’ is used uninitialized in this function [-Wuninitialized]
    bar.c:23:13: warning: ‘d’ is used uninitialized in this function [-Wuninitialized]
    bar.c:25:13: warning: ‘e’ is used uninitialized in this function [-Wuninitialized]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    Also....you need to learn how prinf is supposed to be....the following is WRONG!!!
    Code:
    printf("The sum of the dice is %i, and their average is %d.1", sum, average);
    no matter what you do here, you will always get .1 at the end...

    ALSO

    Code:
    average = (a + b + c + d + e)/5;
    your adding up ints, dividing by ints, then assigning to a double....

    means you will never get the average, you will only get a whole number.

    need to learn int/double math
    Last edited by Crossfire; 01-30-2013 at 09:07 AM.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int num,loop,total,mean;
        srand((unsigned)time(NULL));
        
    for(loop=0;loop<5;loop++)
    {
        num=rand()%6+1;
        printf("Dice roll #%d = %d\n",loop+1,num);
        total+=num;
    }
    
        mean=total/5;
        printf("\nSum = %d.\n",total);
        printf("Average = %d.\n",mean);
    
    return(0);
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,434
    Quote Originally Posted by LTA85 View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int num,loop,total,mean;
        srand((unsigned)time(NULL));
        
    for(loop=0;loop<5;loop++)
    {
        num=rand()%6+1;
        printf("Dice roll #%d = %d\n",loop+1,num);
        total+=num;
    }
    
        mean=total/5;
        printf("\nSum = %d.\n",total);
        printf("Average = %d.\n",mean);
    
    return(0);
    }
    You neglected to initialize "total."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Roll Program
    By HeidiPagel in forum C Programming
    Replies: 7
    Last Post: 12-13-2010, 09:39 AM
  2. Blackjack And Dice Program
    By arda1404 in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2010, 11:37 AM
  3. Dice program getting 0s
    By domezy in forum C Programming
    Replies: 4
    Last Post: 10-27-2009, 03:52 AM
  4. Dice Rolling Program help
    By courtesan in forum C Programming
    Replies: 3
    Last Post: 08-17-2005, 03:14 PM
  5. rolling dice program
    By sunny2005 in forum C Programming
    Replies: 20
    Last Post: 03-21-2005, 03:45 PM

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