Question

In: Computer Science

How do i make this program accept strings? #include <iostream> #include <algorithm> using namespace std; int...

How do i make this program accept strings?


#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;
}

Solutions

Expert Solution

#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()
{
    // **** I have updated here
    string arr;
    cout << "Enter the binary string: ";
    cin >> arr;

    int n = arr.size();

    // ****

    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:-

If you have any doubt, feel free to ask in the comments.


Related Solutions

write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice; int biden = 0 , trump = 0 , bugs = 0 ; int vc = 0 ; do { cout<<"\n\n\nEVOTE\n-----" <<"\n1.Joe Biden" <<"\n2.Donald Trump" <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote [hidden hacker option] E.C. // 7. END THE ELECTION <<"\n\n Your selection? "; cin >>...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int...
Question 1 Given the program below, what are the errors. #include <iostream> using namespace std; int main() { int ind_square(int &); int x, *p; x=15; p = &x; ind_square(*p); } int ind_square(int &p) { *p = *p **p; } Question 1 options: C. No return for non-void function A. Indirection for the variables in ind_square requires pointer operand A and B B. Invalided use of address (&) symbol as a parameter for ind_squared A and C Question 2 Which statement...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function...
What is the output of the following program? #include <iostream> using namespace std; void showDouble(int); //Function prototype int main() { int num; for (num = 0; num < 10; num++) showDouble(num); return 0; } // Definition of function showDouble void showDouble(int value) { cout << value << ‘\t’ << (value * 2) << endl; } Please do the following Program and hand in the code and sample runs. Write a program using the following function prototypes: double getLength(); double getWidth();...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT