Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Modify the code so that is includes the following function:

void pay_amount (int dollars, int *twenties, int *tens, int *fives, int *ones);

The function determines the smallest number of $20, $10, $5, and $1 bills necessary to pay the amount represented by the dollars parameter. The twenties parameter points to a variable in which the function will store the number of $20 bills required. The tens fives and ones parameters are similar.


I'm quite confused on how to add this, I tried for awhile but I was tired and went to sleep.

What I have tried:

C
int main(void)
{
  int amount, twenties, tens, fives, ones, reduced_amount;

  printf("Enter a dollar amount: ");
  scanf("%d", &amount);

  twenties = amount / 20;
  reduced_amount = amount - (20 * twenties);

  tens = reduced_amount / 10;
  reduced_amount = reduced_amount - (10 * tens);

  fives = reduced_amount / 5;
  ones = reduced_amount - (5 * fives);

  printf("\n");   /* blank line */

  printf("$20 bills: %d\n", twenties);
  printf("$10 bills: %d\n", tens);
  printf(" $5 bills: %d\n", fives);
  printf(" $1 bills: %d\n", ones);

  return 0;
}
Posted
Updated 16-Mar-18 0:25am
v2

Read the instructions your tutor gave you: they are pretty clear.
It instructs you to add a function that takes a specific set of parameters and to set the values as needed. So start by adding a function, and then work out what goe sin it.
It's simple enough to do: take the amount given, divide it by 20. That's the number of twenties you need.
Remove the value in twenties from the value.
Repeat for tens.
Then fives.
What's left is ones.


This is your homework, so I'll give you no code, but I'll give you a hint: Look at the modulus operator.
 
Share this answer
 
v2
Comments
Member 13729630 16-Mar-18 19:12pm    
I understand how to do that, the problem is i'm getting this error.

https://gyazo.com/32d21feeaa99b186b71c1d9f36e72470

void pay_amount (int dollars, int *twenties, int *tens, int *fives, int *ones);


int main(void)
{
int amount, twenties, tens, fives, ones, reduced_amount;

pay_amount( amount, &twenties, &tens, &fives, &ones);


printf("\n"); /* blank line */

printf("$20 bills: %d\n", twenties);
printf("$10 bills: %d\n", tens);
printf(" $5 bills: %d\n", fives);
printf(" $1 bills: %d\n", ones);

return 0;
}


void pay_amount (int dollars, int *twenties, int *tens, int *fives, int *ones)
{
int reduced_amount;

printf("Enter a dollar amount: ");
scanf("%d", &dollars);

*twenties = dollars / 20;
reduced_amount = dollars - (20 * twenties);

*tens = reduced_amount / 10;
reduced_amount = reduced_amount - (10 * tens);

*fives = reduced_amount / 5;

*ones = reduced_amount - (5 * fives);

}
OriginalGriff 17-Mar-18 3:15am    
Look at your code:
*twenties = dollars / 20;
Using a pointer, "*" dereferences it, so value goes in the right place.

reduced_amount = dollars - (20 * twenties);
Multiplying a pointer by 20 ... what does that produce? Much the same things as "multiplying you phone number by 20".

BTW: You don't need "reduced_amount" - just overwrite "dollars".
You (or you teacher) correctly implemented the algorithm. Now you just need to know how out parameters are technically implemented in C.
In the following code I show you a simple example:
C
#include <stdio.h>
void square(int x, int * px2) // px2 implements an OUT parameter: the function set the of the pointed variable
{
  *px2 = x*x;
}

int main()
{
  int x;
  int x2;
  printf("enter a number: ");
  if ( scanf("%d", &x) == 1)
  {
    square( x, &x2);
    printf("the square of %d is %d\n", x, x2);
  }
  return 0;
}
 
Share this answer
 
You need some basic coding skills. So search some tutorial.

Your task is quit easy. Write this declaration outside the main function
C++
void pay_amount (int dollars, int *twenties, int *tens, int int *fives, int *ones);
And than you need to implement it
C++
void pay_amount (int dollars, int *twenties, int *tens, int *fives, int *ones)
{
  //your code
}
How to call this function is shown in solution 2.
 
Share this answer
 
v2
Comments
Richard MacCutchan 16-Mar-18 7:37am    
You seem to have stopped typing early on the prototype.
KarstenK 16-Mar-18 11:49am    
Friday afernoon rush for the weekend ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900