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