Question

In: Computer Science

Problem Statement In a restaurant, if you were pleased by the waiter's service, you may leave...

Problem Statement

In a restaurant, if you were pleased by the waiter's service, you may leave him a tip -- you pay him more than the actual value of the bill, and the waiter keeps the excess money. In some countries, not leaving a tip for the waiter is even considered impolite. During my recent holiday I was having dinner in a foreign restaurant. The pamphlet from my travel agency informed me that the proper way of tipping the waiter is the following:

  • The sum I pay must be round, i.e., divisible by 5.
  • The tip must be between 5% and 10% of the final sum I pay, inclusive.

Clearly, sometimes there may be multiple "correct" ways of settling the bill. I'd like to know exactly how many choices I have in a given situation. I could program it easily, but I was having a holiday... and so it's you who has to solve this task. You will be given:

  • an int bill -- the amount I have to pay for the dinner
  • an int cash -- the amount of money I have in my pocket

Write a function that computes how many different final sums satisfy the conditions above.

Constraints

  1. Assume that both bill and cash are in dollars.
  2. All the money I have is in one-dollar banknotes.

Examples

  1. 4  100
    
    Returns:  0
    

    4 isn't a round sum, and 5 is too much.

  2. 23  100
    
    Returns: 1
    

    The only correct choice is to pay 25 dollars, thus leaving a tip of 2 dollars.

  3. 23  24
    
    Returns: 0
    
    The same bill, but I don't have enough money to leave an appropriate tip.
  4. 220  239
    
    Returns: 1
    
    This time, it is appropriate to pay either 235 or 240 dollars. Sadly, I don't have enough money for the second possibility.
  5. 1234567  12345678
    
    Returns: 14440
    
    A large bill, but with that much money I don't care.
  6. 1880000000  1980000000
    
    Returns: 210527
    
  7. 171000000  179999999
    
    Returns: 0

Given Function

int possible_payments(int bill, int cash) {
// fill in code here
}


Answer the problem statement by completing the Given Function. Follow the constraints and examples.

Solutions

Expert Solution

The code is written in C if you want you can also compile it in c++ using the required headers. Comments are included in the code. If you require any clarifications please ask in the comments. Thank you.  

Note: Go through the code screenshot to understand the indentation in a better way.

THE CODE:

#include<stdio.h>

#include<stdlib.h>

//Function to calculate the number of ways

long int possible_payments(long int bill, long int cash) {

   //Calculate the number to remaining to get to the nearest multiple of 5

   int rem = (5-bill%5);

   bill+=rem;

   //Varibale to calaculate the tip

   long int tip = rem;

   //Varible to store the number of ways

   long int ways = 0;

   double percentage = 0.0;

   //This loops till the amount of bill gets higher than the amount of cash available

   //At each iteration inreases the tip and the bill to the next multiple of 5

   while(bill<=cash){

      //Calculates the percentage of tips in the total bill

      percentage = ((double)tip/(double)bill)*100;

      if(percentage>=5 && percentage<=10){

         ways++;

      }  

      if(percentage>10){

         return ways;

      }

      tip+=5;

      bill+=5;

   }

   return ways;

}

void main(){

   //The long int is given in order tor deal with very large numbers. You may use int of necessary.  

   long int bill;

   long int cash;

   long int numberofways = 0;

   //Takes the bill and cash as input

   printf("Enter the bill and the cash: ");

   scanf("%ld", &bill);

   scanf("%ld", &cash);

   //Calls the function to calculate the number of ways

   numberofways = possible_payments(bill,cash);

   //Display the number of ways

   printf("The number of ways are: %ld\n", numberofways);

}

CODE SCREENSHOT

OUTPUT:


Related Solutions

