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; }...
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; }
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int binarySearch(int [], int, int);  // function prototype const int SIZE = 16; int main() { int found, value; int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched cout << "Enter an integer to search for:" << endl; cin >> value; found = binarySearch(array, SIZE, value); //function call to perform the binary search   //on array looking for an occurrence of value if (found == -1) cout...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT