11111111
This is a discussion on c program within the C Programming forums, part of the General Programming Boards category; 11111111...
11111111
Last edited by ordaz91; 04-25-2010 at 11:40 PM.
Did you try compiling this code?
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
please..help///me ..at least tell me if iam going in the right direction..or if all the porgram is wrong..
Where are you reading the birthdays for each guest at the party?
should be on birtdays..but i dont think is right..can you tell me waht i can do//??
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]);
Do you get what is going on now?
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); }
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.
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.
oh ok..thanks.... and hte rest.. is ok.. or is wrong/.?
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.
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.
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.