C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP! confused on homework Posted by jwood44 on 3 Apr 2012 at 4:49 PM
question:
Write a program that simulates the rolling of two dice. The program should use rand to roll the
first die, and should use rand again to roll the second die. The sum of the two values should
then be calculated. [Note: Since each die can show an integer value from 1 to 6, then the sum of
the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 the least
frequent sums.]
The following Figure shows the 36 possible combinations of the two dice. Your program should
roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each
possible sum appears. Print the results in a tabular format. Also, determine if the totals are
reasonable; i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should
be 7.

output in form in table with colums:
Sum Total Expected% Actual%


heres what I have so far, help please:(

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RANDOMIZER          rand((unsigned)time(0))
#define ROLLS               (long)(36000)
#define DIES                2
#define SIDES               6

int roll(int roll2) {
        int i, total = 0;
        if (roll2 < 0) roll2 = 0;
        for(i = 0; i < roll2; i++)  
                total += (rand() % SIDES);
        total += roll2;
        return total;
        }

void print_bar(int length)  {
        while(length)   {
                printf("-");
                length--;
                }
        printf("\n"); 
        }

int main()  {
        long buckets[(SIDES * DIES) + 1];
                                           
                                           
        long i;
        long percentile;

        RANDOMIZER;
        
        for(i = 0; i <= (SIDES * DIES); buckets[i++] = 0);

        for(i = 0; i < ROLLS; i++)
                buckets[roll(DIES)]++;  /* WOW! */
        
        printf("%dd%d\n", DIES, SIDES);
        printf("\nSum\t#occ\tPercentile\n");
        for(i = DIES; i <= (SIDES * DIES); i++)    {
                percentile = (buckets[i] * 10000) / ROLLS; 

/* Integer Math! */
                printf("%li\t%li\t%3.2f%%\t", i, buckets[i], (double)(percentile) / 100.00);
                print_bar(percentile / 100);
                }
        return 0;
        }



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.