The problem for my CMPT103 class is to write a program that takes a binary number as an integer and returns it as a decimal, i would be able to do this easily by taking it as a charecter array and dealing with each individual element but i dont understand how to do this. my teacher gave us 3 functions we have to use and a descrption of what theyre supposed to do:

• bool convertBin(int &binary);
This function will take an integer value that is in binary and change it to a decimal value. In the event that the conversion is not possible (for example, trying to convert 6 from binary to decimal), the function will return false and leave binary untouched. If the conversion is possible, then binary will contain the converted number, and the function will return true.
• int lastDig(int num);
This function returns the last digit of num.
• int removeLast(int num);
This function returns a number without the last digit. For example, removeLast(123) would return 12. The body of this function can be accomplished with one line.


i assume im supposed to use lastDIG to make sure its a 0 or 1 and then evalate it, based on how many times ive used removeLast, then use removeLast to get rid of it and continue but i dont know how to deal with each digit? please help! thanks!

P.S. sorry i screwed up the title it should say binary not boolean

Let's start with these two functions first:

• int lastDig(int num);
This function returns the last digit of num.

If you pass in the value 100101101 this function will return 1. You can do this using the modulus (%) operator.

• int removeLast(int num);
This function returns a number without the last digit. For example, removeLast(123) would return 12. The body of this function can be accomplished with one line.

Divide.


Remember, you have a binary representation in integer form. It's still an integer, in base 10, not base 2.

thanks i think ive almost got it but i know im making some stupid mistake

int removeLast(int &num){
    num=num/10;}


int convertBin(int binary){
    int i;
    int count;
    int sum;
    sum=0;
    count=1;
    for (i=0;binary!=0;i++){
    if(binary % 2==1){
    sum=2^count;
    binary=binary-1;}
    count++;
    removeLast(binary);}
}

i apologize for my ignorance but it seems to work when i trace it out on paper?

Let's start with these two functions first:

If you pass in the value 100101101 this function will return 1. You can do this using the modulus (%) operator.

Divide.


Remember, you have a binary representation in integer form. It's still an integer, in base 10, not base 2.

thanks i think ive almost got it but i know im making some stupid mistake

int removeLast(int &num){
    num=num/10;}


int convertBin(int binary){
    int i;
    int count;
    int sum;
    sum=0;
    count=1;
    for (i=0;binary!=0;i++){
    if(binary % 2==1){
    sum=2^count;
    binary=binary-1;}
    count++;
    removeLast(binary);}
}

int removeLast(int &num){ num=num/10;} int convertBin(int binary){ int i; int count; int sum; sum=0; count=1; for (i=0;binary!=0;i++){ if(binary % 2==1){ sum=2^count; binary=binary-1;} count++; removeLast(binary);} }
i apologize for my ignorance but it seems to work when i trace it out on paper?

You should have a warning about removeLast() . Did you see it? If so, did you ignore it?

What's all that at the end of your post?

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.