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

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 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
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!!!!
Binary How is 00001001 (base 2) represented in 8-bit two’s complement notation? Convert 0.3828125 to binary...
Binary How is 00001001 (base 2) represented in 8-bit two’s complement notation? Convert 0.3828125 to binary with 4 bits to the right of the binary point. How is 00110100 (base 2) represented in 8-bit one's complement.  
Recall that a 5-bit string is a bit strings of length 5, and a bit string...
Recall that a 5-bit string is a bit strings of length 5, and a bit string of weight 3, say, is one with exactly three 1’s. a. How many 5-bit strings are there? b. How many 5-bit strings have weight 0? c. How many 5-bit strings have weight 1? d. How many 5-bit strings have weight 2? e. How many 5-bit strings have weight 4? f. How many 5-bit strings have weight 5? g. How many 5-bit strings have weight...
1a. Convert 67 (base 10) to 8-bit binary using signed magnitude. Show your work. 1b. Convert...
1a. Convert 67 (base 10) to 8-bit binary using signed magnitude. Show your work. 1b. Convert 69 (base 10) to 8-bit binary using one’s complement. Show your work 1c. Convert 70 (base 10) to 8-bit binary using two’s complement. Show your work. 1d. Convert - 67 (base 10) to 8-bit binary using signed magnitude. 1e. Convert - 67 (base 10) to 8-bit binary using ones compliment. Show your work. 1f. Convert - 67 (base 10) to 8-bit binary using 2s...
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many...
An n-bit binary string is a sequence of length n over the alphabet {0,1}. How many n-bit binary strings are there? How many n-bit binary strings b1,…,bn are there such that b1b2≠00? In other words, how many n-bit binary strings don't begin with 00? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and b2b3≠11? How many n-bit binary strings b1,…,bn are there such that b1b2≠00 and such that b2b3≠01?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT