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?>

Turing:


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
<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

Turing:


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.

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:
Turing:


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

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:
Turing:
for i : 1..9 by 2
    put i
end for


Harder way:
Turing:
for i : 1..9
    if not i mod 2 = 0 then
        put i
    end if
end for

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:
Turing:
for i : 1..9 by 2
    put i
end for


Harder way:
Turing:
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

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
code:

>> dt [for i 0 100000 1 [unless equal? 0 MOD i 2 [print i]]]
...
99993
99995
99997
99999
== 0:00:02.033888

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

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.

Smile

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

Turing:

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


: