Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 PLZ HElp Randomly Generating a Word From an Array
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
josh




PostPosted: Tue Feb 10, 2004 7:53 pm   Post subject: PLZ HElp Randomly Generating a Word From an Array

I am a real Newbi and am trying to self - teach turing. I made this program to practice what I know (it is pretty lam) and I am having trouble figuring ouyt how to get it to generate a random anme from the array that I have provided. Any help with this would be greatly apreciated.

code:

%Variables used in program
var firstname : string
var lastname : string
var age : int
var yn : string
var myage : int
var myfname: string := ""
var mylname: string
var randomfname: int:= 0


%Array for random generation of names
var myfname: array 1..5 of string: init("Josh", "Adam", "Michael", "Steve", "Joanne")




mylname:= "Rosen"

%Random for age
randomize
randint (myage, 5, 30)

%Get first name
put "Hello my first name is ", myfname, "."
put "What is your first name?"
get firstname

%Get last name
put "My last name is ", mylname, "."
put "What is your last name?"
get lastname

%Get age
put "I am ", myage, " years old"
put "How old are you?"
get age

%Verify
put "Let me see if I got this right."
put "Your name is ", firstname
put "Your last name is ", lastname
put "and you are ", age, " years old."
put "is this correct? (y/n)"

get yn

%Make comparison
if yn = "y" then

    if age = myage then
        put "We are the same age!!!!"
    end if
    if age > myage then
        put "You are ", age - myage, " years older then me!!"
    end if

    if age < myage then
        put "You are ", myage - age, " years younger then me!!"
    end if


end if

if yn = "n" then
    put "oh sorry i guess we better start again."
end if

if myfname = firstname and mylname = lastname then
    put "What a coincidence, you an I have the same first and last name!!!"
end if

if myfname = firstname and mylname not= lastname then
    put "Hey, we have the same first name"
end if

if mylname = lastname and myfname not= firstname then
    put "Hey we have the same last name"
end if

Sponsor
Sponsor
Sponsor
sponsor
Paul




PostPosted: Tue Feb 10, 2004 7:58 pm   Post subject: (No subject)

I don't think you need to put randomize, as for generating random names, you have to generate the random subscript for the array:
code:

var a: 1..5
var name: array 1..5 of string:= init("Josh", "Adam", "Michael", "Steve", "Joanne")
randint (a, 1, 5)
put name(a) %<--- thats your random name
josh




PostPosted: Tue Feb 10, 2004 8:04 pm   Post subject: (No subject)

I jsut tried what u said and it does not work. What I want to do is have it randomly generate the first name each time (it used to always be Josh) and the variable myfname is supposed to be the randomly generated first name.
josh




PostPosted: Tue Feb 10, 2004 8:07 pm   Post subject: (No subject)

Thsi was my original code and I wanted to make it more interesting by generating arandom firt name:

code:

%Variables used in program
var firstname : string
var lastname : string
var age : int
var yn : string
var myage : int
var myfname: string
var mylname: string

myfname:= "Josh"
mylname:= "Rosen"

%Random for age
randomize
randint (myage, 5, 30)

%Get first name
put "Hello my first name is ", myfname, "."
put "What is your first name?"
get firstname

%Get last name
put "My last name is ", mylname, "."
put "What is your last name?"
get lastname

%Get age
put "I am ", myage, " years old"
put "How old are you?"
get age

%Verify
put "Let me see if I got this right."
put "Your name is ", firstname
put "Your last name is ", lastname
put "and you are ", age, " years old."
put "is this correct? (y/n)"

get yn

%Make comparison
if yn = "y" then

    if age = myage then
        put "We are the same age!!!!"
    end if
    if age > myage then
        put "You are ", age - myage, " years older then me!!"
    end if

    if age < myage then
        put "You are ", myage - age, " years younger then me!!"
    end if


end if

if yn = "n" then
    put "oh sorry i guess we better start again."
end if

if myfname = firstname and mylname = lastname then
    put "What a coincidence, you an I have the same first and last name!!!"
end if

if myfname = firstname and mylname not= lastname then
    put "Hey, we have the same first name"
end if

if mylname = lastname and myfname not= firstname then
    put "Hey we have the same last name"
end if

Paul




PostPosted: Tue Feb 10, 2004 8:13 pm   Post subject: (No subject)

so you can just make 2 arrays and hold last names in one and first names in the other, and generate random names by randomizing the subscript variable.
josh




PostPosted: Tue Feb 10, 2004 8:16 pm   Post subject: (No subject)

I want to leave the last name the same for now but what i can't figure out is how to get the variable myfname to be generated randomly.

P.S. I am a real Newbee so i don't realy uderstnd when u use the programing words. Can u dumb i down a notch thanx.
Paul




PostPosted: Tue Feb 10, 2004 8:20 pm   Post subject: (No subject)

something like this:
code:

%Variables used in program
var firstname : string
var lastname : string
var age, a,b : int
var yn : string
var myage : int
var myfname: array 1..5 of string:=init ("Josh", "Adam", "Michael", "Steve", "Joanne")
var mylname: array 1..5 of string:=init ("Smith", "Johnson", "Michaels", "Hanks", "MacDonald")

randint (a, 1, 5)
randint (b, 1, 5)
%Random for age
randomize
randint (myage, 5, 30)

%Get first name
put "Hello my first name is ", myfname(a), "."
put "What is your first name?"
get firstname

%Get last name
put "My last name is ", mylname(b), "."
put "What is your last name?"
get lastname

%Get age
put "I am ", myage, " years old"
put "How old are you?"
get age

%Verify
put "Let me see if I got this right."
put "Your name is ", firstname
put "Your last name is ", lastname
put "and you are ", age, " years old."
put "is this correct? (y/n)"

get yn

%Make comparison
if yn = "y" or  yn = "Y" then

    if age = myage then
        put "We are the same age!!!!"
    end if
    if age > myage then
        put "You are ", age - myage, " years older then me!!"
    end if

    if age < myage then
        put "You are ", myage - age, " years younger then me!!"
    end if


end if

if yn = "n" then
    put "oh sorry i guess we better start again."
end if

if myfname(a) = firstname and mylname(b) = lastname then
    put "What a coincidence, you an I have the same first and last name!!!"
end if

if myfname(a) = firstname and mylname(b) not= lastname then
    put "Hey, we have the same first name"
end if

if mylname(b) = lastname and myfname(a) not= firstname then
    put "Hey we have the same last name"
end if

When you want to pick a random string out of a list, in an array, you have to make the subscript (the number that identifies each variable in an array) in order to pick a random word.
josh




PostPosted: Tue Feb 10, 2004 8:39 pm   Post subject: (No subject)

here is the new code and it errors when i declare the variable myfname on the line with the array

code:

%Variables used in program
var firstname : string
var lastname : string
var age, a, b : int
var yn : string
var myage : int
var myfname: string := ""
var mylname: string
var randomfname: int:= 0


%Array for random generation of names
var myfname: array 1..5 of string: init("Josh", "Adam", "Michael", "Steve", "Joanne")

randint (a, 1, 5)


mylname:= "Rosen"

%Random for age
randomize
randint (myage, 5, 30)

%Get first name
put "Hello my first name is ", myfname(a), "."
put "What is your first name?"
get firstname

%Get last name
put "My last name is ", mylname, "."
put "What is your last name?"
get lastname

%Get age
put "I am ", myage, " years old"
put "How old are you?"
get age

%Verify
put "Let me see if I got this right."
put "Your name is ", firstname
put "Your last name is ", lastname
put "and you are ", age, " years old."
put "is this correct? (y/n)"

get yn

%Make comparison
if yn = "y" then

    if age = myage then
        put "We are the same age!!!!"
    end if
    if age > myage then
        put "You are ", age - myage, " years older then me!!"
    end if

    if age < myage then
        put "You are ", myage - age, " years younger then me!!"
    end if


end if

if yn = "n" then
    put "oh sorry i guess we better start again."
end if

if myfname(a) = firstname and mylname = lastname then
    put "What a coincidence, you an I have the same first and last name!!!"
end if

if myfname(a) = firstname and mylname not= lastname then
    put "Hey, we have the same first name"
end if

if mylname = lastname and myfname(a) not= firstname then
    put "Hey we have the same last name"
end if

Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: