|
 |
|
 |
02-04-2007, 11:41 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
need help with class assignment
the question they gave us was:
Your friend Joe saves pennies in a jar, which he empties every month when he goes tothe bank. You are to create a problem that allows him to enter the number of pennies, and then calculates and displays the number of dollars, quarters, dimes, nickels, and pennies he will receive when he trades in the pennies at the bank.
I need to code this and confused on how to can anyone help?
|
|
|
02-04-2007, 11:52 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
First of, try and see how many dolars it fits into, say he has 881 pennies, that would be 8 dolars, then theres 81 pennies left. take the highest number of quaters it would be, which is 3, that'll leave you with 11 pennies, then take the highest number of dimes that'll provide, which is zero, this will leave 11 pennies, then take the highest number of nickels that'll provide, which is two, which will leave you with 1 pennie...
So the outcome to that will be:
Dollars: 8
Quarters: 3
Dimes: 0
Nickels: 2
Pennies: 1
Anyway, you will allways use the greed paradigm..
Last edited by redhead; 02-04-2007 at 12:30 PM.
|
|
|
02-04-2007, 12:18 PM
|
#3 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
thanks for response but...
I need a write a programming code for that question and then desk check my answer.
|
|
|
02-04-2007, 01:36 PM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Here is one in ML
Code:
val values = [(100, "Dollars"), (25, "Quarters"),
(10, "Dimes"), (5, "Nickels"), (1, "Pennies")];
fun head([]) = []
| head((x,y)::xs) = x :: head(xs);
fun tail([]) = []
| tail((x,y)::xs) = y :: tail(xs);
fun combine([],[]) = []
| combine(x, []) = []
| combine([], y) = []
| combine(x::xs, y::ys) = (x,y) :: combine(xs,ys);
fun count([], value, result) = result
| count(x::xs, value, result) =
if value = x then
count(xs, value, result +1)
else
count(xs, value, result);
fun count_all([], []) = []
| count_all([], y) = []
| count_all(x, []) = []
| count_all(x, y::ys) = count(x,y,0) :: count_all(x, ys);
fun change (coinvals, 0) = []
| change ([], amount) = []
| change (c::coinvals, amount) =
if amount<c then change(coinvals, amount)
else c :: change(c::coinvals, amount-c);
fun make_change(value) =
combine(
count_all(
change(
head(values), value
), head(values)
), tail(values)
);
Quote:
|
I need a write a programming code for that question
|
I'm not here to do your homework, show us how far you've gotten, then we might help...
If you know functional programming, then my ML version will provide great help.. If not, you're not any further than you were to begin with..
|
|
|
02-04-2007, 02:04 PM
|
#5 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
whats ML
I need it in C++ coding I'm just a beginner so any help would be appreciated
|
|
|
02-04-2007, 02:11 PM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Then I would say the structure would be something like: - if amount bigger than 100, then dollars equals amount/100 and change amount to amount%100
- if amount bigger than 25, then quarters equals amount/25 and change amount to amount%25
- and so forth...
//edit
ML is a functional programming language the Moscow ML version was developed by an old professor of mine. So I started out learning LISP and ML as the first two programming languages.
By the way, welcome here at Code Newbie 
Last edited by redhead; 02-04-2007 at 03:12 PM.
|
|
|
02-04-2007, 04:18 PM
|
#7 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 704
|
% would work
881 % 100 leaves 81
or
881 / 100 = 8
__________________

UT: Ultra-kill... God like!
|
|
|
02-04-2007, 10:18 PM
|
#8 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
how do you code this assignment in C++
thanks for the message but the question wants us to complete a IPO chart and use the chart to code the program. This is an introduction course to programming with c++ I have already completed the IPO chart I just need some asssistance with the coding of the program to complete this exercise.
Thanks
|
|
|
02-04-2007, 10:55 PM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Ok, you convinced me.. .I'll do the start part, you can then complete it...
Code:
#include <iostream>
int main()
{
int amount=-1;
while(amount < 0){
std::cout << "Input amount of pennies: " ;
std::cout.flush();
std::cin >> amount;
if(amount < 0)
std::cout << "It's impossible to have negative amount" << std::endl;
}
if(amount >= 100){
std::cout << "Dollars:\t" << amount/100 << std::endl;
amount = amount %100;
}
if(amount >= 25){
/* now you continue */
|
|
|
02-05-2007, 10:06 PM
|
#10 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
I figured out the coding on this one but I can't figure out the calculatons on this exercise can some one help I'm a beginer with this c++ coding stuff so help would be greatly appreciated.
|
|
|
02-05-2007, 11:28 PM
|
#11 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Alright, here is the missing code to this one:
Code:
std::cout << "Quarters:\t" << amount/25 << std::endl;
amount = amount %25;
}
if(amount >= 10){
std::cout << "Dimes:\t\t" << amount/10 << std::endl;
amount = amount %10;
}
if(amount >= 5){
std::cout << "Nickels:\t" << amount/5 << std::endl;
amount = amount %5;
}
if(amount >= 1)
std::cout << "Pennies:\t" << amount << std::endl;
return 0;
}
|
|
|
02-06-2007, 02:46 AM
|
#12 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 704
|
calculatons have nothing to do with c++, learn your mathematics 
__________________

UT: Ultra-kill... God like!
|
|
|
02-06-2007, 11:03 AM
|
#13 (permalink)
|
|
Recruit
Join Date: Feb 2007
Posts: 18
|
but how do you code the calculations
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:27 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|