binary to decimal using array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

binary to decimal using array

rumonrumon Posts: 4Member
Okay i'm not that great at C++ as you will see from how far i got until i hit a wall.

The problem is this

Allows the user to input a series of 0's and 1's which will be stored as an array representing a binary word. Input should stop when something other than a 0 or a 1 is input by the user and display the decimal equivalent.

To convert i think i got the concept but i just cant seem to implement it

binary 11010

take each number individually and multiply it by 2 to the power of the placement of the number - 1.

so you start with the number far left and move right:

(1*2^4)+(1*2^3)+(0*2^2)+(1*2^1)+(0*2^0) = 26

so i had a crack at it

[code]#include
#include
using namespace std;

void main ()
{
int power;
char binary [16],*p; //array size declared
p=binary;

cout<<"Enter binary input(1's & 0's):";

cin>>binary;

cout<<"
Binary Input is:"<<binary<<endl; //display array output

for (power=strlen(binary)-1;power>-1; power--) //calculates the power

cout<<power<<endl; //test output
}
[/code]


i'm sure there need to be another loop to retrieve the array values. then its a simple task of calculating the output

sum + = binary[i] * pow(2, power)


This is probably easy but i have been working on my other units and made the mistake of assuming it was easier then it looked :(

thanks in advance.
rumon

«1

Comments

  • HrishiOA007HrishiOA007 Posts: 5Member
    : Okay i'm not that great at C++ as you will see from how far i got
    : until i hit a wall.
    :
    : The problem is this
    :
    : Allows the user to input a series of 0's and 1's which will be
    : stored as an array representing a binary word. Input should stop
    : when something other than a 0 or a 1 is input by the user and
    : display the decimal equivalent.
    :
    : To convert i think i got the concept but i just cant seem to
    : implement it
    :
    : binary 11010
    :
    : take each number individually and multiply it by 2 to the power of
    : the placement of the number - 1.
    :
    : so you start with the number far left and move right:
    :
    : (1*2^4)+(1*2^3)+(0*2^2)+(1*2^1)+(0*2^0) = 26
    :
    : so i had a crack at it
    :
    : [code]: #include
    : #include
    : using namespace std;
    :
    : void main ()
    : {
    : int power;
    : char binary [16],*p; //array size declared
    : p=binary;
    :
    : cout<<"Enter binary input(1's & 0's):";
    :
    : cin>>binary;
    :
    : cout<<"
    Binary Input is:"<<binary<<endl; //display array output
    :
    : for (power=strlen(binary)-1;power>-1; power--) //calculates the power
    :
    : cout<<power<<endl; //test output
    : }
    : [/code]:
    :
    :
    : i'm sure there need to be another loop to retrieve the array values.
    : then its a simple task of calculating the output
    :
    : sum + = binary[i] * pow(2, power)
    :
    :
    : This is probably easy but i have been working on my other units and
    : made the mistake of assuming it was easier then it looked :(
    :
    : thanks in advance.
    : rumon
    :
    :

    There seems to be a better way. Ru through the array (use another pointer *x, not any extra vars) until you hit a value you don't want. Then use a loop (i) from 0 till you reach the beginning (old *p), and as it increments use pow(2,i). don't use binary[i], use sum += *x * pow(power,i).

    Doubts?


  • rumonrumon Posts: 4Member

    : There seems to be a better way. Ru through the array (use another
    : pointer *x, not any extra vars) until you hit a value you don't
    : want. Then use a loop (i) from 0 till you reach the beginning (old
    : *p), and as it increments use pow(2,i). don't use binary[i], use sum
    : += *x * pow(power,i).
    :
    : Doubts?
    :
    :
    : Thank you for the post. How exactly would i implement a second pointer?
    would it be

    char binary [16], *p,*x;
    p=binary;
    x=p;

    not quite sure on how its going to use the 2 points to convert to decimal.

  • HrishiOA007HrishiOA007 Posts: 5Member
    :
    : : There seems to be a better way. Ru through the array (use another
    : : pointer *x, not any extra vars) until you hit a value you don't
    : : want. Then use a loop (i) from 0 till you reach the beginning (old
    : : *p), and as it increments use pow(2,i). don't use binary[i], use sum
    : : += *x * pow(power,i).
    : :
    : : Doubts?
    : :
    : :
    : : Thank you for the post. How exactly would i implement a second pointer?
    : would it be
    :
    : char binary [16], *p,*x;
    : p=binary;
    : x=p;
    :
    : not quite sure on how its going to use the 2 points to convert to
    : decimal.
    :
    :
    You're right in the code, almost.
    Sorry, I wanted to post the full code, I've done this before, but I haven't got much time, I'm on a tight schedule.

    You just equalise the two pointers by [code]x = p;[/code];
    then run the array till the end with [code]while(*x++ < 2) ;[/code];
    Then using a for loop and a variable i, [code]for(i=0;x!=p;i++,x--)[/code]
    You can implement the algorithm using [code]sum == *x * pow(2,i);[/code]
    If you have any doubts, please contact me.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]
    This seems way more complex than it is needed. pow() also kind of slow. All what is needed here is to have a mask of bits, like that one: 0000 0000 0000 0000 0001. Then begin from the end of the input string checking for '0' and '1' characters. If '1' is entered, then OR that mask with a result variable (which is zero at the beginning of this loop). When looking at the next character - mask also shifts LEFT by one bit:
    [/color]
    [code]
    unsigned int BinaryStringToHex32 (char* s)
    {
    int nBits = strlen (s);
    unsigned int result = 0;
    unsigned int mask = 1;

    s += nBits;
    while (nBits--)
    {
    s--;
    if (*s == '1') result |= mask;
    mask <<= 1;
    }
    return result;
    }
    [/code]
  • rumonrumon Posts: 4Member
    HrishiOA007, AsmGuru62 Thank you for helping out a first year student ^^ with his C++ homework really appreciate it. Got java second semester lesson learn. Got to dedicate a little more to getting my head around the programming language.

    anyway thanks again, really help out :)
  • HrishiOA007HrishiOA007 Posts: 5Member
    : HrishiOA007, AsmGuru62 Thank you for helping out a first year
    : student ^^ with his C++ homework really appreciate it. Got java
    : second semester lesson learn. Got to dedicate a little more to
    : getting my head around the programming language.
    :
    : anyway thanks again, really help out :)
    :
    Anytime, man.......
    And ASMGuru, I was just trying to tell this first year student, not a boolean master. Frankly, even I've had problems earlier with MASKS and bitmaps in VC++. Who cares for speed with today's processors? Its a few instructions more or less in such small programs...
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : : HrishiOA007, AsmGuru62 Thank you for helping out a first year
    : : student ^^ with his C++ homework really appreciate it. Got java
    : : second semester lesson learn. Got to dedicate a little more to
    : : getting my head around the programming language.
    : :
    : : anyway thanks again, really help out :)
    : :
    : Anytime, man.......
    : And ASMGuru, I was just trying to tell this first year student, not
    : a boolean master. Frankly, even I've had problems earlier with MASKS
    : and bitmaps in VC++. Who cares for speed with today's processors?
    : Its a few instructions more or less in such small programs...
    :
    [color=Blue]I agree, of course.

    My apologies...
    It's just when I see a lot of CPU cycles spent - I always want to point the better solution.

    Your code was fine too.[/color]
  • Ed HallEd Hall Posts: 395Member
    What say you guys to this version:

    [code]
    #include
    #include

    int main()
    {
    char *input="10110112";
    int i=0, j=0;

    while (i<strlen(input) && (input[i]=='1' || input[i]=='0'))
    j=j*2+(input[i++]-48);

    printf("The decimal equivalent is %d.

    ", j);


    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [/code]

    Take Care,
    Ed
  • HrishiOA007HrishiOA007 Posts: 5Member
    : What say you guys to this version:
    :
    : [code]:
    : #include
    : #include
    :
    : int main()
    : {
    : char *input="10110112";
    : int i=0, j=0;
    :
    : while (i<strlen(input) && (input[i]=='1' || input[i]=='0'))
    : j=j*2+(input[i++]-48);
    :
    : printf("The decimal equivalent is %d.

    ", j);
    :
    :
    : system("PAUSE");
    : return EXIT_SUCCESS;
    : }
    : [/code]:
    :
    : Take Care,
    : Ed
    :

    Good, but you won't get a null character terminated string during input
    unless you use strin. Nice work!!
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]strlen() in a loop. ..mmm... sweet cycles... :-)[/color]
«1
Sign In or Register to comment.