Help! Stumped with a (math) programming problem!

Started by
5 comments, last by vibaka 22 years, 3 months ago
Ok everyone. I am COMPLETELY stumped on this problem (not a game programming problem, just a regarular ol'' programming problem). It''s actually a math problem that I can''t figure out. And it''s a simple problem, at that! But for some reason I am having the hardest time getting the correct results with my code. I even showed the math problem to my Mom and Dad and some other people and they couldn''t figure it out. So here''s the problem (from the book Object-Oriented Programming In C++, Chapter Two, Excersize Ten.) In the heyday of the British empire, Great Britain used a monetary system based on pounds, shillings, and pence. There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, and two decimal points, so that, for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Pence is the plural of penny.) The new monetary system, introduced in the 1950''s, consists of only pounds and pence, with 100 pence to a pound (like US dollars and cents). We''ll call this new system decimal pounds. Thus £5.2.8 in the old notation is £5.13 in decimal pounds (actually £5.13333333). Write a program to convert the old pounds-shillings-pence format to decimal pounds. An example of the user''s interaction would be Enter pounds: 7 Enter shillings: 17 Enter pence: 9 Decimal pounds: £7.89 So there you have it. The question that has stumped me like a... well, a stump! I don''t exactly want somebody to code up the problem, just give me a push in the right direction. What math am I missing? I don''t get it... And on top of that this is only from chapter two so you can''t use any type of loops or decisions in your code. The only topics that have been covered so far are c++ program structure, variables, input/output with cout and cin, arithmetic operators, assignment and increment operators, and type casts. Please help... Thanks in advance, -vibaka-
Advertisement
Just use the given conversions to convert the input into pence and then divide by the amount of pence to a pound to get it in decimal pounds.
So you take (pounds*20*12 + shillings*12 + pence)/240.
That will be the amount in decimal pounds.

Just edited, because you are supposed to divide by 240 like it says now, not 100 like it said before, as the conversion is done so 1 pound in the old system = 1 pound in the new system, and not so that 1 pence in the old system = 1 pence in the new system..

Edited by - ziphnor on January 16, 2002 8:23:22 AM
If you claim your user to enter only ''really'' valid values, i.e. no more than 20 shillings / 12 pence, you can just leave the number of pounds the user entered.
Given the fact that 12 pence make a shilling and 20 shillings make a pound, you need 12*20 pence to make a pound.
So all you need now is to convert the shillings to pence and then convert then pence from range[0,240] to range[0,100], what should be an easy task.
I hope that helps.
Ziphnor''s way is the better one, it correctly converts every entered value. I just wanted to keep the "push in the right direction" a bit smaller ;-)
ziphnor-

yay! i feel dumb now but you''re the man(tm).

doener-

thanks for the "push".

once again, thanks for all the help. you guys are grand!

ciao,

-vibaka-
The problem is actually relatively simple. All you do is divide the pence by 12 and add that to the shillings. Ex. 9/12=0.75 0.75+17=17.75 Then all you do is divide the shillings by 20. Ex. 17.75/20=0.8875 You round this to the second decimal place, and then you add it to the pounds. Ex. 0.89+7=7.89
In this example, the value of the pound is constant. It is the value of the pennies that change. So all you really need to do is work out how many new pennies the shillings and pence add up to, and add that to the number of pounds.

Spoiler code below - highlight to read it.


float GetDecimalCurrency(int pounds, int shillings, int pence){  int decimalPennies = 0;  float decimalPounds;  decimalPennies = pence + (12 * shillings) + (240 * pounds);    decimalPounds  = (float)decimalPennies/240.0f;   return decimalPounds;}  




Edited by - Sandman on January 16, 2002 8:31:31 AM

This topic is closed to new replies.

Advertisement