Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 25th, 2005, 1:24 PM   #1
Polaris
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 Polaris is on a distinguished road
Unhappy Stuck with a C problem

Hello every one,

I am stuck with the following problem, it's the course final assignment of my 'structured programming' course at the univerisity. I'm student of EEE, and therefor I'm not much into programming... I know it's cheating, but I would really be thankful if some can kindly solve the problem for me, I really don't want to get a double zero :o

Here's the problem, and it should be solved with C (no C++)
:

Write a program to dispense change.The user enters the amount paid and the amount due. The program determines how many dollars, 50cents, 20cents, 10cents should be given as change.

a) write a function with the heading:
void dispense(int change,int *dollars,int *c10,int *c20,int *c50) that determines and return the quantity of each kind of coin.
(Note:16 cents-5 cents=11 cents meaning that one 10cents is returned
for charge.However 16 cents-1 cents=15 cents meaning that one 20-cents
is required(round-up))

b) write a function int getData(int *paid,int *due) that doe the following:

inform the user that amount paid and amount due should be entered in
cents(integer)
do
prompt the user to enter the amount paid and amount due
read in the data
while (amount due<0 or amount paid<amount due);
if both amount paid and amount due are zeros
return 0
otherwise
return 1

following until both the amount paid and amount due are zeros.

1.call the function getData to ask for the amount paid and amount due
2.print out the charge and the # of each kind of coins to dispense.

Program output:

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:16 5
amount due:5,amount paid:16,and thus change=11
You are suggested to give him/her 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:26 5
amount due:5,amount paid:26,and thus change=21
You are suggested to give him/her 1 20-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:66 5
amount due:5,amount paid:66,and thus change=61
You are suggested to give him/her 1 50-cents coin(s) 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:81 5
amount due:5,amount paid:81,and thus change=76
You are suggested to give him/her 1 50-cents coin(s) 1 20-cents
coin(s) 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:5 81
Enter the amount paid and amount due please:0 0
Polaris is offline   Reply With Quote
Old Feb 25th, 2005, 1:31 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Shit, i actually had this program already done... let me fumble around with some floppies and see if i can find it.
__________________

tempest is offline   Reply With Quote
Old Feb 25th, 2005, 1:41 PM   #3
Polaris
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 Polaris is on a distinguished road
Unhappy

Oops, I posted on the wrong forum! Sorry guys! Actually I'm too tensed about the grade... I've got to study for three other subjects! I hate school!

tempest, it would be great if you can help... my address is rimina1985@yahoo.com. Thanks in advanced.
Polaris is offline   Reply With Quote
Old Feb 25th, 2005, 2:46 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Post it here too - I'm curious.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 25th, 2005, 3:50 PM   #5
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
~moved
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 26th, 2005, 6:30 AM   #6
Polaris
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 Polaris is on a distinguished road
Cool

Here's what I wrote. Not to mention, this doesn't work :(

#include <stdio.h>

int getData(int *paid,int *due){
printf ("\nThe amount-paid and amount-due should be entered in cents(integer)");
do {
printf ("\nTo terminate the program,enter 0 for both values");
printf ("\nEnter the amount-paid and amount-due please:");
scanf ("%d %d",&paid,&due); } while (due<0||paid<due);
if (paid==0&&due==0)
return 0;
else return 1;}

void dispense(int change,int *dollars,int *c10,int *c20,int *c50){
dollars=round(paid-due);
change=dollars/10;
while (change>=1&&change<=9){
switch (change) {
case '1' :c10+=1;
break;

case '2' :c20+=1;
break;

case '3' :c10+=1;
c20+=1;
break;

case '4' :c20+=2;
break;

case '5' :c50+=1;
break;

case '6' :c10+=1;
c50+=1;
break;

case '7' :c20+=1;
c50+=1;
break;

case '8' :c10+=1;
c20+=1;
c50+=1;
break;

case '9' :c20+=2;
c50+=1;
break;

default:printf ("\nCheck your input!");
printf ("\nEnter the amount-paid and amount-due please:");
}
}


int main ()

{
do {
getData();} while getData==1;

dispense();
printf ("\namount due:%d",due);
printf ("amount paid:%d",paid);
printf ("and thus change=%d",change);

if (change<5) {
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s)",c10);}
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s)",c20);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coiu(s)",c10,c20);}

else if (change>5) {
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s) %d 50-cents coin(s)",c20,c50);}
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 50-cents coin(s)",c10,c50);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s) %d 50-cents coin(s)",c10,c20,c50);}

else if (change==5) {
printf ("You are suggested to give him/her %d 50-cents coin(s)",c50);}


}
}

Please help me!
Polaris is offline   Reply With Quote
Old Aug 19th, 2006, 1:53 PM   #7
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
here is what i got, but the source is in c++ (sorry )

//exact change
#include <iostream>
using namespace std;

void GetExactChange (int total)
{
     int quarters, dimes, nickels, pennies, leftOver;
     
     //get amount of quarters
     quarters = total/25;
     leftOver = total%25;
     
     //get amount of dimes
     dimes = leftOver/10;
     leftOver = leftOver%10;
     
     //get amount of nickels
     nickels = leftOver/5;
     leftOver = leftOver%5;;
     
     //get amount of pennies
     pennies = leftOver;
     
     cout<<endl<<"From "<<total<<" cents you get: ";
     
     cout<<endl<<quarters<<" quarters";
     cout<<endl<<dimes<<" dimes";
     cout<<endl<<nickels<<" nickels";
     cout<<endl<<pennies<<" pennies"<<endl;
}

int main ()
{
    int cents;
    char rep[1];
    cout<<endl<<"Enter amount of pennies to convert: ";
    cin>>cents;
    GetExactChange(cents);
    cout<<endl<<"Repeat?[Y/N]";
    cin>>rep[1];
    if(rep[1]=='y') main ();
    else return 0;
}

sorry that it is not exactly how you wanted your program to be, but i will work on it so that it will be =)
RemoteC2 is offline   Reply With Quote
Old Aug 19th, 2006, 1:54 PM   #8
RemoteC2
Programmer
 
RemoteC2's Avatar
 
Join Date: Jan 2006
Posts: 55
Rep Power: 3 RemoteC2 is on a distinguished road
lol it only took 540 days to get an answer lol
RemoteC2 is offline   Reply With Quote
Old Aug 19th, 2006, 3:30 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
540 days to break the forum's rules regarding homework? Wonderful. You must be very proud. Since it's fucked up, you must be doubly proud. Since it appears many times on every C/C++ forum that ever existed, you must be triply proud. Now, if you'll excuse me, I need to go sniff my ass and puke in my shirt pocket.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC