1. Requesting help again... as a beginner....

Okay, I got started writing a math program, and I wanted to know is there a way to make the statement 'if' do something only if it gets an odd number. For example...

atom a a = gets(KEYBOARD) if a = ODDNUMBER then ... puts(1,"\nThat is an odd number.") end if

How do I make it so that if a person types in an odd number, then that message would come up? What do I put in place of 'ODDNUMBER'? Sorry, I couldn't think of an easier way to say that question, but I hope you understand what I am asking.... Also, would all of that be right in syntax? I put it in .exw, if that matters, and I am using Euphoria 3.1.1. And.... what is \n used for? I am basing this off of the source code of the demo Animal. I did read what /n does in ABGTE, but I didn't understand. It said something like:

Data has outputted in text mode has carriage return codes (13 or ‘\r’) added before any line feed codes (10 or ‘/n’)

Can someone explain it to me a little bit better? Or is that the simplest form it can be explained in? Thanks for the help!

new topic     » topic index » view message » categorize

2. Re: Requesting help again... as a beginner....

Mattymatt said...

Okay, I got started writing a math program, and I wanted to know is there a way to make the statement 'if' do something only if it gets an odd number. For example...

atom a 
a = gets(KEYBOARD) 
if a = ODDNUMBER then ... 
puts(1,"\nThat is an odd number.") 
end if 

How do I make it so that if a person types in an odd number, then that message would come up? What do I put in place of 'ODDNUMBER'?

You can't. Nothing will fit. However, you can do this:

if remainder(a, 2) != 0 then 
puts(1,"\nThat is an odd number.") 
end if 

Since by definition, odd numbers are not divisible by two, and so they will have a remainder.

You have another problem, basically gets(KEYBOARD) does not return a number but it returns a sequence. It returns a string of what the user typed, which can not be used as a number directly.

You probably want to use prompt_number() in get.e instead.

Mattymatt said...

Sorry, I couldn't think of an easier way to say that question, but I hope you understand what I am asking.... Also, would all of that be right in syntax? I put it in .exw, if that matters, and I am using Euphoria 3.1.1. And.... what is \n used for? I am basing this off of the source code of the demo Animal. I did read what /n does in ABGTE, but I didn't understand. It said something like:

Data has outputted in text mode has carriage return codes (13 or ‘\r’) added before any line feed codes (10 or ‘/n’)

Can someone explain it to me a little bit better? Or is that the simplest form it can be explained in? Thanks for the help!

It is always \n and \r, never use /n or /r.

The \ is a backslash, and the / is a forward slash.

Basically, \n is a new line. So if you have a string like "Hello.\nThis is some text.", when you print that string it will show up like this:

Hello. 
This is some text. 

But if you put it into a file, it is turned into "Hello.\r\nThis is some text." as this is the DOS text file format. The DOS text file format is a discussion in and of itself.

Btw, if you did a puts(1, "\r") then that would move the cursor back to the first column but it would stay on the same row, the same line. This might be useful if you wanted to erase a line or overwrite a line with new text.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Requesting help again... as a beginner....

jimcbrown said...
if remainder(a, 2) != 0 then 
puts(1,"\nThat is an odd number.") 
end if 

Bits twiddling is neater and faster:

if and_bits(a, 1) then ... 

jiri

new topic     » goto parent     » topic index » view message » categorize

4. Re: Requesting help again... as a beginner....

jiri said...
jimcbrown said...
if remainder(a, 2) != 0 then 
puts(1,"\nThat is an odd number.") 
end if 

Bits twiddling is neater and faster:

if and_bits(a, 1) then ... 

jiri

Yes, but needs more explanation for a newcomer.

The remainder() idea just works on the premise that odd numbers always have a remainder of 1 when divided by 2. However, dividing is often slower than most other arithmetic operations.

The bit twiddling idea works by knowing that the way that computers store numbers in memory. All numbers are just a set of bits (ones and zeros). In Euphoria, whole numbers use a set of 31-bits and the number of combinations that these have means that it can easily store all the whole numbers from -1,073,741,824 to +1,073,741,823. However, every odd number always has the 31st bit set to 1 and even numbers always have this set to 0. The and_bits() function does a binary 'and' operation on every bit in the two sets of bits supplied in its parameters. An 'and' operation simply returns 0 if either bit is a zero, and returns a 1 if both bits are 1.

So testing for oddness using and_bits(a, 1) is just this ...

31-bits in 'a' = xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx 
31-bits in 1   = 000 0000 0000 0000 0000 0000 0000 0000 0001 
Where the 'x' is either 1 or 0. Because all but the 31st bit in '1' is zero, the results for anding these are also zero. But with that last bit, if 'a' is odd then its last bit is 1 otherwise 0 (even). So odd numbers 'and' 1 and 1 ==> 1, and even numbers 'and' 0 and 1 ==> 0.

Therefore, and_bits(a,1) for odd numbers always returns 1 and 0 for even numbers, and this is a lot faster than doing a division.

As this comes up frequently, I'll add it as a standard function to the math.e library for v4. The inlining feature should ensure that this will not have a function call overhead.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Requesting help again... as a beginner....

DerekParnell said...
jiri said...
jimcbrown said...
if remainder(a, 2) != 0 then 
puts(1,"\nThat is an odd number.") 
end if 

Bits twiddling is neater and faster:

if and_bits(a, 1) then ... 

jiri

Yes, but needs more explanation for a newcomer.

The remainder() idea just works on the premise that odd numbers always have a remainder of 1 when divided by 2. However, dividing is often slower than most other arithmetic operations.

The bit twiddling idea works by knowing that the way that computers store numbers in memory. All numbers are just a set of bits (ones and zeros). In Euphoria, whole numbers use a set of 31-bits and the number of combinations that these have means that it can easily store all the whole numbers from -1,073,741,824 to +1,073,741,823. However, every odd number always has the 31st bit set to 1 and even numbers always have this set to 0. The and_bits() function does a binary 'and' operation on every bit in the two sets of bits supplied in its parameters. An 'and' operation simply returns 0 if either bit is a zero, and returns a 1 if both bits are 1.

So testing for oddness using and_bits(a, 1) is just this ...

31-bits in 'a' = xxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx 
31-bits in 1   = 000 0000 0000 0000 0000 0000 0000 0000 0001 
Where the 'x' is either 1 or 0. Because all but the 31st bit in '1' is zero, the results for anding these are also zero. But with that last bit, if 'a' is odd then its last bit is 1 otherwise 0 (even). So odd numbers 'and' 1 and 1 ==> 1, and even numbers 'and' 0 and 1 ==> 0.

Therefore, and_bits(a,1) for odd numbers always returns 1 and 0 for even numbers, and this is a lot faster than doing a division.

As this comes up frequently, I'll add it as a standard function to the math.e library for v4. The inlining feature should ensure that this will not have a function call overhead.

Alright, thanks. Is there a way to copy the code out of the editor, so I don't have to retype it? Anyways, heres what I got so far, but I have this ex.err that keeps saying I should have end instead. Heres just the segment of it that it reads up to.

sequence t 
integer h, e 
t = {1,10000} 
while t = {1,10000} do 
t = prompt_number("\nPlease enter a positive odd number.") 
if t <= 2 then 
puts(1,"\nThat is an unusable number.") 
t = {1,10000} 
else t = and_bits(t,1) 
power(t,2) = h 
____________________ 
puts(1,h) 

(I didn't know how to indent in eucode) The ex.err shows <eucode> power(t,2) = h</eucode>
^ is under r at power.
expected to see possible 'end', not a function.
the code under the line is part of the code after the problem. Why does it want me to put end there? shouldn't it be so it can do the next function?

new topic     » goto parent     » topic index » view message » categorize

6. Re: Requesting help again... as a beginner....

Mattymatt said...

<snip>
Alright, thanks. Is there a way to copy the code out of the editor, so I don't have to retype it?

Which editor?
If it's the Euphoria "ed", then you have to go over to the left top of the cmd window, click on the icon there, select first "edit", then "mark", use the cursor to mark the section you want to copy, then go back to that icon at the top left, select "edit" and then "copy"; that will put the marked portion into the "clipboard",(you have to make sure to go as far to the right side as necessary, to get full extent of all lines), then here in a post just press <ctl>V to put what you copied into a post here.

Mattymatt said...

Anyways, heres what I got so far, but I have this ex.err that keeps saying I should have end instead. Heres just the segment of it that it reads up to.

sequence t 
integer h, e 
t = {1,10000} 
while t = {1,10000} do 
t = prompt_number("\nPlease enter a positive odd number.") 
if t <= 2 then 
puts(1,"\nThat is an unusable number.") 
t = {1,10000} 
else t = and_bits(t,1) 
power(t,2) = h 
____________________ 
puts(1,h) 

(I didn't know how to indent in eucode) The ex.err shows

power(t,2) = h 


^ is under r at power.
expected to see possible 'end', not a function.
the code under the line is part of the code after the problem. Why does it want me to put end there? shouldn't it be so it can do the next function?

power is a function, not a procedure, your code there needs to be:
h = power(t,2)

I'm not sure if it's adequate to have "h" be an integer, though, because you could be raising decimal fractions to a power, and the result of that won't be integer type. So maybe you need "h" to be atom, I'm not sure.

Dan

new topic     » goto parent     » topic index » view message » categorize

7. Re: Requesting help again... as a beginner....

Hello Mattymatt,

To indent you code it would depend on which editor you are using, See the docs for you editor.

To indent in euphoria:

puts(1,repeat(" ",6)&"hello")

or

puts(1,"\t hello")\t is tab

As for your code you have a while and a for loop not closed.

Your code should be:

 
 
procedure whatever()--open procedure 
  while t = {1,10000} do --open while loop 
   if t <= 2 then --open if loop 
      --your stuff 
   end if --close if loop 
  end while --close while loop   
end procedure--close procedure 
 

don cole

new topic     » goto parent     » topic index » view message » categorize

8. Re: Requesting help again... as a beginner....

DanM said...

power is a function, not a procedure, your code there needs to be:
h = power(t,2)

I'm not sure if it's adequate to have "h" be an integer, though, because you could be raising decimal fractions to a power, and the result of that won't be integer type. So maybe you need "h" to be atom, I'm not sure.

Dan

Also, i'm doing this in .exw but I think I mentioned that before.

Okay, changed, but it keeps saying true/false condition must be an ATOM. I have changed h and e to atoms though. in the ex.err it says

6 true/false condition must be an ATOM

C:\EUPHORIA\include\get.e: input_file = <no value> input_string = <no value> string_next = <no value> ch = <no value>

t = {1,10000} h = <no value> e = <no value>

I tried adding an atom k = 10000 and using that to make t = {1,k}, and changing t to k, for the prompt number. something like k = prompt_number("\nsomething",t), but that didn't work either, its became basically the same ex.err file, but it would be 7 true/fals condition must be an ATOM, and added k = 10000.

Can anyone help?

new topic     » goto parent     » topic index » view message » categorize

9. Re: Requesting help again... as a beginner....

Mattymatt said...

Anyways, heres what I got so far, but I have this ex.err that keeps saying I should have end instead. Heres just the segment of it that it reads up to.

sequence t 
integer h, e 
t = {1,10000} 
while t = {1,10000} do 
t = prompt_number("\nPlease enter a positive odd number.") 
if t <= 2 then 
puts(1,"\nThat is an unusable number.") 
t = {1,10000} 
else t = and_bits(t,1) 
power(t,2) = h 
____________________ 
puts(1,h) 

The ex.err shows <eucode> power(t,2) = h</eucode>
^ is under r at power.
expected to see possible 'end', not a function.
the code under the line is part of the code after the problem. Why does it want me to put end there? shouldn't it be so it can do the next function?

Try working with this code:

sequence range 
atom h, e, t 
range = {1,10000} 
t = -1 
while 1 do 
	t = prompt_number("\nPlease enter a positive odd number.", range) 
	if t <= 2 then 
		puts(1,"\nThat is an unusable number.") 
	else 
		t = and_bits(t,1) 
		h = power(t,2) 
		printf(1, "%d\n", {h}) 
	end if 
end while 
new topic     » goto parent     » topic index » view message » categorize

10. Re: Requesting help again... as a beginner....

jimcbrown said...

Try working with this code:

sequence range 
atom h, e, t 
range = {1,10000} 
t = -1 
while 1 do 
	t = prompt_number("\nPlease enter a positive odd number.", range) 
	if t <= 2 then 
		puts(1,"\nThat is an unusable number.") 
	else 
		t = and_bits(t,1) 
		h = power(t,2) 
		printf(1, "%d\n", {h}) 
	end if 
end while 

You. Rock. The program actually starts! For me, thats a big leap forward. However, there are a few areas that are confusing to me. Why does t = -1 need to be there? I deleted that and it still works. Just so I know, in <eucode> while 1 do </eucode> means 1 means true? Because I changed the number, and that still works.

The only faulty up part is when I put in a number, I always get back a 1 or 0. I want it so I get back the number by the power of 2. 1 means true if I remember, and 0 means false.... but, I was while I was messing with it, I think I got a 0.5 somehow, I don't remember what I put though. Shouldn't printf(1, "%d\n", {h}) show h instead of one, say if I typed in 5, it should return 25? I tried print(), puts(), messed with t = 1 in the beginning, and while.

new topic     » goto parent     » topic index » view message » categorize

11. Re: Requesting help again... as a beginner....

Part of the problem is that we don't actually know what you are trying to do. I'm guessing now, that you want the user to enter an odd number and your program will calculate what the square of that number is, and then display the result. Is that it? If so, try this version ...

sequence range 
atom h, t 
range = {3,10000} 
 
while 1 do 
    t = prompt_number("\nPlease enter a positive odd number greater than 2: ", range) 
    if not and_bits(t,1) then 
        puts(1,"\nThat is not an  odd number, try again.") 
    else 
        h = power(t,2) 
        printf(1, "%d squared is %d\n", {t,h}) 
    end if 
 
end while 
new topic     » goto parent     » topic index » view message » categorize

12. Re: Requesting help again... as a beginner....

DerekParnell said...

Part of the problem is that we don't actually know what you are trying to do. I'm guessing now, that you want the user to enter an odd number and your program will calculate what the square of that number is, and then display the result. Is that it? If so, try this version ...

sequence range 
atom h, t 
range = {3,10000} 
 
while 1 do 
    t = prompt_number("\nPlease enter a positive odd number greater than 2: ", range) 
    if not and_bits(t,1) then 
        puts(1,"\nThat is not an  odd number, try again.") 
    else 
        h = power(t,2) 
        printf(1, "%d squared is %d\n", {t,h}) 
    end if 
 
end while 

Thanks, this helps a lot, I think I don't need anymore help, but if I do I'll come back and ask. Thanks everyone!

new topic     » goto parent     » topic index » view message » categorize

13. Re: Requesting help again... as a beginner....

What is 'while 1 do'?

What does the 1 mean? Does it mean the condition is always true, so then 'while' would just keep looping just to clarify.

new topic     » goto parent     » topic index » view message » categorize

14. Re: Requesting help again... as a beginner....

Mattymatt said...

What is 'while 1 do'?

What does the 1 mean? Does it mean the condition is always true, so then 'while' would just keep looping just to clarify.

This is correct.

In short, 0 is false. Any integer or decimal point number that is not zero is true, i.e. -1 and 1 and 9 and 0.1 are all true.

new topic     » goto parent     » topic index » view message » categorize

15. Re: Requesting help again... as a beginner....

It's amazing. No hints, no clues, no feedback at all. Just questions. sad

new topic     » goto parent     » topic index » view message » categorize

16. Re: Requesting help again... as a beginner....

Mattymatt said...

What is 'while 1 do'?

What does the 1 mean? Does it mean the condition is always true, so then 'while' would just keep looping just to clarify.

this usage is an optimized form of the the "while some condition true, do the following, but exit when the condition is not true".

It's optimized in that in this form, it doesn't actually (as far as I know) check for any change in the status of the condition, but simply continuously loops. In such a case, you should usually provide some way to actually EXIT the loop, using the command "exit". Perhaps, allow user to input a zero, and if a zero is input, the command in the if statement would be "exit".

Dan

new topic     » goto parent     » topic index » view message » categorize

17. Re: Requesting help again... as a beginner....

euler said...

It's amazing. No hints, no clues, no feedback at all. Just questions. sad

Hey man I'm still learning.

new topic     » goto parent     » topic index » view message » categorize

18. Re: Requesting help again... as a beginner....

Mattymatt said...
euler said...

It's amazing. No hints, no clues, no feedback at all. Just questions. sad

Hey man I'm still learning.

Firstly, that's not the point, Mattymatt. Everyone here is willing to help, no complaints about asking questions, of any level, but you'd help us a lot (and also yourself) if you elaborate it a bit. Secondly, several fellows asked you simple questions and you answered to none. I find this sort of impolite. They are trying to refine which is the best answer, or just trying to find your needs, so you could at least answer their questions.

Again, this is a very helpful bunch of people. I'm here also to learn so, let's not spoil the help offered so kindly. My tuppence.

new topic     » goto parent     » topic index » view message » categorize

19. Re: Requesting help again... as a beginner....

 
while 1 do 

is the same as:

while 1=1 do 

Don Cole

new topic     » goto parent     » topic index » view message » categorize

20. Re: Requesting help again... as a beginner....

DerekParnell said...

Part of the problem is that we don't actually know what you are trying to do. I'm guessing now, that you want the user to enter an odd number and your program will calculate what the square of that number is, and then display the result. Is that it? If so, try this version ...

sequence range 
atom h, t 
range = {3,10000} 
 
while 1 do 
    t = prompt_number("\nPlease enter a positive odd number greater than 2: ", range) 
    if not and_bits(t,1) then 
        puts(1,"\nThat is not an  odd number, try again.") 
    else 
        h = power(t,2) 
        printf(1, "%d squared is %d\n", {t,h}) 
    end if 
 
end while 

Yes, that is what I was trying to do, then later I was going to add a formula to get another number. Just didn't want you guys to write the exact code, because then I would have done none of the work.

new topic     » goto parent     » topic index » view message » categorize

21. Re: Requesting help again... as a beginner....

DanM said...
Mattymatt said...

<snip>
Alright, thanks. Is there a way to copy the code out of the editor, so I don't have to retype it?

Which editor?
If it's the Euphoria "ed", then you have to go over to the left top of the cmd window, click on the icon there, select first "edit", then "mark", use the cursor to mark the section you want to copy, then go back to that icon at the top left, select "edit" and then "copy"; that will put the marked portion into the "clipboard",(you have to make sure to go as far to the right side as necessary, to get full extent of all lines), then here in a post just press <ctl>V to put what you copied into a post here.

Mattymatt said...

Anyways, heres what I got so far, but I have this ex.err that keeps saying I should have end instead. Heres just the segment of it that it reads up to.

sequence t 
integer h, e 
t = {1,10000} 
while t = {1,10000} do 
t = prompt_number("\nPlease enter a positive odd number.") 
if t <= 2 then 
puts(1,"\nThat is an unusable number.") 
t = {1,10000} 
else t = and_bits(t,1) 
power(t,2) = h 
____________________ 
puts(1,h) 

(I didn't know how to indent in eucode) The ex.err shows

power(t,2) = h 


^ is under r at power.
expected to see possible 'end', not a function.
the code under the line is part of the code after the problem. Why does it want me to put end there? shouldn't it be so it can do the next function?

power is a function, not a procedure, your code there needs to be:
h = power(t,2)

I'm not sure if it's adequate to have "h" be an integer, though, because you could be raising decimal fractions to a power, and the result of that won't be integer type. So maybe you need "h" to be atom, I'm not sure.

Dan

Yes, Euphoria editor. Okay, answered all the questions I could find, see anybody elses' euler?

new topic     » goto parent     » topic index » view message » categorize

22. Re: Requesting help again... as a beginner....

Mattymatt said...

<snipped>

Yes, Euphoria editor. Okay, answered all the questions I could find, see anybody elses' euler?

HUH? [insert confused face here]

Dan

new topic     » goto parent     » topic index » view message » categorize

23. Re: Requesting help again... as a beginner....

Dan_M said...
Mattymatt said...

<snipped>

Yes, Euphoria editor. Okay, answered all the questions I could find, see anybody elses' euler?

HUH? [insert confused face here]

Dan

Ok, never mind, I finally got it, you were responding to two different posts in one, and the other post was from euler. I spent a while googling and reading about Euler the mathematician, until my eyes crossed and I had to stop! smile

Dan

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu