Hey, I have never programmed before, so everything is very confusing for me. I was given an assignment to change binary to a decimal number. Not very much information was given to me. I am NOT looking for an answer, but I definitely am looking for some help as to understand what I am suppose to be doing. Please Help!

**Exercise 1
Binary to Decimal Number Conversion
5 Points

1 Exercise 1**
The included programnamed ModifyMe:cpp will take the input of a binary num-ber up to 8 digits and output the sum of those binary digits. You are tomodify it so
that it will take as input an unsigned binary number up to 16 digits in length and
convert it into its equivalent decimal number and output that decimal number. To
verify your program, unsigned binary number of 10010001 should be converted
to 145. You may use library function strlen() in < math:h > to calculate the
length of the input string.
Hint: 2
4
is calculated by
pow(2,4); //pow(); is found in math.h

Code given:

#include <iostream> // include a header file for input/output facility cin/cout
// using namespace std;
int main() // main entry point; in standard C++ such as Dev-C++, main() has to return an int
{
       char b[8];  // declare an array of element char elements
       cout << "Please input a binary number up to 8 digits...\n\n"; // output the string after output       // operator <<
       cin >> b; // input sequence of binary digits into array b
       int sum = 0, i=0; // declare and initalize sum (to store converted decimal number) and i 
// (index of array from 0 to 7)
       while (i<8 && b[i] != (char)0) {
//each element of b[] is initialized to (char)0 – NUL
// it loops through all the input digits up to 8; when the loop hits (char)0, that is the end of input; e.g.
// if input is 10010, b would be b = ['1', '0', '0', '1', '0', (char)0, (char)0, (char)0]. When loop finishes // the first 5 elements and hits (char)0 in blue, it will jump out of loop.
              if (b[i] == '1') sum++; // if the digit is a '1', increment sum by 1
              else if (b[i] == '0') ;  // if the digit is a '0', do nothing
              else cout << " you are not inputing a binary number! \n"; // if the digit is not a '1' or '0', // // output an error message
              i++; // loop to the next element
       }
       cout << sum; // output the calculated sum
       char c; cin >> c; // hold the output screen
       return 0; // return an integer, usually a 0
} //end main

Recommended Answers

All 2 Replies

Seems like you need to first understand how to convert binary to decimal before actually programming it. Take for example the binary number 1010 which is 10 in decimal. How can we get to it mathmatically? Pretty easy actually, what you can do is sum up pow(2,nextOnePosition). For example,

1010 in base10 = pow(2,3) + pow(2,1) = 10
^^^^
3210 = position of each bit

Where did I get those numbers for pow function? The 2 is from it being base 2. The 3 is because there is a 1 in the 3rd bit. Similarly, for pow(2,1), the 2 is there because of base 2, and the 1 is there because there is a 1 in bit position 1. In general the base2 to base10 conversion is as follows:

Given a binary number, let b_i be the bit at ith position, where i = 0 is the right most bit and let 'n' be the length of the binary number. The formula to convert base2 to base10 is:

SUM(b_i)  = b_i * pow(2,i) 
i = 0 to n

Note, if b_i is 0 then b_i * pow(2,i) result in 0, thus being ineffective. But if b_i = 1 then we add pow(2,i). 

I would finish this up with the code but I'll let you work on that since you asked not to write the code. Also as a tip, use std::string if possible instead of char[8]. They work pretty much same char raw char arrays, but are safer and easier to work with. For example:

std::string binaryNumber;
cin >> binaryNumber;
//print each binary bit by bit
for(int i = 0; i < binaryNumber.size(); ++i){
     cout << binaryNumber[i] << endl;
}

That should be a good starting point. Post back here if you need more help.

Another method is coming from the observation and you don't need math.h.

Let's have a look at the definition (see previous post):

1010 in base10 = pow(2,3) + pow(2,1) = 10
^^^^
3210 = position of each bit

So, 1010 is converted from base 2 to base 10 like this:

1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10

From here, one method (as shown in the above post) is to compute the power of 2 and to multiply it with 0 or 1, depending of the existing value on the given position.

Let us now rewrite the previous example in an iterative way (left to right) using brackets:

((1*2 + 0)*2 + 1)*2 + 0 = ((2+0)*2+1)*2 = (4+1)*2 = 10

Generalizing, that means:

Let S be a string of 1's and 0's
Let I be the char position in S
Let B_S (base 10 transformation) be 0
For I from first position to the last position
    B_S = 2*B_S + (int)S[I]

Just to see if this algorithm works:

S = 1010
I = 0,1,2,3
B_S = 0
1st step: B_S = 2*B_S + 1 => B_S = 2*0+1 = 1
2nd step: B_S = 2*B_S + 0 => B_S = 2*1+0 = 2
3rd step: B_S = 2*B_S + 1 => B_S = 2*2+1 = 5
4th step: B_S = 2*B_S + 0 => B_S = 2*5+0 = 10
Stop (we reached the end of the string)

Notes:

  1. You don't need to specify the length of the string (nevertheless, you need to specify a maximum because of int capacity).
  2. You don't need to have knowledge of the string length from before.
  3. You can use left shift (<<) operator on B_S instead of multiplying by 2, if you prefer).

Good luck! If you have questions, let us know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.