Computer Science Canada using mod |
Author: | foo fighter [ Thu Nov 24, 2005 5:44 pm ] |
Post subject: | using mod |
Ok here's what i want my program to do: I want the user to input a number. Then its divided by 2 continuosly until its at zero and for each time it divides if theres a demainder i want it to output a 1 and if no remaider then output a 0. my friend said i should use mod, but i cant figure it out ![]() |
Author: | MysticVegeta [ Thu Nov 24, 2005 5:50 pm ] |
Post subject: | |
remainder is the mod function. for example: put 4 mod 2. Output will be 0 Because 4/2 = 2. Remainder = 0 |
Author: | foo fighter [ Thu Nov 24, 2005 6:24 pm ] |
Post subject: | |
ummm... okay, but its not rounding numbers with remainders of 1, it just puts them as 0.5 |
Author: | person [ Thu Nov 24, 2005 6:56 pm ] |
Post subject: | |
x:= x div 2 y:= x mod 2 put x,y[/quote] |
Author: | [Gandalf] [ Fri Nov 25, 2005 4:38 pm ] |
Post subject: | |
Yes, as person's example shows, you must use the div command for the mod (remainder) to be accurate. |
Author: | Dan [ Fri Nov 25, 2005 5:16 pm ] |
Post subject: | |
it should be noted that such a progame whould never aucatly hit 0 unless sigficant rounding was used..... |
Author: | person [ Fri Nov 25, 2005 6:50 pm ] |
Post subject: | |
put 1 div 2 outputs 0 |
Author: | Dan [ Fri Nov 25, 2005 9:58 pm ] |
Post subject: | |
person wrote: put 1 div 2
outputs 0 Thats b/c the div comand is not the same as dividing 2 numbers. It dvides them and then cuts of the decmans (ether by round or truncating them, i whold have to check). Since he side divides and not divides and truncates........ Tho i blive that at some point turing whould start round even the number divide using the noraml divsion comand. But a exit condtion based soly on that whould not be good progaming. |