help me... making change for \$1.00 - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# help me... making change for \$1.00

Posts: 3Member
Ok, I dont want anyone to do the whole program for my, but I would really like some help. This is a school project, the teacher and the book are no help at all.
Instructions:
Please find the number of ways that you can provide change for one dollar, using only pennies nickles dimes and quarters. Print the number of coins of each denomination as well as the total number of ways you can make change.
Please help, I am lost and I would like some guidance... thanks

## Comments

• Posts: 6,349Member
: Ok, I dont want anyone to do the whole program for my, but I would really like some help. This is a school project, the teacher and the book are no help at all.
: Instructions:
: Please find the number of ways that you can provide change for one dollar, using only pennies nickles dimes and quarters. Print the number of coins of each denomination as well as the total number of ways you can make change.
: Please help, I am lost and I would like some guidance... thanks
:
I don't know the precise value of all the coins you mentioned, so I will describe it without coin names. The procedure itself is quite simple. Here is a part of the code:
[code]
procedure RemoveCoins(money:real);
var
tempmoney:real;
i,count:integer;
begin
tempmoney:=money;
count:=0;
while tempmoney-coinsize>0 do begin { Find the maximum number of coins in the money }
tempmoney:=tempmoney-coinsize;
inc(count);
end;
if Count>0 then
for i:=count downto 0 do begin
tempmoney:=money-coinsize*count; { remove the coins to get the remainder }
RemoveCoins(tempmoney); { go on to the next coin size }
end;
end;
[/code]

This code removes the coins, until the \$1.00 is completely gone. But it does more. It also iterates through all the combinations of the initial money value.
CoinSize holds the size of the coin, which the program is currently subtracting. I suggest, that you make this an array with all the sizes you need, sorted by size from large to small.
You then need to rewrite the procedure in which you use the correct coinsize.
You also need to add some kind of counting mechanism for the number of combinations.

Good Luck.

• Posts: 3Member
Well strange enough, that night after posting the message, I was going to sleep and bam it hit me. I had an idea in my head for how to do it and it worked really well, super easy to do. Here is the code if anyone else needs to do this program. You might want to take the Lst off of the writeln's at first so it doesn't print right away... when you get it to look like what you want then stick it back in and you're good to go:

PROGRAM change;
USES Printer;

VAR
q, d, n, p: (* coins *)
integer;
cnt: (* counter *)
integer;

BEGIN
cnt:=0;
FOR q:=4 DOWNTO 0 DO
FOR d:=10 DOWNTO 0 DO
FOR n:=20 DOWNTO 0 DO
FOR p:=100 DOWNTO 0 DO
BEGIN
IF (q*25)+(d*10)+(n*5)+(p)=100 THEN
BEGIN
writeln(Lst,'There are ',q,' quarters, ',d,' dimes, ',n,' nickels, and ',p,' pennies in \$1.00');
cnt:=cnt+1;
END;
END;
writeln(Lst,'');
writeln(Lst,'There are ',cnt,' ways to make change for \$1.00');
END.

• Posts: 354Member
: Well strange enough, that night after posting the message, I was going to sleep and bam it hit me. I had an idea in my head for how to do it and it worked really well, super easy to do. Here is the code if anyone else needs to do this program. You might want to take the Lst off of the writeln's at first so it doesn't print right away... when you get it to look like what you want then stick it back in and you're good to go:
:
: PROGRAM change;
: USES Printer;
:
: VAR
: q, d, n, p: (* coins *)
: integer;
: cnt: (* counter *)
: integer;
:
: BEGIN
: cnt:=0;
: FOR q:=4 DOWNTO 0 DO
: FOR d:=10 DOWNTO 0 DO
: FOR n:=20 DOWNTO 0 DO
: FOR p:=100 DOWNTO 0 DO
: BEGIN
: IF (q*25)+(d*10)+(n*5)+(p)=100 THEN
: BEGIN
: writeln(Lst,'There are ',q,' quarters, ',d,' dimes, ',n,' nickels, and ',p,' pennies in \$1.00');
: cnt:=cnt+1;
: END;
: END;
: writeln(Lst,'');
: writeln(Lst,'There are ',cnt,' ways to make change for \$1.00');
: END.
:
:
Pretty slick! Here it is in c.
[code]
/* CHANGE.C A program to determine the different ways to make a dollar
with pennies, nickels, dimes and quarters. */

#include

int main() {
int p, n, d, q;
int cntr = 0;

for (p = 100; p >= 0; p--)
for (n = 20; n >= 0; n--)
for (d = 10; d >= 0; d--)
for (q = 4; q >=0; q--)
if ((p + (n * 5) + (d * 10) + (q * 25)) == 100) {
if (p != 0) printf("%d pennies ", p);
if (n != 0) printf("%d nickels ", n);
if (d != 0) printf("%d dimes ", d);
if (q != 0) printf("%d quarters ", q);
printf("
");
cntr++;
}
printf("There are %d ways to change a dollar.
", cntr);
return 0;
}

/* END OF PROGRAM */
[/code]
Your pascal code compiled just fine with QuickPascal, but I wasted 4 sheets of paper when I ran it. Oh well! I should have taken the lst out.

The C code compiled with turbo C, but sends output to STDOUT. If you want to print it's results you can change the printf's to fprintf(stdprn, ...) or you can redirect it.

Keep on!

Sign In or Register to comment.