c program

This is a discussion on c program within the C Programming forums, part of the General Programming Boards category; 11111111...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    10

    Post c program

    11111111
    Last edited by ordaz91; 04-25-2010 at 11:40 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Did you try compiling this code?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    yes... is compiles. but does not gives me the right anwer.... also.. i need..to get the porbability..but idont know..where to put it... so i can get it right

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    please..help///me ..at least tell me if iam going in the right direction..or if all the porgram is wrong..

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Where are you reading the birthdays for each guest at the party?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    should be on birtdays..but i dont think is right..can you tell me waht i can do//??

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    bdays = 1 + rand() % 365;
    printf("%d, %d \n", i, bdays);

    Change that to:

    bdays[i] = 1 + rand()%365;
    printf("%d, %d\n",i,bdays[i]);

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Do you get what is going on now?

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    this is my new..modeified code.. that i was working on... but still doesnt work can you tell me waht is wrong...with it.. pleas


    Code:
    #include<stdlib.h>
    #include <time.h>
    int party(int n);
    main()
    {
    int guests,j,i,k, prob,n;
    long int parties, samebday, diffbday, count, result;
    printf("Enter the number of parties: ");
    scanf("%ld", &parties);
    printf("Enter the number of guests: ");
    scanf("%d", &guests);
    printf(" the probability that 2 guests have the same birthday is: %d", prob);
    srand(time(NULL));
    samebday = 0;
    diffbday = 0;
    for(i=1;i<=k;i++)
         {
           prob = prob*((double) (n-i+1) / n);
           prob = 1.0- prob;
         }
    
    for (j = 1; j <= parties; j++)
           {
    	party(guests);
    	count = party(result);
    	printf("%d \n", count);
    	if (count == 1)
    	samebday++;
    	if (count == 0)
    	diffbday++;
         }
      printf("%d \n", samebday);
      printf("%d \n", diffbday);
    return 0;
    
    }
    
    int party(int n)
    {
      int i,k,match, bdays[i];
      long int parties, hold;
      match = 0;
      for(i = 0; i < n; i++)
      {
        bdays[i] = 1 + rand() % 365;
          printf("%d, %d \n", i, bdays[i]);
          for(k=0;k>=i;k --)
           {
            if(bdays[k] == bdays[i])
              match=1;
    
           }
       }
           return(match);
    }

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No. Your modification shows that you copied this from somewhere and don't have the slightest clue what you are doing. I am not going to edit this for you unless you explain what your code does in detail.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Okay, so why the hell are you declaring the array in your party function as size i?

    Give it a meaningful value like 50 or 100 or however many guests you have. In this case, I'm assuming n.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    oh ok..thanks.... and hte rest.. is ok.. or is wrong/.?

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    10
    there are..hints...my teacher...sent me by email..but still i dont get it



    • Write a function with prototype int party(int n) that will simulate one party with n guests. The function will return 1 if two of the guests have the same birthday, and 0 if all guests have different birthdays (i.e., born on the same day of the year, not necessarily in the same year). To be more precise, the function party should return 1 if any two birthdays are the same, regardless of whether any other guests have the same birthday. For example, if two pairs of guests have the same birthday, the function returns 1. If three guests have the same birthday, the function returns 1. And so forth. The only time the function returns 0 is if all the birthdays are different.
    • In the function party, use the function rand to generate the random birthdays. (A birthday may simply be a number between 1 and 365. You can ignore leap years).
    • Call srand once at the beginning of main. (Do not call srand in the function party. If you do, and pass it the same seed every time, your function will simulate the same party every time it is called.)
    • In the function party, use an int array to record birthdays that have been assigned. Give some thought to how large the array should be, and how you will record birthdays. There is more than one reasonable way to do this.
    • Aside: If you understand probability theory, you might be able to answer this question analytically. For many other questions of this sort, however, simulation is the only practical way to find an answer.

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    for(k=0;k>=i;k --) this is wrong.

    It needs to read (for k = i; k>=0;k--). The rest seems right. Give it a try.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Actually one more thing. Delete all the variables you are not actually using, they are just cluttering up the code.

    Also, make sure you return 1 where you currently set your match to 1 instead of just setting the variable to 1. That's what the prof. was suggesting. If you find two that match return 1, you don't even care about the rest. If not, the for will finish and it will return 0 at the bottom of the function.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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