Computer Science Canada Display only odd number in arrary 1..10 |
Author: | nelsonkkyy [ Thu Oct 22, 2009 5:21 pm ] | ||
Post subject: | Display only odd number in arrary 1..10 | ||
What is it you are trying to achieve? <Replace all the <> with your answers/code and remove the <>> What is the problem you are having? <Answer Here> Describe what you have tried to solve this problem <Answer Here> Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) <Am i doing it right?>
Please specify what version of Turing you are using <Answer Here> |
Author: | DtY [ Thu Oct 22, 2009 5:51 pm ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
So far you're good. You're currently displaying every number in the array, now you just need to filter it so it only shows the numbers that are odd. You'll need - a conditional statement (if) - The modulo (mod) operator |
Author: | nelsonkkyy [ Thu Oct 22, 2009 6:04 pm ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
Can anyone show me how to write it?????/ |
Author: | Zren [ Thu Oct 22, 2009 6:06 pm ] | ||
Post subject: | Re: Display only odd number in arrary 1..10 | ||
Or the simplier method... just pump the counter by 2 starting it out on an odd number. |
Author: | nelsonkkyy [ Thu Oct 22, 2009 7:13 pm ] |
Post subject: | Re: Display only odd number in arrary 1..10 |
var num: array 1..9 of int for i:1..9 by 2 put num(i) end for It cannot run. |
Author: | Zren [ Thu Oct 22, 2009 7:38 pm ] |
Post subject: | Re: Display only odd number in arrary 1..10 |
Did you intialize the values of the variables before trying to output them? |
Author: | nelsonkkyy [ Thu Oct 22, 2009 7:54 pm ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
HOW to intialize the values of the variables?? |
Author: | darkn00b [ Thu Oct 22, 2009 8:15 pm ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
So you need to give the nums a value. You can do this a few ways, on is: var num: array 1..9 of int for i:1..9 get num(i) end for for i:1..9 by 2 put num(i) end for This will allow you the person who runs the program to type in a value for all of the nums, and then it outputs only the odd ones. |
Author: | DtY [ Fri Oct 23, 2009 6:24 am ] | ||
Post subject: | Re: Display only odd number in arrary 1..10 | ||
Zren @ Thu Oct 22, 2009 6:06 pm wrote:
Or the simplier method... just pump the counter by 2 starting it out on an odd number. That shows odd indices, not the odd numbers |
Author: | btiffin [ Fri Oct 23, 2009 11:02 pm ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
On Odd Even tests. number AND 1 If the result is 0, the number is even. If the result is 1, (low bit set), the number is odd. Cheers |
Author: | andrew. [ Sat Oct 24, 2009 7:26 am ] | ||||
Post subject: | RE:Display only odd number in arrary 1..10 | ||||
Simple way:
Harder way:
|
Author: | DtY [ Sat Oct 24, 2009 7:35 am ] | ||||
Post subject: | Re: RE:Display only odd number in arrary 1..10 | ||||
andrew. @ Sat Oct 24, 2009 7:26 am wrote: Simple way:
Harder way:
Again, he's looking for odd numbers in the array, not the items with odd indices, so the first one is not right, the second one should be if num(i) mod 2 = 1 |
Author: | andrew. [ Sat Oct 24, 2009 9:43 am ] |
Post subject: | RE:Display only odd number in arrary 1..10 |
Oh I didn't understand the question. I thought he wanted the odd numbers in a range, not the odd numbers out of a list of numbers. |
Author: | btiffin [ Sat Oct 24, 2009 11:31 am ] | ||||
Post subject: | Re: Display only odd number in arrary 1..10 | ||||
Crusty Old guy again Try and not use if num(i) mod 2 = 1, when [b]num(i) and 1 = 1[/i] will be more efficient. It's not a huge deal, but it is a deal. Some REBOL timings; Using modulo and integer arithmetic
delta time of 2 seconds. Using logical and and bitwise 'arithmetic'
delta time of 1.6 seconds. So, if you write code that has to check 100,000 numbers for parity through your life time, using bit-wise you'll get like a whole extra half second of 'not waiting around' life to live. Choose life, aim for efficient code. ![]() Cheers |
Author: | DtY [ Sat Oct 24, 2009 12:25 pm ] |
Post subject: | Re: Display only odd number in arrary 1..10 |
btiffin @ Sat Oct 24, 2009 11:31 am wrote: aim for efficient code.
This might actually be relevant if it was posted anywhere but Turing help :p But yeah, using bitwise and would be considerably faster because it doesn't actually need to divide, and learning optimizations like that will be helpful in the future |
Author: | Zren [ Sat Oct 24, 2009 3:58 pm ] |
Post subject: | Re: RE:Display only odd number in arrary 1..10 |
andrew. @ Sat Oct 24, 2009 9:43 am wrote: Oh I didn't understand the question. I thought he wanted the odd numbers in a range, not the odd numbers out of a list of numbers.
Same here. Sorry bout the mixup guys. The mod attached at the end of the for statement messed up my understanding. |
Author: | B-Man 31 [ Tue Oct 27, 2009 10:33 pm ] | ||
Post subject: | Re: Display only odd number in arrary 1..10 | ||
im pretty sure this is what hes asking
|