
-----------------------------------
nelsonkkyy
Thu Oct 22, 2009 5:21 pm

Display only odd number in arrary 1..10
-----------------------------------
What is it you are trying to achieve?



What is the problem you are having?



Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)




var num:array 1..10 of int
for i:1..10 mod
put num(i)
end for
%How to do it???!!!!!?&




Please specify what version of Turing you are using


-----------------------------------
DtY
Thu Oct 22, 2009 5:51 pm

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

-----------------------------------
nelsonkkyy
Thu Oct 22, 2009 6:04 pm

RE:Display only odd number in arrary 1..10
-----------------------------------
Can anyone show me how to write it?????/

-----------------------------------
Zren
Thu Oct 22, 2009 6:06 pm

Re: Display only odd number in arrary 1..10
-----------------------------------


for i:1..9 by 2
put num(i)
end for



Or the simplier method... just pump the counter by 2 starting it out on an odd number.

-----------------------------------
nelsonkkyy
Thu Oct 22, 2009 7:13 pm

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.

-----------------------------------
Zren
Thu Oct 22, 2009 7:38 pm

Re: Display only odd number in arrary 1..10
-----------------------------------
Did you intialize the values of the variables before trying to output them?

-----------------------------------
nelsonkkyy
Thu Oct 22, 2009 7:54 pm

RE:Display only odd number in arrary 1..10
-----------------------------------
HOW to intialize the values of the variables??

-----------------------------------
darkn00b
Thu Oct 22, 2009 8:15 pm

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.

-----------------------------------
DtY
Fri Oct 23, 2009 6:24 am

Re: Display only odd number in arrary 1..10
-----------------------------------


for i:1..9 by 2
put num(i)
end for



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

-----------------------------------
btiffin
Fri Oct 23, 2009 11:02 pm

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

-----------------------------------
andrew.
Sat Oct 24, 2009 7:26 am

RE:Display only odd number in arrary 1..10
-----------------------------------
Simple way:
for i : 1..9 by 2
    put i
end for

Harder way:
for i : 1..9
    if not i mod 2 = 0 then
        put i
    end if
end for


-----------------------------------
DtY
Sat Oct 24, 2009 7:35 am

Re: RE:Display only odd number in arrary 1..10
-----------------------------------
Simple way:
for i : 1..9 by 2
    put i
end for

Harder way:
for i : 1..9
    if not i mod 2 = 0 then
        put i
    end if
end for

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

-----------------------------------
andrew.
Sat Oct 24, 2009 9:43 am

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.

-----------------------------------
btiffin
Sat Oct 24, 2009 11:31 am

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
[code]
>> dt [for i 0 100000 1 [unless equal? 0 MOD i 2 [print i]]]
...
99993
99995
99997
99999
== 0:00:02.033888
[/code]
delta time of 2 seconds.

Using logical and and bitwise 'arithmetic'
[code]
>> dt [for i 0 100000 1 [unless equal? 0 i AND 1 [print i]]] 
99993
99995
99997
99999
== 0:00:01.563786
[/code]
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

-----------------------------------
DtY
Sat Oct 24, 2009 12:25 pm

Re: Display only odd number in arrary 1..10
-----------------------------------
 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

-----------------------------------
Zren
Sat Oct 24, 2009 3:58 pm

Re: 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.

Same here. Sorry bout the mixup guys. The mod attached at the end of the for statement messed up my understanding.

-----------------------------------
B-Man 31
Tue Oct 27, 2009 10:33 pm

Re: Display only odd number in arrary 1..10
-----------------------------------
im pretty sure this is what hes asking


var num : array 1 .. 10 of int

% mod checks for the remainder so if the remandier is 1, then it has to be odd therefore the function returns a true value
function oddEven (num : int) : boolean
    var ans : int
    ans := num mod 2 
    result ans = 1
end oddEven

%Here all im doing is giving the array values (you can make them any value an it will work)
for i : 1 .. 10
    num (i) := i
end for

%This is where im actually checking to see if its odd using my function
for i : 1 .. 10
    if oddEven (num (i)) then
        put num (i)
    end if
end for

