P: n/a

I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).
eg: If user enters 45.99420 then we should get 5, which are the number
of digits after the decimal point.
 Kuljit  
Share this Question
P: n/a

Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit
Well...
I'll tell you the algorithm for doing it, you do the code :)
1. Allocate a buffer to hold the input as a string(eg char input[80])
2. Use scanf(hint: scanf("%s", input))
3. Start walking the string character by character(assumes ASCII)
4. When you encounter the char x = '.' start increment a counter
5. Finish incrementing when you've reached '\0'
Hope you can write the code yourself.  
P: n/a

"Kuljit" writes: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
Read the input into a string. getline() would probably be a good choice.
Search for the decimal point and then count digits until you get to the end
of the number. Note that you can look at a character in a string, s, with
s[i], much like an array.  
P: n/a

"osmium" writes:
<snip>
Never mind. Wrong news froup.  
P: n/a

gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit
Well... I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes ASCII) 4. When you encounter the char x = '.' start increment a counter 5. Finish incrementing when you've reached '\0'
Hope you can write the code yourself.
While doing this, make sure that what the user inputs is an actual
valid number, i.e.:
input like +a1b2.53.678.984 is not valid.

Ioan  Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)  
P: n/a

Nelu wrote: gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit
Well... I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes ASCII) 4. When you encounter the char x = '.' start increment a counter 5. Finish incrementing when you've reached '\0'
Hope you can write the code yourself.
While doing this, make sure that what the user inputs is an actual valid number, i.e.: input like +a1b2.53.678.984 is not valid.
Even better. You can use only getchar. No need to allocate memory for a
string.
You just have to know how to parse a number: [sign] [integer part] .
[fractional part]
What's between [ and ] is optional.

Ioan  Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)  
P: n/a

BEGIN PGP SIGNED MESSAGE
Hash: SHA1
Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
I think that you are going to have to redefine the problem before you
can get your solution.
Using your example above, how many digits to the right of the decimal
point does
45.9942
and
45.994200
have?
If the count is /not/ 5 (and I suspect that it won't be), then you are
/not/ counting decimal places in a floating point number. You are,
instead, counting digit characters that follow a '.' in a string.
(FWIW, you'd have other problems with floatingpoint numbers; often the
numerical value of a floatingpoint number does not match it's
floatingpoint value, so 45.99420 might actually count 10 decimal places
because it is actually stored as 45.9941789301 or something.)
HTH
 
Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
BEGIN PGP SIGNATURE
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla  http://enigmail.mozdev.org
iD8DBQFD825MagVFX4UWr64RAqypAJ9Ly9NJPaw4IA6cyVJV+v uyk5eZxACeMQf0
qnivmuy3/BTPu8WsLeK0+9o=
=FNJR
END PGP SIGNATURE  
P: n/a

Nelu schrieb: Nelu wrote:
gamehack wrote:
Kuljit wrote:
I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit
Well... I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input)) 3. Start walking the string character by character(assumes ASCII) 4. When you encounter the char x = '.' start increment a counter 5. Finish incrementing when you've reached '\0'
Hope you can write the code yourself.
While doing this, make sure that what the user inputs is an actual valid number, i.e.: input like +a1b2.53.678.984 is not valid.
Even better. You can use only getchar. No need to allocate memory for a string. You just have to know how to parse a number: [sign] [integer part] . [fractional part] What's between [ and ] is optional.
Not entirely.
"." does not count...
Cheers
Michael

EMail: Mine is an /at/ gmx /dot/ de address.  
P: n/a

Michael Mair wrote: Nelu schrieb: Even better. You can use only getchar. No need to allocate memory for a string. You just have to know how to parse a number: [sign] [integer part] . [fractional part] What's between [ and ] is optional.
Not entirely. "." does not count...
Right. '.' doesn't stand alone as a number. My mistake.

Ioan  Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)  
P: n/a

Lew Pitcher wrote: BEGIN PGP SIGNED MESSAGE Hash: SHA1
Kuljit wrote:
I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
I think that you are going to have to redefine the problem before you can get your solution.
Using your example above, how many digits to the right of the decimal point does 45.9942 and 45.994200 have?
If the count is /not/ 5 (and I suspect that it won't be), then you are /not/ counting decimal places in a floating point number. You are, instead, counting digit characters that follow a '.' in a string.
(FWIW, you'd have other problems with floatingpoint numbers; often the numerical value of a floatingpoint number does not match it's floatingpoint value, so 45.99420 might actually count 10 decimal places because it is actually stored as 45.9941789301 or something.)
The original question was counting digits after the decimal point
entered by the user. Any input by the user is text. The program can
simply capture the text appropriately, and count the digits the user
typed. This is not a floating point problem (as originally stated)!
If you must convert the input text to a floating point value and then
determine from that what the user typed, I think you are lost.

Joe Wright
"Everything should be made as simple as possible, but not simpler."
 Albert Einstein   
P: n/a

gamehack wrote: Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit Well... I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input))
Bigger hint. Don't use scanf("%s", input) ever. It allows the user to
scribble all over your memory by typing in more characters than you have
allowed for.
3. Start walking the string character by character(assumes ASCII)
No need to assume ASCII. C provides perfectly good methods for
identifying digits, '+', '' and '.'
4. When you encounter the char x = '.' start increment a counter 5. Finish incrementing when you've reached '\0'
You forgot to check for invalid input.
Hope you can write the code yourself.
Yes, the OP does have to attempt to write the code. Having done so
people here can assist with any C questions.

Flash Gordon
Living in interesting times.
Web site  http://home.flashgordon.me.uk/
comp.lang.c posting guidlines and intro  http://clcwiki.net/wiki/Intro_to_clc  
P: n/a

gamehack wrote: Kuljit wrote: Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case). I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input))
I must have missed the part where the OP wanted to cause
a buffer overflow. (Never use scanf with %s).
3. Start walking the string character by character(assumes ASCII)
You don't have to assume ASCII to walk a string character
by character.  
P: n/a

On 15 Feb 2006 10:03:18 0800, "gamehack" <ga******@gmail.com> wrote: Kuljit wrote: I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
 Kuljit Well... I'll tell you the algorithm for doing it, you do the code :) 1. Allocate a buffer to hold the input as a string(eg char input[80]) 2. Use scanf(hint: scanf("%s", input))
fgets() is a much better choice than using scanf() with an
unrestricted %s.
3. Start walking the string character by character(assumes ASCII) 4. When you encounter the char x = '.' start increment a counter 5. Finish incrementing when you've reached '\0'
Hope you can write the code yourself.
Remove del for email  
P: n/a

Hello everyone,
Thankyou for your help. i have written the code as you have said to
accept it as a string and got the output, but as many of you have said
this is not a floating point problem.
So can we ask the user to enter the decimal number in floating point
value and then determine from that what the user typed.
Hope you wil be able to solve this problem also.
 Kuljit  
P: n/a

Kuljit wrote: Hello everyone, Thankyou for your help. i have written the code as you have said to accept it as a string and got the output, but as many of you have said this is not a floating point problem.
You should quote what you're replying to, even if you're replying to
yourself.
So can we ask the user to enter the decimal number in floating point value and then determine from that what the user typed.
Hope you wil be able to solve this problem also.
Luckily for you, I'm using Google at the moment (grrrr) and can
actually see what the original problem was, so I may be able to help...
Are you now referring to numbers of the 1.23e12 format? If yes:
The only difference is figuring out the rules that these numbers
follow, and checking the input against them (read in, and walk through
the string). The rules are (I may get this wrong, please do check):
<float> ::= <fixed><exponent><integer>.
<fixed> ::= [<sign>]<unsigned>['.' [<unsigned>]].
<exponent> ::= 'e'  'E'  'd'  'D'.
<integer> ::= [<sign>]<unsigned>.
<sign> ::= '+'  ''.
<unsigned> ::= <digit>[<digit>]*.
<digit> ::= '0' ... '9'.
Now, using the grammar above, depending on where you encounter '\0',
you'll know exactly what sort of the number was entered (including
integers, both signed and unsigned).
Note:
<> token
::= is
 one of
[] optional
* zero or more
. end of the rule

BR, Vladimir  
P: n/a

