Computer Science Canada

Help me with this problem plz...

Author:  KillCloud [ Sat May 27, 2006 12:22 pm ]
Post subject:  Help me with this problem plz...

Please help me to write a program that does the following:
Suppose that people enter an empty room until a pair of people share a birthday. Write a program to find out how many people will have to enter before there is a match. Assume birthdays are uniform random integers between 0 and 364.
thanks

Author:  jamonathin [ Sat May 27, 2006 1:04 pm ]
Post subject: 

Well, it's obvious what time of the year it is - [FP] Time.

The members of CompSci.ca do not do homework assignments for other people, and it's obvious that it's for school.

Now hopefully for your sake you have been paying attention in class and you can attempt a program of your own. Once you manage to accomplish that, then we can assist you in your program. Untill then try something of your own and refer to the Turing Walkthrough for explanations on commands.

Author:  upthescale [ Sat May 27, 2006 2:39 pm ]
Post subject: 

Ok, terrible coding, but it still does the job...the delay is at the bottom set to '30' so the years and births will go fast, but it will take shorter time to find a match! play with the delay to apeed it or slow it doen

code:

%May not be the best coding, but it still works
%Nevermind, TERRIBLE coding
setscreen ("graphics:600;500;position:truemiddle;center;nobuttonbar;title:Matching Birthdays;offscreenonly")
var birth, birthgirls : int
var jan, feb, mar, apr, may, june, july, aug, sep, oct, nov, dec : int
var year, yeargirls : int
var font : int := Font.New ("arial:15:underline")
loop
    colorback (10)
    cls
    %Randoms
    randint (birth, 1, 31)
    randint (year, 1, 12)
    randint (birthgirls, 1, 31)
    randint (yeargirls, 1, 12)
    %Randoms
    %Boys
    if year = 1 then
        locate (4, 10)
        put "January"
    end if
    if year = 2 then
        locate (4, 10)
        put "February"
    end if
    if year = 3 then
        locate (4, 10)
        put "March"
    end if
    if year = 4 then
        locate (4, 10)
        put "April"
    end if
    if year = 5 then
        locate (4, 10)
        put "May"
    end if
    if year = 6 then
        locate (4, 10)
        put "June"
    end if
    if year = 7 then
        locate (4, 10)
        put "July"
    end if
    if year = 8 then
        locate (4, 10)
        put "August"
    end if
    if year = 9 then
        locate (4, 10)
        put "September"
    end if
    if year = 10 then
        locate (4, 10)
        put "October"
    end if
    if year = 11 then
        locate (4, 10)
        put "November"
    end if
    if year = 12 then
        locate (4, 10)
        put "December"
    end if
    %Boys
    %%Girls
    if yeargirls = 1 then
        locate (4, 30)
        put "January"
    end if
    if yeargirls = 2 then
        locate (4, 30)
        put "February"
    end if
    if yeargirls = 3 then
        locate (4, 30)
        put "March"
    end if
    if yeargirls = 4 then
        locate (4, 30)
        put "April"
    end if
    if yeargirls = 5 then
        locate (4, 30)
        put "May"
    end if
    if yeargirls = 6 then
        locate (4, 30)
        put "June"
    end if
    if yeargirls = 7 then
        locate (4, 30)
        put "July"
    end if
    if yeargirls = 8 then
        locate (4, 30)
        put "August"
    end if
    if yeargirls = 9 then
        locate (4, 30)
        put "September"
    end if
    if yeargirls = 10 then
        locate (4, 30)
        put "October"
    end if
    if yeargirls = 11 then
        locate (4, 30)
        put "November"
    end if
    if yeargirls = 12 then
        locate (4, 30)
        put "December"
    end if
    %%Girls
    locate (7, 10)
    put birth
    locate (7, 30)
    put birthgirls
    locate (1, 1)
    Font.Draw ("Boys", 0, maxy - 20, font, red)
    Font.Draw ("Year", 0, maxy - 70, font, red)
    Font.Draw ("Birth", 0, maxy - 120, font, red)
    Font.Draw ("Girls", 210, maxy - 20, font, red)
    Font.Draw ("Year", 210, maxy - 70, font, red)
    Font.Draw ("Birth", 210, maxy - 120, font, red)
    if birth = birthgirls and year = yeargirls then
        exit %End Loop
    end if
    locate (1, 8)
    delay (30)
    View.Update
end loop
Font.Draw ("We Have a Match", 0, maxy - 320, font, red)

Author:  KillCloud [ Sat May 27, 2006 3:11 pm ]
Post subject: 

thx for your reply
however, im thinking of a easier way, where the numbers from 0 to 364 can represent the specific dates (doesnt matter which year they were born in)
and i have problems with the counting part, where i have to output the number of people who had already entered the room when there is a match of the same birthday
here is what i have so far. it's messed up all right
code:

var Max := 500
var arrayCount := 1
var x : int
var birthday : array 1 .. Max of int
for i : 1 .. Max
    randint (x, 0, 364)
    birthday (arrayCount) := x
    var found : boolean := false
    for j : 1 .. arrayCount - 1
        if birthday (arrayCount) = birthday (j) then
            found := true
            exit
        end if
    end for
    if not found then
        arrayCount := arrayCount + 1
    end if
end for
put arrayCount
[/code]

Author:  Clayton [ Sat May 27, 2006 7:04 pm ]
Post subject: 

Turing:

var birthday : flexible array 1 .. 1 of int
var flag : boolean := false
loop %because we dont know how many people will come until a match is made
    birthday (upper (birthday)) := Rand.Int (1, 355)
    for i : 1 .. upper (birthday)
        put birthday (i), " " ..
    end for
    Time.DelaySinceLast (500)
    if upper (birthday) >= 2 then
        for j : 1 .. upper (birthday) - 1
            if birthday (upper (birthday)) = birthday (j) then
                flag := true
                exit when flag
            end if
        end for
    end if
    new birthday, upper (birthday) + 1
    cls
end loop
put upper (birthday), " kids showed up until they had a common birthday"


your program should look something like that you mostly have it

Author:  MysticVegeta [ Sat May 27, 2006 8:00 pm ]
Post subject: 

I dont understand why you have the if structure and the for loop to output brithday, also you need 1 extra line before the end loop, "exit when flag"..

Turing:
var birthday : flexible array 1 .. 0 of int
var flag : boolean := false
loop
    new birthday, upper (birthday) + 1
    birthday (upper (birthday)) := Rand.Int (0, 365)
    put birthday (upper (birthday)), " " ..
    for x : 1 .. upper (birthday) - 1
        if birthday (x) = birthday (upper (birthday)) then
            flag := true
        end if
    end for
    exit when flag
end loop
put skip, upper (birthday), " kids showed up until they had a common birthday"

Author:  KillCloud [ Sat May 27, 2006 8:20 pm ]
Post subject: 

thanks a lot
that really helps, i really appreciate that Very Happy


: