Digits after the decimal point

K

Kuljit

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
 
G

gamehack

Kuljit said:
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.
 
O

osmium

Kuljit said:
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, much like an array.
 
N

Nelu

gamehack said:
Kuljit said:
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.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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+vuyk5eZxACeMQf0
qnivmuy3/BTPu8WsLeK0+9o=
=FNJR
-----END PGP SIGNATURE-----
 
N

Nelu

Nelu said:
gamehack said:
Kuljit said:
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.
 
M

Michael Mair

Nelu said:
Nelu said:
gamehack said:
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
 
N

Nelu

Michael said:
Nelu said:
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.
 
F

Flash Gordon

gamehack said:
Kuljit said:
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.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
 
J

Joe Wright

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



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.
 
O

Old Wolf

gamehack said:
Kuljit said:
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.
 
B

Barry Schwarz

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
 
K

Kuljit

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
 
V

Vladimir S. Oka

Kuljit said:
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.23e-12 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
 
V

Vladimir S. Oka

Vladimir said:
Kuljit said:
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.23e-12 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
 
K

Kuljit

Dear Vladmir,
Thankyou for your reply.
I think you havent understood what i meant. My original text was
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
 
R

Richard G. Riley

Dear Vladmir,
Thankyou for your reply.
I think you havent understood what i meant. My original text was



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 :)
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kuljit wrote:
[snip]
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/agVFX4UWr64RAi9eAJ4uhYQiboRiLlx6FjtRvS9sb8zGtwCfa81k
BMvnCHhESl8LOYmRfeg0YeQ=
=Brvz
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top