In: Computer Science
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; }
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.