Question

In: Computer Science

Design a transducer to convert a binary string into octal. For example the bit string 001101110...

Design a transducer to convert a binary string into octal. For example the bit string 001101110 should produce 156.

Please complete the code to account for the 7 cases of 3 digit binary strings.

//Function binaryToOctal takes a char array digits of length 3

//Pre: digits contains 3 binary digits.

//Post: function returns the octal number equivalent to the 3 binary digits

int binaryToOctal( char digits[], int 3){

int number;

if(digits[0]=='0')

if (digits[1]=='1')

if (digits[2]=='0')

return 2;//found "010"

else return 3;//found "011"

else if(digits[1]=='0')

if (digits[2]== '1'

return 1// found "001"

.......//keep listing all combinaisons until all are considered.

C++

Complete code

Use arr declared in main function for testing

#include <iostream>
using namespace std;
int binaryToOctal( char digits[], int a){
int number;
if(digits[0]=='0'){

        if (digits[1]=='1')
                if (digits[2]=='0')
                        return 2;//found "010"
                 else return 3;//found "011"
        else if(digits[1]=='0')
                if (digits[2]== '1')
                        return 1;// found "001"
                else return 0;//found "000"     
}
else{ 
if(digits[0]=='1')
    if (digits[1]=='1')
                if (digits[2]=='0')
                        return 6;//found "110"
                else return 7;//found "111"
        else if(digits[1]='0')
            if(digits[1]=='0')
                if (digits[2]== '1')
                        return 5;// found "101"
                else return 4;//found "100"
                }
                
}
int main(){
    char arr[3]={'1','0','0'};
int ans=binaryToOctal(arr,3);
cout<<ans;
                        
return 0;                       
}

Solutions

Expert Solution

Step1:

We will traverse the given array from back and take 3 values from array every time .

We will appropriately check the size left of array and append 0 as required as:

  //for three values
        if (i >= 2)
        {
            char temp[3] = {arr[i - 2], arr[i - 1], arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i -= 3;
        }
        //but if only size left with 2 then add one 0 and take two element
        else if (i == 1)
        {
            char temp[3] = {0, arr[i - 1], arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i -= 2;
        }
        //similarly add two 0 then one value for size 1 left
        else if (i == 0)
        {
            char temp[3] = {0, 0, arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i--;
        }
        else
        {
            i--;
        }

Step2:

Appending to string after converting to integer as:

res += (ans + '0');

Step3:

Code: Evrything about code is explained in comments of code:

#include <iostream>
#include <algorithm>
using namespace std;
int binaryToOctal(char digits[], int a)
{
    int number;
    if (digits[0] == '0')
    {

        if (digits[1] == '1')
            if (digits[2] == '0')
                return 2; //found "010"
            else
                return 3; //found "011"
        else if (digits[1] == '0')
            if (digits[2] == '1')
                return 1; // found "001"
            else
                return 0; //found "000"
    }
    else
    {
        if (digits[0] == '1')
            if (digits[1] == '1')
                if (digits[2] == '0')
                    return 6; //found "110"
                else
                    return 7; //found "111"
            else if (digits[1] = '0')
                if (digits[1] == '0')
                    if (digits[2] == '1')
                        return 5; // found "101"
                    else
                        return 4; //found "100"
    }
}
int main()
{
    char arr[] = {'0', '0', '1', '1', '0', '1', '1', '1', '0'};
    int n = (sizeof(arr) / sizeof(arr[0]));
    cout << "For the input array : ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
    cout << "Octal is: ";
    int i = n - 1;
    string res = "";
    //traverse the loop from back and take three values from left to right
    while (i >= 0)
    {
        //for three values
        if (i >= 2)
        {
            char temp[3] = {arr[i - 2], arr[i - 1], arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i -= 3;
        }
        //but if only size left with 2 then add one 0 and take two element
        else if (i == 1)
        {
            char temp[3] = {0, arr[i - 1], arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i -= 2;
        }
        //similarly add two 0 then one value for size 1 left
        else if (i == 0)
        {
            char temp[3] = {0, 0, arr[i]};
            //call function for finding the octal in three
            int ans = binaryToOctal(temp, 3);
            //appending to new string  after converting the integer to char
            res += (ans + '0');
            i--;
        }
        else
        {
            i--;
        }
    }
    //reverse the resultant string to get the octal values
    reverse(res.begin(), res.end());
    cout << res << endl;
    return 0;
}

Output:

Thanks.


Related Solutions

Design a Decoder Circuit that can convert a 4-bit Binary Number to a Hexadecimal Output.
Design a Decoder Circuit that can convert a 4-bit Binary Number to a Hexadecimal Output.
Convert 7A216 to octal
Convert 7A216 to octal
Convert the following to Octal, hexadecimal and binary (long method (multiply and Divide by methods)) 2647.95...
Convert the following to Octal, hexadecimal and binary (long method (multiply and Divide by methods)) 2647.95 (10)
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Convert the following decimal number into (a) binary and (b) Octal (SHOW ALL STEPS) 205.75
Design a 5 bit binary counter on logicly?
Design a 5 bit binary counter on logicly?
Convert binary to the following 100110101.1011101101)2 a.) Octal ( )8 b.) Hexa  ( )16 steps would be...
Convert binary to the following 100110101.1011101101)2 a.) Octal ( )8 b.) Hexa  ( )16 steps would be helpful thanks
Convert 9F.216 to octal
Convert 9F.216 to octal
1) Convert (0.513)10 to octal. 2) Given the two binary numbers X = 1010100 and Y...
1) Convert (0.513)10 to octal. 2) Given the two binary numbers X = 1010100 and Y = 1000011, perform the subtraction (a) X - Y and (b) Y - X by using 2’s complements. 4) Simplify the Boolean function and draw the logic diagram to implement the function (i) F(a,b,c,d) = ∑(0,1,9,12,13,14) (ii) F(a,b,c,d) = ∑(0,2,3,5,9,10,15) with don’t care d(a,b,c,d) = ∑(1,3,7,8,11,) 5) Implement the Boolean expression F (A, B, C, D) = _(1, 3, 4,7, 8,12, 13, 14, 15)...
Convert CC53 (hexidecimal) to the octal equivalent
Convert CC53 (hexidecimal) to the octal equivalent
Convert the following numbers to 8-bit binary and 8-bit hexadecimal: a) 20 b) 78 c) -25...
Convert the following numbers to 8-bit binary and 8-bit hexadecimal: a) 20 b) 78 c) -25 d) -96 Convert the following hexadecimal numbers to binary and decimal assuming two's compliment format: a) 0x56 b) 0x14 c) 0xF8 d) 0xCC MUST DO ALL PROBLEMS AND SHOW ALL WORK!!!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT