
-----------------------------------
sriyegna
Mon Mar 08, 2010 4:42 pm

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)




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

-----------------------------------
USEC_OFFICER
Mon Mar 08, 2010 4:47 pm

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
Mon Mar 08, 2010 4:51 pm

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



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
Mon Mar 08, 2010 5:21 pm

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
Mon Mar 08, 2010 5:29 pm

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
Mon Mar 08, 2010 5:44 pm

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?

kay :) 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 :P

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 :(

-----------------------------------
sriyegna
Mon Mar 08, 2010 5:45 pm

Re: 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.

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
Mon Mar 08, 2010 6:10 pm

Re: RE:Not statements...
-----------------------------------
Could you please elaborate on your theory about parenthesis'?

Ok, but you can't use it to solve this problem:
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:
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.

-----------------------------------
sriyegna
Mon Mar 08, 2010 7:22 pm

RE:Not statements...
-----------------------------------
Thanks, the problem was though, i couldnt get the not statements :P

-----------------------------------
TheGuardian001
Mon Mar 08, 2010 7:55 pm

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
Mon Mar 08, 2010 9:43 pm

RE:Not statements...
-----------------------------------
im confused :P could you please point out what i did wrong?

-----------------------------------
TheGuardian001
Mon Mar 08, 2010 11:40 pm

Re: Not statements...
-----------------------------------
Sure,


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:
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
Tue Mar 09, 2010 6:58 am

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 :P 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 :(

Anyways, thanks ALOT! to all of you for your help! Its greatly appreciated!