Vladimir S. Oka wrote: Kuljit wrote: Hello everyone, Thankyou for your help. i have written the code as you have said to accept it as a string and got the output, but as many of you have said this is not a floating point problem. You should quote what you're replying to, even if you're replying to yourself.
So can we ask the user to enter the decimal number in floating point value and then determine from that what the user typed.
Hope you wil be able to solve this problem also.
Luckily for you, I'm using Google at the moment (grrrr) and can actually see what the original problem was, so I may be able to help...
Are you now referring to numbers of the 1.23e12 format? If yes:
The only difference is figuring out the rules that these numbers follow, and checking the input against them (read in, and walk through the string). The rules are (I may get this wrong, please do check):
<float> ::= <fixed><exponent><integer>. <fixed> ::= [<sign>]<unsigned>['.' [<unsigned>]]. <exponent> ::= 'e'  'E'  'd'  'D'. <integer> ::= [<sign>]<unsigned>. <sign> ::= '+'  ''. <unsigned> ::= <digit>[<digit>]*. <digit> ::= '0' ... '9'.
Obviously:
<digit> ::= '0'  '1'  ...  '9'.
:( Now, using the grammar above, depending on where you encounter '\0', you'll know exactly what sort of the number was entered (including integers, both signed and unsigned).
Note: <> token ::= is  one of [] optional * zero or more . end of the rule
 BR, Vladimir  
P: n/a

Dear Vladmir,
Thankyou for your reply.
I think you havent understood what i meant. My original text was
Kuljit wrote: I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
so i want to count the number of digits after the decimal point as i
have said in my quoted text. But instead of accepting it in a string, i
want to accept it in float datatype.
So please try to help me out with this problem.
Kuljit  
P: n/a

On 20060224, Kuljit <ku**********@gmail.com> wrote: Dear Vladmir, Thankyou for your reply. I think you havent understood what i meant. My original text was
Kuljit wrote:
I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the number of digits in a given number. I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
so i want to count the number of digits after the decimal point as i have said in my quoted text. But instead of accepting it in a string, i want to accept it in float datatype.
So please try to help me out with this problem.
Kuljit
You can read it into a string first, count your decimal places
and then convert the string to a float : google will help you convert a
string to a float. All the things to be careful about in parsing the
string to detect number of decimal places are already covered in the thread.
I can see no reason why you would want to calculate decimal places
from the float variable when you can have the string representation to
start with. You can store the input in a float afterwards maybe?
See here : http://tinyurl.com/heyck
This covers using scanf, strtod and others with the usual style and
robustness arguments prevalent in all threads to do with string to
number conversions :)

Remove evomer to reply  
P: n/a

BEGIN PGP SIGNED MESSAGE
Hash: SHA1
Kuljit wrote:
[snip] Ask the user to enter a decimal/float number(eg. 32.8952), then count the number of digits in that number after the decimal point(4 in this case).
eg: If user enters 45.99420 then we should get 5, which are the number of digits after the decimal point.
so i want to count the number of digits after the decimal point as i have said in my quoted text. But instead of accepting it in a string, i want to accept it in float datatype.
Unfortunately, if (as you say) you want to count the number of digits
after the decimal point in a /floating point number/ (and not a string),
then your examples are a bit out of whack.
Consider that the user enters the string "32.8952", and you convert it
to a floatingpoint number (your first example). There are clearly 4
significant digits following the decimal point. *But*, 32.8952 /as a
rational number/ is the same as 32.89520, which (by your second example)
you wish to count as 5 digits after the decimal point. /And/, the number
is the same as 32.895200, which is 6 decimal places (by extension of
your two examples).
Do you see the problem? Which interpretation of the number is correct
for your count?
Also, when working with /floatingpoint/ numbers, floatingpoint is
inaccurate: a number /entered/ as 45.99420 might actually become
45.994197827734, which will have a different count of significant
decimal places than you expect
So, to satisfy your count requirement, you cannot count the digits in
the floatingpoint datatype.
 
Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
BEGIN PGP SIGNATURE
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla  http://enigmail.mozdev.org
iD8DBQFD/y9/agVFX4UWr64RAi9eAJ4uhYQiboRiLlx6FjtRvS9sb8zGtwCfa8 1k
BMvnCHhESl8LOYmRfeg0YeQ=
=Brvz
END PGP SIGNATURE   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 11121
 replies: 18
 date asked: Feb 15 '06