Instruction: For this problem, you may leave your answer as as unsimplified expressions with factorials, exponents,...
Instruction: For this problem, you may leave your answer as as unsimplified expressions with factorials, exponents, binomial coefficients, etc. However, you still need to include a brief justification for your results. (40 points - parts (a)–(h): 4 points each; part (h): 8 points In a futuristic dystopian Chicago, society is divided into five factions: Abnegation, Amity, Candor, Dauntless and Erudite. At the age of 16, each person is allowed to choose any faction as their permanent social group at the...
Use stars and bars to solve each counting problem. You may leave your answers as binomial...
Use stars and bars to solve each counting problem. You may leave your answers as binomial coefficients. (a) How many collections of 6 (not necessarily distinct) coins can be made from an infinite supply of pennies, nickels, dimes, and quarters? (b) A social security number is a sequence of 9 digits. How many social security numbers are there n1n2n3 . . . n9 such that ni ≤ ni+1 for i = 1 to 8? For example, 024455888 would count but...
Use stars and bars to solve each counting problem. You may leave your answers as binomial...
Use stars and bars to solve each counting problem. You may leave your answers as binomial coefficients. (a) How many collections of 6 (not necessarily distinct) coins can be made from an infinite supply of pennies, nickels, dimes, and quarters? (b) A social security number is a sequence of 9 digits. How many social security numbers are there n1n2n3 . . . n9 such that ni ≤ ni+1 for i = 1 to 8? For example, 024455888 would count but...
Let’s assume you were hired by Panera Bread( a restaurant with fast service and healthy options)...
Let’s assume you were hired by Panera Bread( a restaurant with fast service and healthy options) to restructure how this business operates with the following goals in mind: Increase customer service by decreasing negative comments on customer comment cards Create a physical redesign that increases efficiency for customers and employees, and As a result of the reorganization, increase store overall revenue by 7% in the next year What model(s) would you use to meet these goals? How would you use...
You have completed the Cash Flow statement. Pleased with your work so far, Barry asks you...
You have completed the Cash Flow statement. Pleased with your work so far, Barry asks you to work on an assignment for Steve Fox, relating to a different Regal subsidiary, Phrygian Equipment Capital. Phrygian has two business segments that are reported separately in its financial statements. The segments are “machinery” and “investment and Insurance.” In its management accounts, the company reports four different divisional results. The four divisions are machinery leasing, machinery sales, investments, and insurance. The results of the...
//C++// Problem Statement: Have you been to a popular restaurant recently? Many are using cell phones...
//C++// Problem Statement: Have you been to a popular restaurant recently? Many are using cell phones to notify groups when their table is ready. But, why not an App? It could let groups know their place in line and be a sales tool for the restaurant to forward coupons and other promotions to increase business. Let’s say you had a friend opening up a new restaurant business and they are looking for help with the software support. They have asked...
If you were to leave calcium metal in contact with oxygen at a partial pressure of...
If you were to leave calcium metal in contact with oxygen at a partial pressure of 1.00 atm at 298 k, would the calcium oxide form spontaneously? Use free energy to answer this.
As a consultant for as service company (such as a hospital, airport, restaurant etc.) you can...
As a consultant for as service company (such as a hospital, airport, restaurant etc.) you can analyze their service operations in multiple ways. Three of them are: 1) application of waiting line theory, 2) application of a manual Monte Carlo Simulation and 3) Application of a simulation software such as Simquest. For each of these three, describe a business situation where you would apply this methodology, and why this approach would be superior to the other two.
For the prelab assignment, you may not use the if statement nor may you use the...
For the prelab assignment, you may not use the if statement nor may you use the if/else statement. You are to write a program that calculates factorials from 1 up to and including a maximum number given by the user. Because factorials get large quickly, use long unsigned int as the type for the number entered by the user as well as the factorials you are calculating. If the user enters a 0, print an error message. Then ask if...
Suppose you were on a state supreme court and faced with a restaurant-choking case. Should you require restaurant employees
Suppose you were on a state supreme court and faced with a restaurant-choking case. Should you require restaurant employees to know and employ the Heimlich maneuver to assist a choking victim? If they do a bad job, they could cause additional injury. Should you permit them to do nothing at all? Is there a compromise position? What social policies are most important?  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT