anonymous
  • anonymous
How do you make an algorithm to validate if a floating point value is a floating point value in C?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
So far I've tried using isalpha() but it does not seem to work. Does this work for floating point values?
anonymous
  • anonymous
try the math library, particularly the ceilf() and floorf() functions, or maybe even the modf() function
anonymous
  • anonymous
http://ideone.com/q6FJu

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
Is it possible to accomplish this using only the libraries string.h and stdio.h?
anonymous
  • anonymous
So you are trying to test string representation of numbers?
anonymous
  • anonymous
I think it is possible.
anonymous
  • anonymous
let me try :-P I will only include and
anonymous
  • anonymous
Well, I'm asking the user to input a floating point value, and check if the input is indeed a floating point.
anonymous
  • anonymous
prompting the user? then it is very possible.
anonymous
  • anonymous
Using scanf, more specifically.
anonymous
  • anonymous
http://ideone.com/DpEMx The idea is to get input as a string and parse it to test if it is a float.
anonymous
  • anonymous
I still think that using the math.h functions to test the float value itself is far better than using the string and stdio functions the way I just did :-P
anonymous
  • anonymous
This is what I've done so far, but it doesn't seem to be working well. Basically, I'm using strchr to see if the string AmtInput contains the characters ".0123456789", and assigning variables for later use. Where am I going wrong? int i, FloatPoint; char AmtInput[99]; int main() { scanf("%s", AmtInput); getchar(); // Check if user input is a floating point value for (i = 0; i <= strlen(AmtInput); i++) { if (strchr(".0123456789", AmtInput[i]) != 0) { printf("Not floating pt\n"); // User input IS a floating point value FloatPoint = 1; i = strlen(AmtInput) + 1; } } }
anonymous
  • anonymous
Well it may be easier, but the problem is as of now, I don't know much about functions outside string.h and stdio.h. I may just end up stabbing in the dark if I use other libraries.
anonymous
  • anonymous
You won't be stabbing in the dark if you use anything out of the C standard library, though :-D Go ahead, try out the nice math library so that you can test it in one line instead of a complicated loop.
anonymous
  • anonymous
try posting your code on a site like http://ideone.com with input to see how it works. Right now I think your code's logic is a bit messed up. in particular, the if block contains two different things, one saying the input is not a float and another saying your input is a float. What happens is the first character (which is always a digit or a decimal point whether it is a whole number or a fractional number) is passed to strchr, returning a non-null pointer and therefore executing the code in the ifblock, saying that your number is "Not floating pt"
anonymous
  • anonymous
Yes. You can do it only using and header file. Try strtok() library function. I think it could help .
anonymous
  • anonymous
Just think that, Floating point number can be divide into token.( By split ".") :D , I think you got my idea.
anonymous
  • anonymous
or just do this #include int is_float(double num) { return ceil(num) != floor(num) }
anonymous
  • anonymous
I forgot to say that I don't need to check if it's a floating point per se; I just need the algorithm to recognize if it contains characters aside from . 0 1 2 3 4 5 6 7 8 9. Here's my take: AmtInput[99] FloatSymbols[11] = {".0123456789"} // Check if user input contains characters aside from ".0123456789" for (i = 0; i <= strlen(AmtInput); i++) { for (j = 0; j < 11; j++) { if (FloatSymbols[j] == AmtInput[i]) { FCount++; } } } // Check for validity of input if(FCount != strlen(AmtInput)) { MsgInvalidInput(); } There's more to my source code that's not relevant to the question, so I'm leaving them out. For my simple purposes, this works.

Looking for something else?

Not the answer you are looking for? Search for more explanations.