# Storing variables in an array

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 4 years ago

I am trying to create code that converts a number to binary. I have the main code done I think..but I want to store the elements in an array and then possibly reverse the order to show the binary form.

I set "n = 1" and then label the array with "n" elements so it can get all the numbers after the final iteration. I just need to know how to store "r" in that array, and also could I possibly reverse the array by putting [ n-1] as the output?

I set "n = 1" and then label the array with "n" elements so it can get all the numbers after the final iteration. I just need to know how to store "r" in that array, and also could I possibly reverse the array by putting [ n-1] as the output?

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 4 years ago

I think what you're trying to do is convert an integer to a binary string. For example, for the input 11, the output would be "1011". Is that right? The reason you might need to reverse an array is that it's easier to figure out the bits from right to left.

The calculations you're doing look OK to me, but you're just printing your results to the screen. What you want to do is start building up a String with the r's as you calculate them. The last thing you'd do then is reverse the String.

This creates an array with n elements with indexes from 0 to n-1. The means a[n] is definitely going to be a problem, because that's one past the last element. Now, "r" is what you put in the array, so using it as an index doesn't make sense either. That is, binary[r] isn't what you want to be doing.

The calculations you're doing look OK to me, but you're just printing your results to the screen. What you want to do is start building up a String with the r's as you calculate them. The last thing you'd do then is reverse the String.

This creates an array with n elements with indexes from 0 to n-1. The means a[n] is definitely going to be a problem, because that's one past the last element. Now, "r" is what you put in the array, so using it as an index doesn't make sense either. That is, binary[r] isn't what you want to be doing.

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 4 years ago

Oh ok I see what you are doing. I have to save the "r" which is my remainder as a String and then I can store them in the array through iterations? I saw the Integer class thing when I did a google search, but we aren't allowed to use that class. Our instructor actually wants us to create the code. I have to do conversions from 2, 8, 10 and 16 and back. I think thats like 32 different methods right lol. But someone told me that its easier to turn any other base that isn't binary to binary and then from binary to the base. So I am just trying to code this first and then go about it.

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 4 years ago

No, you surely don't want 32 methods, but the loop you have (I think) does the job for you.

Let's say you start with an input of 11.

11 % 2 = 1 (1 is the first thing you've computed)

11 / 2 = 5 (Integer division truncates away the remainder. This is next input for your calculations)

5 % 2 = 1 (1 is the next computation)

5 / 2 = 2 (This is the next input)

2 % 2 = 0 (0 is the next computation)

2 / 2 = 1 (1 is the next input)

1 % 2 = 1 (1 is the next calculation)

1 / 2 = 0 (done)

OK, so all your computed values are 1,1,0,1. You need to reverse that to get "1101". That's the right answer for the binary of 11. The cool thing is that the same process will work on any positive integer you give it.

Let's say you start with an input of 11.

11 % 2 = 1 (1 is the first thing you've computed)

11 / 2 = 5 (Integer division truncates away the remainder. This is next input for your calculations)

5 % 2 = 1 (1 is the next computation)

5 / 2 = 2 (This is the next input)

2 % 2 = 0 (0 is the next computation)

2 / 2 = 1 (1 is the next input)

1 % 2 = 1 (1 is the next calculation)

1 / 2 = 0 (done)

OK, so all your computed values are 1,1,0,1. You need to reverse that to get "1101". That's the right answer for the binary of 11. The cool thing is that the same process will work on any positive integer you give it.

Tarrell Fletcher

Ranch Hand

Posts: 60

posted 4 years ago

Yea I just revised my program it will display the remainders, but I have to figure out how to store them so I can reverse the order to get the binary form. Here's my new loop statement that works perfectly.

I tried it with different numbers and I get I get the answer in binary form but it isn't exactly binary until I reverse the order.

I tried it with different numbers and I get I get the answer in binary form but it isn't exactly binary until I reverse the order.

Campbell Ritchie

Sheriff

Posts: 48981

60

posted 4 years ago

The reason is that when you do the

What you want to do is to create an array big enough to hold all the bits (remember an

`i % 2`bit, you are working out whether the number is even or odd,__or__whether the last digit is 0 or 1. Then you divide by 2, and repeat the process to find whether the quotient was even or odd, which gives the value of the penultimate bit. Etc., etc.What you want to do is to create an array big enough to hold all the bits (remember an

`int`occupies 4 bytes = 32 bits of memory), and fill it in from right to left.Since an`int[]`is filled with 0s as a default, you will get`{0, 0, ..., 1, 0, 1, 1}`from 11.
Campbell Ritchie

Sheriff

Posts: 48981

60

posted 4 years ago

Think like a programmer. If you don't have it (or know where to find it), write it:

Winston

Tarrell Fletcher wrote:so I can reverse the order to get the binary form...

Think like a programmer. If you don't have it (or know where to find it), write it:

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

Tarrell Fletcher

Ranch Hand

Posts: 60

Campbell Ritchie

Sheriff

Posts: 48981

60

posted 4 years ago

You cannot create a StringBuilder with a

`char`in its constructor. Go through the different StringBuilder constructors and work out what the JVM will interpret that as. And no, it will not convert it to a String.