Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:52:02+00:00 2026-05-27T08:52:02+00:00

I am writing a function to calculate change given – $1, $5, $10, $20,

  • 0

I am writing a function to calculate change given – $1, $5, $10, $20, $50, and $100 are the types of bills I have available to me. Each denomination is also subtracted from a finite number of bills that are in the drawer at the current time.

There are also no pennies, nickels, dimes, or quarters to deal with here, just whole dollar amounts.

This is what I came up with:

UPDATE

The function now has a correction for error handling when there are no more bills in the drawer for any denomination, the user is supplied with a message to obtain more money for the drawer.

// Set elsewhere in the program
int numberOnesLeft;
int numberFivesLeft;
int numberTensLeft;
int numberTwentiesLeft;
int numberFiftiesLeft;
int numberHundredsLeft;

int numberOfOnes = 0;
int numberOfFives = 0;
int numberOfTens = 0;
int numberOfTwenties = 0;
int numberOfFifties = 0;
int numberOfHundreds = 0;

void CalculateChange(float amount)
{
    char tempStr[128];

    while(amount >= 100.0)
    {
        if(numberOfHundreds >= numberHundredsLeft)
            break;
        else
            amount = amount - 100.0;
        numberOfHundreds++;
    }
    while(amount >= 50.0)
    {
        if(numberOfFifties >= numberFiftiesLeft)
            break;
        else
            amount = amount - 50.0;
        numberOfFifties++;
    }
    while(amount >= 20.0)
    {
        if(numberOfTwenties >= numberTwentiesLeft)
            break;
        else
            amount = amount - 20.0;
        numberOfTwenties++;
    }
    while(amount >= 10.0)
    {
        if(numberOfTens >= numberTensLeft)
            break;
        else
            amount = amount - 10.0;
        numberOfTens++;
    }
    while(amount >= 5.0)
    {
        if(numberOfFives >= numberFivesLeft)
            break;
        else
            amount = amount - 5.0;
        numberOfFives++;
    }
    while(amount >= 1.0)
    {
        if(numberOfOnes >= numberOnesLeft)
            break;
        else
            amount = amount - 1.0;
        numberOfOnes++;
    }
    if(amount > 0)
    {
      printf("You are still owed: $");
      sprintf(tempStr, "%.2f", amount);
      printf(tempStr);
      printf("\n\n");
      printf("Please obtain more money for the drawer\n");
    }
}

From the valued correction of arasmussen, the Big O of this is O(x*n) = x * O(n) = O(n), where x is the number of denominations there are.

Is there a algorithmically faster way for computing each denomination?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:52:03+00:00Added an answer on May 27, 2026 at 8:52 am

    Less loops?

    With infinite bills, should be simple enough.

    int num_100 = amount / 100; amount %= 100;
    int num_50  = amount / 50;  amount %= 50;
    int num_20  = amount / 20;  amount %= 20;
    int num_10  = amount / 10;  amount %= 10;
    int num_5   = amount / 5;   amount %= 5;
    int num_1   = amount;
    

    With finite numbers of bills…

    void get_change(int &amount, int &left, int denom) {
      int num = amount / denom;
      if (left < num)
        num = left;
      left -= num;
      amount -= num * denom;
    }
    
    int amount = ...;
    get_change(amount, num_100 , 100);
    get_change(amount, num_50  , 50);
    get_change(amount, num_20  , 20);
    get_change(amount, num_10  , 10);
    get_change(amount, num_5   , 5);
    num_1 = amount;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a function short getBits(short data, int p, int n) I have
I have a problem with writing a Cartesian power function. I found many examples
I'm writing a C code for improving recursive functions learning. My function must calculate
Looking to learn function writing. I have data laid out in the following (e.g.):
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I'm writing a function that fishes out the src from the first image tag
I'm writing a function for an installer DLL to verify the Authenticode signature of
I am writing a function in Haskell that deals with numbers beyond the length
I'm writing a function that replaces long hex coded color ( #334455 ) with
Short of writing a function manually that translates a few known REFIID to names,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.