I am making a binary converter on scratch. It's a project that is due on Monday. I am having trouble getting my sprite to say the answer. For example if I have it marked with a 64, 16, 8, and a 2. I need it to tell me that the answer is 90.
Offline
I'm confused. What exactly do you want?
Offline
I need to know how to get my sprite to tell me the answer. It's a binary converter so it's only dealing with ones and zeros. So if I have a one where 64, 16, 8, and 2 are I need my sprite to tell me that the answer is 90. If this makes it more clear. I'm confused just trying to write it down.
Offline
Can you please upload your current project; then I might be able to help you better.
Offline
scimonster wrote:
Can you please upload your current project; then I might be able to help you better.
It sound like he/she needs a way to find the total of the numbers.
If that's the case, add all the numbers to a list named "numbers". Create a variable named "counter" and a variable named "result". Then use:
set [counter v] to (1) set [result v] to (0) repeat (length of [numbers v] ) set [result v] to ( (result) + (item (counter) of [numbers v]) ) change [counter v] by (1) end say (result)You can replace that last block with whatever you want to use to tell the answer.
Offline
Yep, Greenatic got it spot on.
Offline
Ok so assuming that the places for the binary are kept in a list (1st row is 1, second 2, 3rd 4 etc.).
set (total) to [0] set (counter) to [0] repeat [max number of bits of binary number] change (counter) by [1] change (total) by (item (counter) of [binarylist v]) * 2^(counter) endSomething like that...probably replace the 2^counter with values from another list since you can't do ^ in scratch.
Offline
So when you click on the grey box it turns it yellow and the yellow box below it grey and the zero changes to a 1. I need to be able to get my sprite to say the total of the numbers if I had more than one 1 at the bottom.
Offline
Cos140 wrote:
So when you click on the grey box it turns it yellow and the yellow box below it grey and the zero changes to a 1. I need to be able to get my sprite to say the total of the numbers if I had more than one 1 at the bottom.
OK, so create a variable named "counter", a variable named "result", and a list called "numbers". This script, when run, will cause the sprite that runs it to say the answer:
delete [all v] of [numbers v] add ( ( ( [costume # v] of [Sprite23 v] ) - (1) ) * (128) )to [numbers v] add ( ( ( [costume # v] of [Sprite23 v] ) - (1) ) * (64) )to [numbers v] add ( ( ( [costume # v] of [Sprite24 v] ) - (1) ) * (32) )to [numbers v] add ( ( ( [costume # v] of [Sprite25 v] ) - (1) ) * (16) )to [numbers v] add ( ( ( [costume # v] of [Sprite26 v] ) - (1) ) * (8) )to [numbers v] add ( ( ( [costume # v] of [Sprite27 v] ) - (1) ) * (4) )to [numbers v] add ( ( ( [costume # v] of [Sprite28 v] ) - (1) ) * (2) )to [numbers v] add ( ( ( [costume # v] of [Sprite29 v] ) - (1) ) * (1) )to [numbers v] set [counter v] to (1) set [result v] to (0) repeat (length of [numbers v] ) set [result v] to ( (result) + (item (counter) of [numbers v]) ) change [counter v] by (1) end say (result)
Last edited by Greenatic (2012-02-05 17:29:05)
Offline
Cos140 wrote:
Thank you so much!!
No problem.
Offline