-
Jun 13th, 2007, 11:59 AM
#1
Thread Starter
New Member
Binary Program, Trace Table Needed!
-
Jun 13th, 2007, 12:06 PM
#2
Re: Binary Program, Trace Table Needed!
Code:
Value = 1
Do While DecimalNumber <> 0 ' Loop until DecimalNumber = 0
Digit = (DecimalNumber Mod 2) ' Mod 2 returns the result of dividing DecimalNumber by 2. the answer is always 0 or 1
BinaryNumber = BinaryNumber & Digit ' Concatenate the new digit to the binary we're building
DecimalNumber = DecimalNumber \ 2 ' Integer division: 7\2 = 3
Value = Value * 2
Loop
Output BinaryNumber
-
Jun 13th, 2007, 01:11 PM
#3
Member
Re: Binary Program, Trace Table Needed!
Well for starters decimal 7 = 111 in binary and 254 = 11111110
(1 mod 2) = 1 not 0 in first example
What's the purpose of value - it's not used for anything?
Looks like an error in the code to me.
Jon
-
Jun 13th, 2007, 03:23 PM
#4
Re: Binary Program, Trace Table Needed!
Value is just calculating the value of a 1 in any given binary position.
-
Jun 13th, 2007, 05:30 PM
#5
Thread Starter
New Member
Re: Binary Program, Trace Table Needed!
hmm i just cant get my head round this whole trace table idea, does any one know a gd tutorial on how to dry run code? thx
-
Jun 13th, 2007, 05:37 PM
#6
Re: Binary Program, Trace Table Needed!
Do you have a specific question about the code?
-
Jun 13th, 2007, 11:50 PM
#7
Re: Binary Program, Trace Table Needed!
The number of digits of a given whole number is int(log(number)/log(10))+1.
There are several threads on getting the binary equivalent, do a search.
If you declared your variable as byte (which is unsigned) then you can convert up to 255, flagging the number as NA can be avoided.
As your already displaying the binary number, what's the output column for?
-
Jun 14th, 2007, 06:07 AM
#8
Thread Starter
New Member
Re: Binary Program, Trace Table Needed!
here is the exact question;
Produce a trace table to check the following piece of code which is intended to convert a Decimal number into the Binary equivalent. BinaryNumber is a String variable all the others are Integers.
Dry-run the code starting with DecimalNumber equal to 7 and again with DecimalNumber equal to 254.
Code:
Value = 1
Do While DecimalNumber <> 0
Digit = (DecimalNumber Mod 2)
BinaryNumber = BinaryNumber & Digit
DecimalNumber = DecimalNumber \ 2
Value = Value * 2
Loop
Output BinaryNumber
Explain your results and suggest any relevant changes to the code?
I really stuck on this, not sure how, before opening this task i have never even open VB never mind read code lol, any help would be greatly appreciated.
-
Jun 14th, 2007, 06:16 AM
#9
Re: Binary Program, Trace Table Needed!
The code is wrong.
EDIT: Although it gets the bits, your placing the lowest order bit to the left of the final string. The concatenation is wrong... and you'll have to post the code your using to fill the grid... cause your getting a 0 bit for decimal number 7 when loop will always produce bit values of 1 from digit=decimalnum mod 2.
Last edited by leinad31; Jun 14th, 2007 at 06:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|