Thread: Probabilities and Arrays

  1. #1
    Registered User
    Join Date
    Mar 2010

    Probabilities and Arrays

    Hey, i'm having trouble with having to simulate a user entered number of groups and user entered number of group members. I am supposed to have a function with the prototype int group(int n) that will simulate one group at a time with n members. I have to assign each member with a random birthday and have the program check to see if two of members have the same birthday. If one group has two members with the same birthday, the function returns 1. If all the birthdays at one party are different, the function returns 0.
    Then main() counts the number of 1's, divides it by the number of parties and prints out a percentage.

    Here's what I have so far, it assigns each member with a bday,but i'm not sure how to make a loop if two or more members have the same bday and return a 1 or 0...

    #include <stdlib.h>
    #include <time.h>
    int group(int g, int p);
          int members;
          long int groups, samebday;
          group(members, groups);
          return 0;
          int group(int g, int p)
          int members, count, i, j, bdays[40];
          long int groups;
          printf("Enter the number of groups: ");
          scanf("%ld", &groups);
          printf("Enter the number of members: ");
          scanf("%d", &members);
          for (j = 1; j <= groups; j++)
           for(i = 0; i < members; i++)
                 bdays[i] = 1 + rand() % 365;
    Any help will be appreciated, thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You can go through all the pairs (0&1, 0&2, 0&3, ..., 0&39, 1&2, 1&3, 1&4, .........) and check if they're the same; if they are, set some flag to 1.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Welcome to the forum, Simplicity.

    There is a slick way of doing this, called distribution counting or counting sort or just "binning". Anyway, whatever the name, here's how it works:
    #include <stdio.h>
    int main() {
      int i; 
      int num[10] = { 4, 1, 4, 12, 3, 8, 12, 4, 9, 4 };
      int count[13] = { 0 };  
      for(i = 0; i < 10; i++)
      for(i = 0; i < 13; i++) {
        if(count[i] > 1)
          printf("\n%2d: %2d", count[i], i);
      printf("\n\n\t\t\t     press enter when ready");
      return 0;
    When you run it, it shows you have 4 4's and 2 12's, in your num[] array.

    Note that the size of cout[] array MUST be able to go to the highest value in num[]. If the highest number in num was 126, then count would have to have at least 127 elements in it (the +1 is because arrays in C start with 0's, not 1's).

    Back to your program. After every party, you run this bit of code, and it will quickly tell you which parties have guests with duplicate bdays.

    Remember to reset the count array to all zero's, in a for loop, before the next party.

    This is a good programming trick to remember, btw.

Popular pages Recent additions subscribe to a feed
Website Security Test