• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lost code Binary to decimal using array  RSS feed

 
katrina arquiza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

im stuck at my code for our lab Homework. Please help me. I do not know how to convert a reversed array to decimal.

The output should be:

How many digits to convert?
(user will input) ex. 3
Please input digits:
(user will input 3 digits)
ex. 0
1
1
The binary digits are: 110 (<-reversed)
The decimal value is: 6

Here is my code:

 
subodh kureel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to add this code..

where mbinary is binary digit variable...otherwise its basic calculation to convert binary to decimal.

while(true){
            if(mbinary == 0){
                break;
            } else {
                int temp = mbinary%10;
                decimal += temp*Math.pow(2, power);
                mbinary = mbinary/10;
                power++;
            }
        }
 
katrina arquiza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
those variables are not declared and where will i add it? thank you
 
katrina arquiza
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output:

Welcome to Number Systems Conversion
Please choose option:
a. Binary to Decimal
b. Octal to Decimal
c. Hexadecimal to Decimal
Answer: a
How many digits to convert?
3
Please input digits:
0
1
1
The binary number is: 110
Decimal value is: 3

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
subodh kureel wrote:try to add this code..
. . .
I would recommend you try to avoid such code. For a start you should get suspicious of anything with while (true) in.
 
subodh kureel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell , I do agree ..while(true) should be avoided..my bad..thanks for pointing it put..

Katrina,
Please try below..i can fix your code but i believe objective is to provide the guidance.. hope this help...

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the objective is to provide guidance, but you are not doing that. You are providing a solution. That does not help the OP learn at all. I suspect the damage has been done and it is too late to undo, but by providing a complete solution you have “excused” OP from thinking through her submission. Look what it says on the contents page of this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Don't be annoyed with me, but I have pulled rank and deleted your solution.

I might replace it in a couple of days, and ask you to explain it, because I am not convinced it is a particularly good solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!