Question

In: Computer Science

Today, Raphael was asked by his coworker (a math teacher) tohelp him write a C++...

Today, Raphael was asked by his coworker (a math teacher) to help him write a C++ program that converts an integer from decimal to binary. This (kinda annoying) guy recently learned about the usefulness of stacks, and wants Raphael to write a program that does this using stacks. He will not accept any algorithm that doesn't use stacks.

As an illustrative example of the algorithm that Raphael was asked to implement, say you want to convert 14 into binary. Then you do the following:
1. 14/2 = 7, remainder is 0.
2. 7/2 = 3, remainder is 1.
3. 3/2 = 1, remainder is 1.
4. 1/2 = 0, remainder is 1.
Then the binary value of 14 is 1110.

Using stacks, write a program for Raphael that takes as input a positive integer n (in decimal), and outputs a string of 0's and 1's which represents n in binary.

Solutions

Expert Solution

#include 
#include 

using namespace std;

int main() {
        
        stack bits;
        int num;

        cout << "Enter number: ";
        cin >> num;

        while(num != 0) {
                bits.push(num % 2);
                num = num / 2;
        }

        cout << "binary number: ";
        while(!bits.empty()) {
                cout << bits.top();
                bits.pop();
        }
        cout << endl;
}

Related Solutions

Ken Jones wants to start a small business and has asked his uncle to lend him...
Ken Jones wants to start a small business and has asked his uncle to lend him $10,000. He has prepared a business plan and some financial statements that indicate the business could be very profitable. Ken is afraid his uncle will want some ownership in the company for his investment, but Ken does not want to share what he believes will be a hugely successful company.   1. What are the ethical issues Ken must face as he prepares to present...
Scenario: A teacher asked his students how much time they spent studying for their statistics exam....
Scenario: A teacher asked his students how much time they spent studying for their statistics exam. The results (in minutes) were: 222 161 239 135 209 179 222 201 228 222 235 228 179 47 238 150 99 201 10. Was the median equal to one of the times that was included in the data? Why. 11. For this example, which measure of central tendency would be the most appropriate measure to report? Why. 12. What is the standard abbreviation...
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
Andrew’s doctor told him he is not meeting his RDA for vitamin C based on what...
Andrew’s doctor told him he is not meeting his RDA for vitamin C based on what Andrew told him about his eating habits. Andrew is worried that he will develop scurvy. Can you assume that he is at risk for scurvy if he isn’t consuming the RDA for vitamin C? Why may the doctor’s assessment be flawed? How can you determine if Andrew isn’t consuming enough vitamin C? What would you tell Andrew to calm his fears?
1.   When John Doe opened an account that allows him to write checks to pay for his...
1.   When John Doe opened an account that allows him to write checks to pay for his groceries and other purchases, the bank is providing the service of: A) Currency exchange B) Discounting commercial notes C) Safekeeping of valuables D) Offering demand deposits QUESTION 15 1.   Which of the following would be the most important consideration for locating a new branch bank? A) Large populations of above average age B) Few shops and a rural farm setting C) A rapidly increasing number...
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Write the program in C++ The Rebel food truck has asked you to write a program...
Write the program in C++ The Rebel food truck has asked you to write a program for them to do both inventory and sales for their food wagon. Ben and Dave run the food truck and they already have full time day jobs so they could really use your help. The food truck sells: hamburger, hotdogs, chilli, fries and soda. The truck has spots for 200 hamburger patties, 200 hot dogs, 75 hamburger buns, 75 hot dog buns, 500 ounces...
You are asked to write a simple C++ phonebook application program. Here are the requirements for...
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application. read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing...
You are asked to write a memo to your client, Mr. Carter regarding his tax question....
You are asked to write a memo to your client, Mr. Carter regarding his tax question. ● Your paper should not be more than 2-3 pages, double spaced ● The format should be as follows: ○ Restate the issue / question (first paragraph) ○ Explain the facts in relation to your research (middle paragraphs) ■ Cite a primary or authoritative material at least once ■ Refer the the tax form the information would be reported on ■ Include calculations and...
Write a program in C++ that asks the user for his/her Taxable Income and displays the...
Write a program in C++ that asks the user for his/her Taxable Income and displays the Income Tax owed. Use the following table to calculate the Income Tax: Taxable Income Tax Rate $0 to $9,225 / 10% $9,226 to $37,450 / $922.50 plus 15% of the amount over $9,225 $37,451 to $90,750 / $5,156.25 plus 25% of the amount over $37,450 $90,751 to $189,300 / $18,481.25 plus 28% of the amount over $90,750 $189,301 to $411,500 / $46,075.25 plus 33%...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT