Computer Science Canada

HELP PLZ , program being weird (turing)

Author:  kousha41564 [ Fri Dec 05, 2008 11:42 pm ]
Post subject:  HELP PLZ , program being weird (turing)

alright, this is a really simple caluclaotr, wiht a remember what the last equation equaled and re use that number kinda thing. at the bottom loop, i set it up so it only leaves the loop if the user types y or n. but no matter what they type the program asumes taht they would like to re use the last number. sorry if its confusin, its one of my first meaningfull programs, and was done for fun so Razz

Turing:

var checklist : int := 0
var c1 : string
var num1 : real
var memory : string
var num2 : real
var answer : real := 0
var checklist2 : int := 0


loop

    if checklist2 = 0 then
        put "please enter the equation (include spaces between each part of the equation)"
        get num1, c1, num2
    end if

    for counter : 1 .. 10000000

        if checklist2 > 1 then
            put "please enter the equation (the first digit is asumed as ", answer, ")"

           
            get c1, num2
        end if


        if answer > 0 then
            num1 := answer
        end if

        if c1 = "*" then
            answer := num1 * num2
            put num1 * num2

        end if

        if c1 = "/" then
            answer := num1 / num2
            put num1 / num2


        end if

        if c1 = "+" then
            answer := num1 + num2
            put num1 + num2

        end if

        if c1 = "-" then
            answer := num1 - num2
            put num1 - num2
        end if





        loop
            put "would you like to clear memory? (y/n)"
            get memory
            exit when checklist = 1
           
             if memory = "y" then
                answer := 0
                checklist2 := 0

                checklist := 1


            end if


            if memory = "n" then
                put "alright"
                checklist2 := 2
                checklist := 1


            end if

            if memory = "n" or memory = "y" then
                checklist := 1
            end if


            put "   "

            put "memory is ", answer
        end loop
       
    end for
end loop

Author:  DanielG [ Sat Dec 06, 2008 5:11 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

this code has too many mistakes, first, your code would repeat twice in the 'y'/'n' part due to your exist when statement being too early (push it to later on in the loop). Also, the reason you are having problem with the memory erase is that you are not getting any input after the first time if checklist2 (the int you used to check if erase is needed) is 0.

What I am writing now isn't really a mistake, but you should change anyway because it's bad coding. First, when you have multiple ifs where only 1 can occur, you should use elsif (or a case statement). Also, use boolean variables. Furthermore, don't use a huge for loop, you can use two loops one inside the other and the exit statement will only exit the inner one.

Author:  A.J [ Sat Dec 06, 2008 6:17 pm ]
Post subject:  Re: HELP PLZ , program being weird (turing)

DanielG wrote:

this code has too many mistakes, first, your code would repeat twice in the 'y'/'n' part due to your exist when statement being too early (push it to later on in the loop). Also, the reason you are having problem with the memory erase is that you are not getting any input after the first time if checklist2 (the int you used to check if erase is needed) is 0.

What I am writing now isn't really a mistake, but you should change anyway because it's bad coding. First, when you have multiple ifs where only 1 can occur, you should use elsif (or a case statement). Also, use boolean variables. Furthermore, don't use a huge for loop, you can use two loops one inside the other and the exit statement will only exit the inner one.


I agree with what DanielG said.

And, to add to that, you have redundant and 'weird' code...not that I am discouraging you or anything

for ur first 'real' coding program, it is really good (the spacing and everything). Good Job! Very Happy

but look into the code and see if u can shorten it at parts (I dont want to give away anything else Wink)

Author:  kousha41564 [ Sat Dec 06, 2008 6:43 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

thanks, but i cant quite figure out what youre saying, so could you fix it just so it works ? i will fix the if and elsif . i found out bout exit an hour ago Razz and i still havent realized what boolean statements/variables are.

i have no book or anything i am using to work this, the only way i wrote this is my past knwoledge of html and basic (not visiual).

so could you show me what exactly is wrong ?

Author:  Insectoid [ Sat Dec 06, 2008 6:51 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

Boolean is 1 or zero (In Turing it's true/false). Works like any variable. Can be modified with boolean algebra.

code:

var bool : boolean := false %create the variable
bool := bool & true %might be '&&'. performs the 'and' boolean operator.
put bool %this should output false, because true and-ed with false = false


For my example you need some rudimentary knowledge of binary & boolean algebra. Think of it as an integer that can only be 1 or 0.

Author:  DanielG [ Sat Dec 06, 2008 7:02 pm ]
Post subject:  Re: HELP PLZ , program being weird (turing)

first you have this part

code:

if checklist2 = 0 then
        put "please enter the equation (include spaces between each part of the equation)"
        get num1, c1, num2
end if

    for counter : 1 .. 10000000

the problem with this is that the if checklist2 = 0 if only comes in the beginning, and needs to be moved into the main for loop

another problem with you code is below:

code:

loop
            put "would you like to clear memory? (y/n)"
            get memory
            exit when checklist = 1
           
             if memory = "y" then
                answer := 0
                checklist2 := 0

                checklist := 1


            end if


            if memory = "n" then
                put "alright"
                checklist2 := 2
                checklist := 1


            end if

            if memory = "n" or memory = "y" then
                checklist := 1
            end if


            put "   "

            put "memory is ", answer
        end loop


which should be changed to something like this:

code:


loop
            put "would you like to clear memory? (y/n)"
            get memory
           
           
             if memory = "y" then
                answer := 0
                checklist2 := 0

                checklist := 1


            end if


            if memory = "n" then
                put "alright"
                checklist2 := 2
                checklist := 1


            end if

            if memory = "n" or memory = "y" then %_% you don't actually need this part, as it was include in your two past ifs
                checklist := 1
            end if

           exit when checklist = 1
           end loop
put "   "

put "memory is ", answer


Author:  Insectoid [ Sat Dec 06, 2008 7:09 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

code:

if memory = "n" or memory = "y"


Is there any option other than 'n' or 'y'? This seems redundant to me, useless extra processing.

Author:  kousha41564 [ Sat Dec 06, 2008 7:57 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

insection what other option would there be ? its either yes or no

and tahnks, could anyone give me some examples of uses of boolean in an program ?

Author:  Insectoid [ Sat Dec 06, 2008 8:06 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

If there is no other option, why have the statement at all? That's why I asked. If it's yes, or no, do this. But it HAS to be yes or no. So why even ask the computer? Just tell it to do whatever is in the block.

Boolean is used to exit multiple loops, to control things that are either one option or the other. I used in it my adder program (but that is a very specific example). In an RPG, you can use boolean to represent true if an item has been used or false if not. You can use it to call a procedure, for example,

code:

if win = true then
    win_procedure
else
    lose_procedure
end if


Further in your programming career, you will find you use boolean a lot.

Author:  kousha41564 [ Sat Dec 06, 2008 8:07 pm ]
Post subject:  Re: HELP PLZ , program being weird (turing)

var checklist : int := 0
var c1 : string
var num1 : real
var memory : string := "ABC"
var num2 : real
var answer : real := 0
var checklist2 : int := 0


loop

checklist := 0

if checklist2 < 1 then
put "please enter the equation (include spaces between each part of the equation)"
get num1, c1, num2
end if


if checklist2 > 1 then
num1 := answer
end if

if checklist2 > 1 then
put "please enter the equation (the first digit is asumed as ", answer, ")"
get c1, num2
end if







if c1 = "*" then
answer := num1 * num2
put num1 * num2



elsif c1 = "/" then
answer := num1 / num2
put num1 / num2




elsif c1 = "+" then
answer := num1 + num2
put num1 + num2



elsif c1 = "-" then
answer := num1 - num2
put num1 - num2

end if





put "would you like to clear memory ? (y/n) "
get memory


loop

if memory = "y" then
answer := 0
checklist2 := 0

checklist := 1


end if


if memory = "n" then
put "alright"
checklist2 := 2
checklist := 1


end if



put " "

put "memory is ", answer

exit when checklist = 1

end loop



end loop





works now , tahtnks for all the help guys, and could you send me that program u used boolean in?

Author:  Insectoid [ Sat Dec 06, 2008 9:08 pm ]
Post subject:  RE:HELP PLZ , program being weird (turing)

Look in the submissions forum; I'm sure there's lots (the one I used in my example was in Java)


: