Help with some array stuff

This is a discussion on Help with some array stuff within the C Programming forums, part of the General Programming Boards category; Basically, 36000 rolls of the dice. I want to output the sum of the two dice (done) the frequency of ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    Help with some array stuff

    Basically, 36000 rolls of the dice. I want to output the sum of the two dice (done) the frequency of each sum (done) and the expected along with actual. Both of the later should be in percentage. I know how they would be calculated. Actual is freq/36000 and expected is just stats stuff (1k, 2k, 3k, 4k, 5k, 6k, 5k, 4k, 3k, 2k, 1k).

    When I try to do freq/36000 I just can't get it to work.

    Any suggestions for getting both of these other needed values into arrays and printed?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define SIZE 13
    
    int rollDice(void);
    
    int main ()
    {
    	int roll, sum, freq[SIZE] = {0};
    	float expected[SIZE] = {0}, actual[SIZE] = {0};
    
    	srand(time(NULL));
    
    	for (roll= 1; roll <= 36000; roll++)
    	{
    		sum = rollDice();
    		++freq[sum];
    	}
    	printf("%s%10s%12s%12s\n", "Sum", "Total", "Expected", "Actual");
    
    	for (sum =2; sum <= SIZE -1; sum++)
    	{
    		printf("%2d%10d%12f%12f\n", sum, freq[sum], expected[sum], actual[sum]);
    	}
    	return 0;
    }
    
    int rollDice(void)
    {
       int die1; 
       int die2;   
       int worksum; 
    
       die1 = 1 + ( rand() % 6 ); 
       die2 = 1 + ( rand() % 6 ); 
       worksum = die1 + die2;     
       
       return worksum; 
    }
    Last edited by khskenny; 11-15-2006 at 09:04 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Assign some values to the arrays you are trying to print (expected and actual)?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by Dave_Sinkula
    Assign some values to the arrays you are trying to print (expected and actual)?
    That is where I am having some trouble

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Well you'll have to elaborate on what you mean by expected and actual...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Actual is going to be the frequency of each sum / 36000

    Expected is as stated above. The statistical chance of each pair of dice.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by khskenny
    Actual is going to be the frequency of each sum / 36000

    Expected is as stated above. The statistical chance of each pair of dice.
    Perhaps you should increment an array member when its result occurs?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by Dave_Sinkula
    Perhaps you should increment an array member when its result occurs?
    Seems to work. I just had to sit and stare at it for a while. Just need to tweak a bit to get %

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    #define SIZE 13
    
    int rollDice(void);
    
    int main ()
    {
    	int roll, sum, freq[SIZE] = {0};
    	float expected[SIZE] = {0,0,1,2,3,4,5,6,5,4,3,2,1}, actual[SIZE] = {0};
    
    	srand(time(NULL));
    
    	for (roll= 1; roll <= 36000; roll++)
    	{
    		sum = rollDice();
    		++freq[sum];
    		++actual[sum];
    	}
    	printf("%s%10s%12s%12s\n", "Sum", "Total", "Expected", "Actual");
    
    	for (sum =2; sum <= SIZE -1; sum++)
    	{
    		printf("%2d%10d%12f%12f\n", sum, freq[sum], expected[sum]/36, actual[sum]/36000);
    	}
    	return 0;
    }
    
    int rollDice(void)
    {
       int die1; 
       int die2;   
       int worksum; 
    
       die1 = 1 + ( rand() % 6 ); 
       die2 = 1 + ( rand() % 6 ); 
       worksum = die1 + die2;     
       
       return worksum; 
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    I've tried a few different ways to display these computed fields as percentages with no luck. Any tricks?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something like this?
    Code:
          printf("%2d%10d%12f%%%12f%%\n", sum, freq[sum], 
                 100*expected[sum]/36, 100*actual[sum]/36000);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by Dave_Sinkula
    Something like this?
    Code:
          printf("%2d%10d%12f%%%12f%%\n", sum, freq[sum], 
                 100*expected[sum]/36, 100*actual[sum]/36000);
    That works. Thanks

    My instructor insisted that only one "%" was needed to make the symbol appear. Obviously two is the trick.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >My instructor insisted that only one "%" was needed to make the symbol appear.
    % is a special character in printf format strings. Your instructor is either confused or shouldn't be teaching C at all.
    My best code is written with the delete key.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Your instructor is either confused or shouldn't be teaching C at all.
    Or maybe he was talking about something else but not printf
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Or maybe he was talking about something else but not printf
    I think that falls under "confused".
    My best code is written with the delete key.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Prelude
    >Or maybe he was talking about something else but not printf
    I think that falls under "confused".
    I thought about possibility of misinterpretation of tutors words...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > My instructor insisted that only one "%" was needed to make the symbol appear.
    Which would be fine when using fputs(), but in printf(), two is needed.

    But then again, fputs() only outputs fixed strings, whereas printf() can do lots of conversion and formatting.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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