|
-
October 1st, 2009, 07:02 PM
#1
inserting a comma on the display
I was given a good question and I can't figure it out. It says:
Write a program that reads in an integer that is greater than 1,000 and less than 1,000,000. The number must be entered without a comma. Display the number with a comma inserted where it belongs. (Hint: use the % operator)
How can this be solved?
-
October 1st, 2009, 11:38 PM
#2
Re: inserting a comma on the display
first two questions to answer for yourself.
1. How does base 10 representations of numbers work?
2. What arithmetical operation does % represent?
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
October 2nd, 2009, 08:08 AM
#3
Re: inserting a comma on the display
base 10 numbers?
10^3=1000
1,000,000/1000=1000
base 10 number- decimal system
% represents the remainder... 9%2=1, 7%100=7, 30%3=0
-
October 2nd, 2009, 08:32 AM
#4
Re: inserting a comma on the display
If the user input is greater than 1,000 and less than 1,000,000, the output will be something like xxx,yyy i.e. a part before the comma and a part after the comma.
Think how to split up the original number into the part before and after the number.
Learn how to format the part after the comma to have exactly 3 digits on output (e.g. 007).
Output the three components (first number, comma character, second number).
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
October 2nd, 2009, 08:56 AM
#5
Re: inserting a comma on the display
If the number is 22202
22202%1000=202 (looking good so far for the part after the comma)
integer math, 22202/1000 gives 22 and that would be the part before the comma
that breaks it up perfectly, but then if the input number is 30000
the part after the comma is 30000%1000=0 and then I can't really make it work. right track I'm not sure
-
October 2nd, 2009, 09:12 AM
#6
Re: inserting a comma on the display
integer math, 22202/1000 gives 22 and that would be the part before the comma
The comma is always placed before the three rightmost digits. There is no exception to that rule if the number will be between 1,000 and 1,000,000 (non-inclusive).
You didn't mention the rules. Are you allowed to use strings to represent the number (i.e. convert the number to a string and work with the string)?
Regards,
Paul McKenzie
-
October 2nd, 2009, 09:23 AM
#7
Re: inserting a comma on the display
There are no rules except using the % operator.
-
October 2nd, 2009, 09:32 AM
#8
Re: inserting a comma on the display
 Originally Posted by Brian_Jones
There are no rules except using the % operator.
That will get you the digits preceding the comma, but to get the digits after the comma is just a matter of stringizing the number and getting the 3 rightmost characters.
You'll need to use some sort of string manipulation to get the last three digits, since an integer 0 (in the case of 30000%1000), is 0. It isn't "000". So either you have to turn the number into a string, or on ouput, format the number to have leading zeros (up to 3 digits). I think this is where your stumbling block is, converting a true integer into a string representation of it, with whatever formatting you need to do. So it isn't the math that's the problem.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 2nd, 2009 at 09:35 AM.
-
October 2nd, 2009, 09:41 AM
#9
Re: inserting a comma on the display
 Originally Posted by Brian_Jones
There are no rules except using the % operator.
here is one option:
Look into manipulators:
http://www.cplusplus.com/reference/i.../manipulators/
read the functions setfill and setw. The examples provided should be enough.
Or do as Paul said: Transform your number into a string, and hack away! Since your number is a string, you know you want to insert the character ',' at position string.end()-3.
hey look! http://www.cplusplus.com/reference/s...string/insert/
-
October 2nd, 2009, 10:05 AM
#10
Re: inserting a comma on the display
well the last two posts might make it work too, but I have to take a step back to posts #2 and #4 because they deal with the % operator.
-
October 2nd, 2009, 10:08 AM
#11
Re: inserting a comma on the display
The setfill and setw manipulators are exactly what you need here.
-
October 2nd, 2009, 10:23 AM
#12
Re: inserting a comma on the display
 Originally Posted by Brian_Jones
well the last two posts might make it work too, but I have to take a step back to posts #2 and #4 because they deal with the % operator.
Yes, use the mod operator to get the integer version of the last three digits. You have that covered already.
Then what monarch_dodra and Lindley are saying is to take that integer and format it correctly using manipulators on output. It's this step that is the final step.
Regards,
Paul McKenzie
-
October 2nd, 2009, 10:24 AM
#13
Re: inserting a comma on the display
this problem has two parts.
1. Use integer arithmetic operations to break a number into two numbers.
One representing the part less than 1000 and one representing the part greater than 1000.
2. Output the result of part 1 to represent the original number with a comma inserted.
For part one given, a number 1000000 > X > 1000 what is the result of integer division by 1000 and the remainder operator by 1000.
For part two io manipulators.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
October 2nd, 2009, 01:03 PM
#14
Re: inserting a comma on the display
Thanks all for your advice, it worked!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|