Question

In: Computer Science

C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...

C++

1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2.

2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. Write a function isPalindrome() that takes a string as a parameter and uses a stack to test whether a string is a palindrome.

3. Suppose that you read a binary string—that is, a string of 0s and 1s—one character at a time. Write a function that use a stack but no arithmetic to see whether the number of 0s is equal to the number of 1s. When these counts are not equal, show which character—0 or 1—occurs most frequently and by how much its count exceeds the other’s.

4. Write a program to test the four functions above.

Solutions

Expert Solution

Code

#include <bits/stdc++.h> //includes the stack and all necessary header diles

using namespace std;

//dispay the binary of a number

void decimalToBinary(int num)

{

    stack<int> s;   //define a stack

    int tnum = num; //copy the number

    while (tnum > 0)

    {

        s.push(tnum % 2);

        tnum = tnum / 2;

    }

    cout << "The binary of " << num << " is : ";

    //now print the stack contents

    while (!s.empty())

    {

        cout << s.top();

        s.pop();

    }

}

//isPalindrome method

void isPalindrome(string s)

{

    stack<char> st;

    for (int i = 0; i < s.length(); i++)

        if (isalnum(s[i]))

            st.push(s[i]);

    //now check the character

    for (int i = 0; i < s.length(); i++)

    {

        if (!isalnum(s[i]))

            continue;

        if (tolower(s[i]) != tolower(st.top()))

        {

            cout << "\n"

                 << s << " is not palindrome\n";

            return;

        }

        st.pop();

    }

    cout << "\n"

         << s << " is palindrome\n";

}

//checkString method

void checkString()

{

    //input a string from user

    string s;

    cout << "\nEnter a binary string : ";

    cin >> s;

    stack<char> s1, s2;

    for (int i = 0; i < s.length(); i++)

    {

        if (s[i] == '0')

            s1.push(s[i]);

        else

            s2.push(s[i]);

    }

    if (s1.size() > s2.size())

    {

        cout << "\n0's are more than 1's by " << (s1.size() - s2.size());

    }

    else if (s2.size() > s1.size())

    {

        cout << "\n1's are more than 0's by " << (s2.size() - s1.size());

    }

    else

        cout << "\nNumber of 1's and 0's are same";

}

//driver code of the program

int main()

{

    //call decimalToBinary() method

    decimalToBinary(19);

    //call the isPalindrome method

    isPalindrome("Race car");

    //call checkString method

    checkString();

}

Screesnhot

Output


Related Solutions

(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws...
Write a function, namely shape(s) that takes a positive integer s as the parameter and draws a square of length s. The square should be filled by a randomized color. Then write another function, that calls the shape function to draw 4 squares as a 2x2 table. Please answer in python.
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the...
python exercise: a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. sample: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a...
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
a. Write a function sumDigits that takes a positive integer value and returns the total sum...
a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. Sample Runs: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a positive...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT