| Author | 
		Message | 
	
		 
		nelsonkkyy
 
 
 
    
		 | 
		
		
			
				  Posted: 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> | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	
	 
		  | 
	
				 
		DtY
 
  
 
    
		 | 
		
		
			
				  Posted: 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 | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		nelsonkkyy
 
 
 
    
		 | 
		
		
			
				  Posted: 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?????/ | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		Zren
 
  
 
    
		 | 
		
		
			
				  Posted: 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. | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		nelsonkkyy
 
 
 
    
		 | 
		
		
			
				  Posted: 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. | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		Zren
 
  
 
    
		 | 
		
		
			
				  Posted: 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? | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		nelsonkkyy
 
 
 
    
		 | 
		
		
			
				  Posted: 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?? | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		darkn00b
 
 
 
    
		 | 
		
		
			
				  Posted: 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. | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
		 
		Sponsor Sponsor 
		 
  
		 | 
		
 | 
	
	 
		  | 
	
				 
		DtY
 
  
 
    
		 | 
		
		
			
				  Posted: 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 | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		btiffin
 
  
 
    
		 | 
		
		
			
				  Posted: 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 | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		andrew.
 
 
 
    
		 | 
		
		
			
				  Posted: 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
 
  | 	 
  | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		DtY
 
  
 
    
		 | 
		
		
			
				  Posted: 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 | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		andrew.
 
 
 
    
		 | 
		
		
			
				  Posted: 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. | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		btiffin
 
  
 
    
		 | 
		
		
			
				  Posted: 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.
 
 
     
 
 
Cheers | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		DtY
 
  
 
    
		 | 
		
		
			
				  Posted: 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 | 
			 
			
				 | 
			 
		  | 
	
	 
		 | 
		
		 | 
	
	
 
		  | 
	
				 
		 |