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 18 12133
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.
"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.
"osmium" writes:
<snip>
Never mind. Wrong news froup.
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...)
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...)
-----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-----
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
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...)
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 ---
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.flash-gordon.me.uk/
comp.lang.c posting guidlines and intro - http://clc-wiki.net/wiki/Intro_to_clc
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.
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
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
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.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
--
BR, Vladimir
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.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
-- BR, Vladimir
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
On 2006-02-24, 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
-----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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Peter Blatt |
last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places
BEFORE the decimal point? Moreover does the number include the decimal point?
Are there...
|
by: italia |
last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal
Places = 6.
I had decimals in the original field. But after the transfer, the
digits after the decimals are gone.
Now...
|
by: guidosh |
last post by:
Hello,
I'm trying to write a printf statement that sets the field width when
printing a number. I'm using this:
printf("%*", fieldwidth, num_to_print);
However, I can't figure out how to...
|
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= |
last post by:
Hello ,
Does someone knows a simple way of how to get the nr of fraction digits ?
example :
1.23 would give a result of 2
1.234 would give a result of 3
Yes
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |