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

Username:   Password: 
 RegisterRegister   
 Function Failing to Give a Result
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SwAnK




PostPosted: Thu Apr 21, 2005 4:05 pm   Post subject: Function Failing to Give a Result

Here is my function, I was trying to create that if a straight was rolled with 5 dice then so many points were awarded, when executed, it states that my function is failing to give a result. Can someone show me why? Here is my code

code:
var count : int := 0
var roll : array 1 .. 5 of int

function straight_on (die : array 1 .. * of int) : boolean
    var one, two, three, four, five, six : boolean := false

    for i : 1 .. 5
        case die (1) of
            label 1 :
                one := true
            label 2 :
                two := true
            label 3 :
                three := true
            label 4 :
                four := true
            label 5 :
                five := true
            label 6 :
                six := true
        end case
    end for
    if (one and two and three and four and five) or (two and three and four and five and six)
            then
        result true
    elsif false then count+=1000
    end if
end straight_on


proc rolling
    for i : 1 .. 5
        roll (i) := Rand.Int (1, 6)
        put "Rolled ", roll (i)
    end for
end rolling
rolling
if straight_on (roll) then
    count += 1000
end if
 put "count", count , ""


thanx
Sponsor
Sponsor
Sponsor
sponsor
Token




PostPosted: Thu Apr 21, 2005 4:34 pm   Post subject: (No subject)

okay, i dont see how that could help you doing it that way, you should probibly order them by size, (not sure how to do that) and then check to see if the seccond one is 1 less than the first, and if that is true 4 times then its a small straight and 5 times its a large straight ... providing that your going for some sort of yatzee thing
wtd




PostPosted: Thu Apr 21, 2005 4:41 pm   Post subject: (No subject)

code:
if (one and two and three and four and five) or (two and three and four and five and six) then
   result true
elsif false then
   count += 1000
end if


A few things here:


  • You have no "else". There's a possibility your if and elsif aren't exhaustive, so nothing would happen.
  • code:
    elsif false then


    false, by it's very nature, isn't true. This elsif will never happen.
  • The code in the elsif doesn't return anything. Even if it could be evaluated, the problem would still exist.
Cervantes




PostPosted: Thu Apr 21, 2005 4:41 pm   Post subject: (No subject)

You're function fails to give a result if
code:

(one and two and three and four and five) or (two and three and four and five and six)

is false.
You need something to result in an 'else' part of that if statement.

No matter what, your function needs to give a result. This could also be achieved by having a 'result something' at the end of the procedure. Note that this doesn't mean that that result will always be returned, because if the function reaches a result line earlier in the code, it will terminate and result that value immediately. Thus, the last result line is only reached under certain conditions.
SwAnK




PostPosted: Thu Apr 21, 2005 10:50 pm   Post subject: (No subject)

so then what can i put for my else statment?? Question
Tony




PostPosted: Fri Apr 22, 2005 8:32 am   Post subject: (No subject)

SwAnK wrote:
so then what can i put for my else statment?? Question

well if it's not returning true from the if part, I'm going to assume that you want
Turing:

if <condition> then
     return true
else
     return false
end if
wtd




PostPosted: Fri Apr 22, 2005 5:55 pm   Post subject: (No subject)

Tony wrote:
SwAnK wrote:
so then what can i put for my else statment?? Question

well if it's not returning true from the if part, I'm going to assume that you want
Turing:

if <condition> then
     return true
else
     return false
end if


It should be noted that this, while Tony means well, is bad code.

When you write the "if" statement (and it's accompanying "else", the condition is either true or false. That's your boolean value right there. So instead of that, let's just say:

code:
result <condition>


Consider a real example:

code:
if 1 = 1 then
   result true
else
   result false
end if


vs.

code:
result 1 = 1
Tony




PostPosted: Fri Apr 22, 2005 6:12 pm   Post subject: (No subject)

wtd wrote:
It should be noted that this, while Tony means well, is bad code.

Indeed it is. On it's own. That was provided as a reference structure to use, since SwAnK also had some attempts at counter+ type of thing.. Wink
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: