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

Username:   Password: 
 RegisterRegister   
 Not statements...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sriyegna




PostPosted: Mon Mar 08, 2010 4:42 pm   Post subject: Not statements...

What is it you are trying to achieve?
An if statement that can use the NOT statement...


What is the problem you are having?
I cant manage to get an if statement to stop if a variable has a certain number. Im trying to get it to only do the equations if resp is not equal to 0....


Describe what you have tried to solve this problem
added a not statement..


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


var num, c, resp, g : real
var num2, hnum, lnum : real
c := 0
num := 0
hnum := 0
lnum := 0
put "Please enter a series of real numbers, and enter a zero to stop."
loop
    get resp
    exit when resp = 0
    if resp not= 0 then
        num2 := resp
    end if
    if num2 > hnum and num2 > 0 or num2 < 0 then
        hnum := num2
    end if
    if num2 < lnum and num2 > 0 or num2 < 0 then
        lnum := num2
    end if
   
    c := c + 1
end loop

if resp = 0 and c = 0 then
put "Invalid"
else
put "The highest number is: ", hnum
put "The lowest number is:  ", lnum
end if




Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
USEC_OFFICER




PostPosted: Mon Mar 08, 2010 4:47 pm   Post subject: RE:Not statements...

Looks okay with me. The problem doesn't seem to be a problem with the not statement. Maybe it has something to do with the if statements. Try nesting them in each other.

eg: if resp not= 0 then
if blah blah blah
blah blah blah
end if
end if
sriyegna




PostPosted: Mon Mar 08, 2010 4:51 pm   Post subject: RE:Not statements...

lol yea, i tried that, along with elsifs. this is what im at now... (well, whats below).

The program is supposed to tell the lowest number and the highest number.... but if i enter 5, 10, 15, 20, 0. The program is supposed to only find the high and lows from 5, 10, 15, 20 however it does 0 as well and marks 0 as the low num


Turing:

var num, c, resp, g : real
var num2, hnum, lnum : real
c := 0
num := 0
hnum := 0
lnum := 0
put "Please enter a series of real numbers, and enter a zero to stop."
loop
    get resp
    exit when resp = 0
    if resp not= 0 then
        num2 := resp

    if num2 > hnum and num2 > 0 or num2 < 0 then
        hnum := num2

    if num2 < lnum and num2 > 0 or num2 < 0 then
        lnum := num2
    end if
    end if
    end if
   
    c := c + 1
end loop

if resp = 0 and c = 0 then
put "Invalid"
else
put "The highest number is: ", hnum
put "The lowest number is:  ", lnum
end if

jbking




PostPosted: Mon Mar 08, 2010 5:21 pm   Post subject: Re: RE:Not statements...

3 points of what I'd question in that code:

Why do you have " and num2 > 0 or num2 < 0 " as part of that if? I don't see why that's important.

Do you have a reason for not taking the first values what you set hnum and lnum to something near the beginning so that you aren't looking for something below 0 to replace in the value of lnum?

Also, why do you have the 3 ifs all enclosed within each other?
chrisbrown




PostPosted: Mon Mar 08, 2010 5:29 pm   Post subject: RE:Not statements...

Logic operators (and, or, not) are handled in the order they appear, so you are really checking if num2 is greater than both 0 and hnum, OR num2 is less than zero. You could put the or statement in parentheses, but there is a better way.

Since you exit the loop if resp is zero, you know that as long as you don't change its value, resp (and therefore num2) will not be zero, so you only need to check if num2 > hnum, and do similarly for lnum.

And the reason lnum is 0 after running is because you set it to 0 at the beginning, and because of what I first said, the value of lnum never changes.
sriyegna




PostPosted: Mon Mar 08, 2010 5:44 pm   Post subject: Re: RE:Not statements...

jbking @ Mon Mar 08, 2010 5:21 pm wrote:
3 points of what I'd question in that code:

Why do you have " and num2 > 0 or num2 < 0 " as part of that if? I don't see why that's important.

Do you have a reason for not taking the first values what you set hnum and lnum to something near the beginning so that you aren't looking for something below 0 to replace in the value of lnum?

Also, why do you have the 3 ifs all enclosed within each other?


kay Smile sorry, i just started turing.

I couldnt get not statements to work, so i tried making it below 0 or above 0, therefore if it was 0, it wouldnt work in that if statement Razz

The number can be below 0, which isnt a problem. for example, if i enter, 5, 10, -23, -34, 0

it will give me the correct numbers, because 0 is not supposed to be counted. whereas if i enter 5, 10, 15, 20, 0. then it will give me the correct high number, but will give me 0 as the low number (which should be 5).


the first person suggested nesting ifs. even with elsif's or without nesting them, i get the same outcome Sad
sriyegna




PostPosted: Mon Mar 08, 2010 5:45 pm   Post subject: Re: RE:Not statements...

methodoxx @ Mon Mar 08, 2010 5:29 pm wrote:
Logic operators (and, or, not) are handled in the order they appear, so you are really checking if num2 is greater than both 0 and hnum, OR num2 is less than zero. You could put the or statement in parentheses, but there is a better way.

Since you exit the loop if resp is zero, you know that as long as you don't change its value, resp (and therefore num2) will not be zero, so you only need to check if num2 > hnum, and do similarly for lnum.

And the reason lnum is 0 after running is because you set it to 0 at the beginning, and because of what I first said, the value of lnum never changes.


the problem is, it doesnt exit once it recognizes its 0. it goes through the if statements, then exits. so it would store it as the lnum variable which would screw up the program lol.

Could you please elaborate on your theory about parenthesis'?
chrisbrown




PostPosted: Mon Mar 08, 2010 6:10 pm   Post subject: Re: RE:Not statements...

sriyegna wrote:
Could you please elaborate on your theory about parenthesis'?

Ok, but you can't use it to solve this problem:
Turing:
var low : int := 0
var mid : int := 5
var high : int := 0

if mid > low and mid > 0 or mid < 0 then
    put "mid can be either a) positive and greater than low or b) any negative number"
end if

if mid > low and (mid > 0 or mid < 0) then
    put "mid is greater than low and is not zero"
end if


The better solution is:
Turing:
if mid > low and mid not= 0 then
    put "mid is greater than low and is not zero"
end if


Look at your code. Because you exit if input is zero, I repeat, you do not need to make sure it is still not zero in your if statements, so clean them up. (Use the first version, not the nested-if version).

Now think about what you're trying to do. If you only enter one number, you want both hnum and lnum to equal that number. That means lnum should be higher than any other number, so any number inputted will be lower than it. Turing calls this number maxint, so replace lnum := 0 with lnum := maxint, and change hnum to -maxint.
Sponsor
Sponsor
Sponsor
sponsor
sriyegna




PostPosted: Mon Mar 08, 2010 7:22 pm   Post subject: RE:Not statements...

Thanks, the problem was though, i couldnt get the not statements Razz
TheGuardian001




PostPosted: Mon Mar 08, 2010 7:55 pm   Post subject: Re: Not statements...

your not statements were fine (and actually completely unnecessary, since you have an exit when), the problem was pretty much everything else described in peoples responses.
sriyegna




PostPosted: Mon Mar 08, 2010 9:43 pm   Post subject: RE:Not statements...

im confused Razz could you please point out what i did wrong?
TheGuardian001




PostPosted: Mon Mar 08, 2010 11:40 pm   Post subject: Re: Not statements...

Sure,

jbking wrote:

3 points of what I'd question in that code

Why do you have " and num2 > 0 or num2 < 0 " as part of that if? I don't see why that's important.

Do you have a reason for not taking the first values what you set hnum and lnum to something near the beginning so that you aren't looking for something below 0 to replace in the value of lnum?

Also, why do you have the 3 ifs all enclosed within each other?


1)the not=0 (or any equivalent, such as the ones jbking mentioned) lines after the exit when are useless. num2 can never be zero at any line in that loop, since the exit when condition takes care of that. While these aren't technically a problem, they aren't useful either.

2)At the very start of your program you have this:
code:

lnum := 0

Now, when you check to see if num2 is less than lnum, will any positive number be less than lnum (is any positive number less than 0)? lnum should not be initialized at 0, since that will mean only negative numbers will ever less than the current value of lnum.

3)All of your ifs are inside one another.
code:

if resp not= 0 then
    num2 := resp

    if num2 > hnum and num2 > 0 or num2 < 0 then
       hnum := num2

       if num2 < lnum and num2 > 0 or num2 < 0 then
           lnum := num2
       end if

    end if

end if


Any code inside of an if condition is only executed if that condition is true. Since your check to see if num2 is less than lnum is inside of the check to see if num2 is greater than hnum, your program will only check to see if num2 is less than lnum if it has already seen that num2 is greater than hnum (which makes it completely pointless.)
sriyegna




PostPosted: Tue Mar 09, 2010 6:58 am   Post subject: RE:Not statements...

OH! Great! THanks, now i see that the problem was initiating lnum as 0. i changed that to 999999999 and the program works fine. the only problem is Razz it kind of looks unprofessional but i suppose there isnt another way.

as for the exit when num = 0, that is also fixed because as you said, no positive number can be lower than 0.

It was a mistake as simple as that and i couldnt realize it lol Sad

Anyways, thanks ALOT! to all of you for your help! Its greatly appreciated!
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  [ 13 Posts ]
Jump to:   


Style:  
Search: