1 Attachment(s)
Binary Program, Trace Table Needed!
Hi i need to create a trace table for a piece of code that was given to me for my studies, how ever we have been given no lessons on how to do it, and i cant seem to find any where that explains it. The idea of the program is to convert decimal numbers to binary. Well here is the code;
Code:
Value = 1
Do While DecimalNumber <> 0
Digit = (DecimalNumber Mod 2)
BinaryNumber = BinaryNumber & Digit
DecimalNumber = DecimalNumber \ 2
Value = Value * 2
Loop
Output BinaryNumber
I need to Dry-run the code starting with DecimalNumber equal to 7 and again with DecimalNumber equal to 254. Below is what i have done however it has been marked wrong;
http://www.vbforums.com/attachment.p...1&d=1181753892
Can anyone help, or know a good tutorial on how to do these, thx in advance.
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
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
Re: Binary Program, Trace Table Needed!
Value is just calculating the value of a 1 in any given binary position.
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
Re: Binary Program, Trace Table Needed!
Do you have a specific question about the code?
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?
Re: Binary Program, Trace Table Needed!
here is the exact question;
Quote:
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.
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.