Probablility with arrays

Hello everyone! I am new to this forum (and C programming) but I hope that I will be able to master C help anyone out with problems in the coming months. But I am having trouble with my own program for an assignment.
I am asked to simulate a user entered number of birthday parties and user entered number of guests at each party. I am supposed to have a function with the prototype int party(int n) that will simulate one party at a time with n guests. I have to assign each guest with a random birthday and have the program check to see if two of the guests have the same birthday. If one party has two guests 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.
I got a little bit into the program and got stuck.

Right off the bat, I don't like float data types for parties and bdays. No one has 1/4th of a party, or .01 bdays.

Unless there is a specific need for floats or doubles, don't use them! They have their own subtle problems.

You will need two nested loops:

for(each party) {
for(each guest) {
assign their bday. maybe put it also into an array of int's
}
now all guests have bdays, see if any of them are duplicate days
maybe sort them out, and they will be adjacent in the sorted list?
if (number of duplicates you find > 0)
return 1
else
return 0
}

That kind of idea.

And welcome to the forum! :)

P.S. If you make your outer loop inside the party function, aren't you going to be stuck trying to return something like your assignment requires?

I would move those questions, and that loop, to main, and use party() as a call, inside that loop.

Right off the bat, I don't like float data types for parties and bdays. No one has 1/4th of a party, or .01 bdays.

Unless there is a specific need for floats or doubles, don't use them! They have their own subtle problems.

well I'm using the float for parties only because I am simulating 1,000,000 parties. I'm not sure why I put bdays there. Must have accidentally left it there when I was trying different things with the program.

Making the array is one of my problems. I'm not quite sure how to set the value inside the array to the number entered by the user. Every time I try something the compiler gives me an error. And I am also unsure how to scan the random number into the array.

Making the array is one of my problems. I'm not quite sure how to set the value inside the array to the number entered by the user. Every time I try something the compiler gives me an error. And I am also unsure how to scan the random number into the array.

You make an array like any other variable, but add [SizeYouWant] after the name of the array. This is a static array - not the only kind, btw.

Use the index to the array's elements, to set your bday numbers up

And on further thought, the easier way to do this is with "counting sort".

Follow me closely:

In the array of numbers {1,3,1,4}, I want to know if there are any duplicate numbers.

Sorry I didnt reply sooner, I had some personal things to take care of.
I understood a little bit of what you said but I got lost after you said "Follow me closely:" haha. I updated the code a little bit and got it to assign each guest with a birthday.

Ok, I understood it a little bit more. I went and edited the code to add it. It compiled fine but when it reaches the loop, the program stops running and wont respond. Can you check what's wrong with it?

It was crashing because you were running outside the boundaries of dupes. It has to have 366 elements (0-365). Even though there is no day 0, the arrays in C always start with zero, so that explains the one day extra in this size (I hope). ;)

The program is a bit odd, because the party() function does everything. I did not change that.

I took the program you made and edited it so the user can enter the parties and guests but it is printing out every party and a random number way more than 365. I'm not sure what it is doing. It's doing something like:
...
30: 1534
31: 1296
32: 840
...

Lol, my bad. I talked to my professor and he helped me out a little bit with the little time he had. I cleaned up my code so it returns 1 or 0 and party() doesn't do everything but it's not printing out the number of same and different bdays. Can you spot what's wrong with it?

All I need to do now is make a loop to check if two+ guests have the same bday and have it return 1 or 0.

I optimized a bit calculation of duplicates - main idea is that when guest count is less than 365 -> we should iterate through guests and not through year days. In that case we win some CPU cycles. Theoretically speed-up factor would be 365/guestCount. If you simulate 1,000,000 parties then this is important thing :)
Code:

Ok, well the code is doing exactly what i want it to do except for one minor thing. It is always returning 1. I'm thinking there is something wrong with the for loop when I am comparing bday and bday[j].

This loop makes no sense at all. Your first step is to format the entire code because it's really hard to see what's going on. I reformatted the loop and it still is senseless.

i and j both start at 0. So your IF is always true since bdays[0] will always be equal to itself.
If the IF wasn't true though, you execute i - 1 which does nothing. Then you test if i is zero, which it is, so you return.

Ok, well the code is doing exactly what i want it to do except for one minor thing. It is always returning 1. I'm thinking there is something wrong with the for loop when I am comparing bday and bday[j].

But in the party() function, there is no need for nested loops. There is only "guests" data from one party at a time, present.

So the for loop with j as the iterator, should be removed, completely.

...//your other code in party() up here,
for(i = 0; i < g; i++) //assign the bdays random day numbers
{
bdays[i] = 1 + rand() % 365;
}
//let the previous loop finish, completely
//now start the loop to test if there is a duplicate
/*
Can't do the test, because you removed the code that would do the counting sort!
Crap!
*/
}

LMat, you need to find someone that you can trust to follow their advice on this matter, and follow it.

Yeah, maybe you're right. My professor wants me to have the program check if any birthdays are the same without assigning all of the guests with b-days first, unless all the b-days are different by the time it reaches the end (which would return 0).
Well, thanks for all your help. I probably wouldn't have even gotten this far if it wasn't for you.

Ok, well the code is doing exactly what i want it to do except for one minor thing. It is always returning 1. I'm thinking there is something wrong with the for loop when I am comparing bday and bday[j].

??
But i dont like brute-force search as I said before, because it will perform slow. So you should try to search for duplicates as in my example before OR at least:
1. Sort bdays array with QSORT function.
2. Then search that bdays array in acending order with only one loop, something like

??
But i dont like brute-force search as I said before, because it will perform slow. So you should try to search for duplicates as in my example before OR at least:
1. Sort bdays array with QSORT function.
2. Then search that bdays array in acending order with only one loop, something like

In this case sorting array + 1 loop should perform faster than performing 2 loops as in your code. But ... as you wish.

Good luck!

As it turns out, the assignment requires LM to NOT have duplicate bdays checked after they've been assigned to all the guests.

I thought it would simplify the program a bit to do it that way. Also, it just seemed odd to check bdays BEFORE they were assigned to the guests.

Bottom line is, it will be done (with the next version), using a very fast counting sort look up, BEFORE the bdays are assigned. So it will be way faster than Quicksort or any other sorting algorithm.

If you haven't run across the simplicity and speed of counting sort, you might want to stay tuned. It's a very worthwhile trick to know. You can't use it all the time, but when you can - it sets the guitars on fire! ;)