A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

How do you make an algorithm to validate if a floating point value is a floating point value in C?

  • This Question is Closed
  1. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So far I've tried using isalpha(<variable name>) but it does not seem to work. Does this work for floating point values?

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    try the math library, particularly the ceilf() and floorf() functions, or maybe even the modf() function

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://ideone.com/q6FJu

  4. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Is it possible to accomplish this using only the libraries string.h and stdio.h?

  5. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    So you are trying to test string representation of numbers?

  6. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think it is possible.

  7. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    let me try :-P I will only include <string.h> and <stdio.h>

  8. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Well, I'm asking the user to input a floating point value, and check if the input is indeed a floating point.

  9. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    prompting the user? then it is very possible.

  10. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Using scanf, more specifically.

  11. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://ideone.com/DpEMx The idea is to get input as a string and parse it to test if it is a float.

  12. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  13. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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; } } }

  14. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  15. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  16. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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"

  17. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yes. You can do it only using <string.h> and <stdio.h> header file. Try strtok() library function. I think it could help .

  18. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Just think that, Floating point number can be divide into token.( By split ".") :D , I think you got my idea.

  19. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    or just do this #include <math.h> int is_float(double num) { return ceil(num) != floor(num) }

  20. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.