C Programming Help

By LMat619 ·
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.

I got most of the code done, but I am stuck where I am supposed to check if any two birthdays are the same. I cannot think of a way to write that code and make it work. Any help would be greatly appreciated.

#include <stdlib.h>
#include <time.h>
int party(int g);
int guests, j;
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);
samebday = 0;
diffbday = 0;
for (j = 1; j <= parties; j++)
/*count = party(result);
printf("%d \n", count);
if (count == 1)
if (count == 0)
//printf("%d \n", samebday);
//printf("%d \n", diffbday);
return 0;

int party(int g)
int i, j, bdays[g];
long int parties, hold;
for(i = 0; i < g; i++)
bdays = 1 + rand() % 365;
printf("%d, %d \n", i, bdays);
//If any 2 match, return 1. If none match, return 0

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

simple but crude

by oldbaritone In reply to C Programming Help

first, in your party function, you probably need to index bdays in the loop when building the array - bdays{i} = ...

EDIT: Had to change square brackets to curly brackets because {i} is a tag if it has square brackets. So maybe the first comment doesn't apply, and change the curlys below to square...

int declarations, add
int k, match;
match = 0;

then in the insert code here,
for (k=i-1; k>=0; k--) if (bdays{k} == bdays{i}) match=1;

and instead of

you'll want to
party=match [or maybe return(match)]

Yes everyone, I know I'm doing the homework for them. I'm bored today. ;-)

and for the old timers,
for (--k=i; (!(match|=(bdays{k}==bdays{i})))&& ((k--)>0); );
would run faster because the loop drops out as soon as a match is found, but LMat might not be able to figure it out, and the prof would surely question it. Academe seems to hate body-less "for" loops, even though they run faster. (My age is showing - at 2 or 3 Ghz, everything is fast)...

And LMat, I'd suggest using more meaningful names for everything - using loop counter names like i,j,k is a sure way to get confused and make the code difficult to debug - and it's really easy to re-use the same counter within the loop, especially in a loop-within-loop situation like this. Then the program goes compute-bound. But you'll learn... and C is pretty much an anachronism any more, anyhow. Everyone wants OOP now.

Collapse -


by LMat619 In reply to simple but crude

OMG THANK YOU SOOOO MUCH!! You are seriously my hero. Now that I look at it, it seems so freaking simple I should have gotten it a long time ago. YOU ROCK!!

Collapse -

oooh a thumb

by .Martin. In reply to HERO!!

I like this guy already

Collapse -

"hitchin a ride"

by oldbaritone In reply to oooh a thumb

the "Vanity Fair" version -



Back to Windows Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums