1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

C question: How to test if an input value...

Discussion in 'Mac Programming' started by mac2x, Sep 25, 2010.

  1. macrumors 65816

    #1
    ...is within 5% of a target value. This IS an assignment (on loops, if's, and else if's), but I'll do my best to only ask about this one detail. I haven't actually written any code yet; trying to get a working paper algorithm first.

    I want to test if the value of a numerical input is within 5% of a target numerical value (which will be defined as a constant). Problem is, I'm not sure how to approach this. :eek:

    Any hints are appreciated. :cool:
     
  2. macrumors Core

    miles01110

    #2
    If someone told you, "This item costs $100, but I'll give it to you at a 5% discount if you buy it right now!" ...how would you go about determining if you had enough money?
     
  3. macrumors 68040

    lee1210

    #3
    My approach would be to get two values, the upper and lower bound. You can do this by multiplying by two numbers. I won't say which. Then see if the input is >= to the first AND <= to the other.

    This doesn't require a loop, but I can't think of a way to do this in a loop. Maybe the loop just runs until they enter a valid value?

    -Lee
     
  4. macrumors 65816

    #4
    $100 * 5/100 would give the amount of the discount in $.
     
  5. macrumors 65816

    #5
    I'll be thinking about that. :) Thanks!

    The required algorithm (as in you won't get a good grade if you don't follow it) for the assignment is as follows:

    Code:
    Get input
        while input > 0
            if
            else if
            else if...
                ...
            else
            get input
        end while
     
  6. macrumors Core

    miles01110

    #6
    :mad:

    Doesn't seem like a very efficient way to do it, even to my awful "programmer's" eye. Oh well... school is school.
     
  7. macrumors 65816

    #7
    I know...I rattled off the first few assignments in no time flat; had 'em working in 30 minutes or less. This one is making me pull my hair. :mad:
     
  8. macrumors 68040

    lee1210

    #8
    Looks like your if could be:
    if inputValue < minValue
    print error
    Elseif inputValue > maxValue
    Print error
    else // success
    accept value, break out of loop

    Otherwise I can't think of a way, especially with floating point values, to enumerate acceptable values.

    If getting input has to be at the end of the loop, not the start, you'll have to use break. Otherwise, if input was at the start of the loop, on success input can be set to 0 and the loop will exit naturally, though you'd have to save the entered value elsewhere. On error, input would need to be set to 1 in case the input was negative.

    -Lee

    Edit: I see that the provided structure has fallen into the odd pattern of first input outside the loop, then getting it again at the end. IMO, the first time they enter a value isn't special. As I sits, if the first input is 0 or negative the loop never executes. That means it "passes" even if it's no in range (if your target value is positive, a 0 or negative is never in range.
     
  9. macrumors 68000

    Sydde

    #9
    Would using something like

    abs( targetValue - inputValue )

    save a step?
     
  10. macrumors 65816

    #10
    No, because I need a percentage. I'm wondering of the good old percent error calculation I used all the time in chemistry would work.

    Code:
    percentError = ( abs( actual - approx )/abs( actual )) * 100
     
  11. macrumors 603

    #11
    Try it, see what happens. Write a small test program that does the calculation and shows results, then call that calculation with test values that you know lie inside and outside the expected 5% range.

    It's not uncommon to write small test programs that exercise some individual facet of a larger program. This is especially so when you're exploring, looking for an algorithm that gives the output you want, without knowing ahead of time exactly which algorithm will work. Small test programs are also useful for testing your assumptions about how something really works.
     
  12. macrumors 68000

    Sydde

    #12
    I keep a few old Cocoa projects lying around that were nothing more than exercises from working through the Hillegass book. Not for sentimental reasons but so it can open one up, create a test object, and work it out before fitting it into a real project.
     
  13. macrumors 65816

    #13
    Thanks for the idea, chown33. It makes it a lot easier to work out the bugs (the first if is always true no matter what). I'm working through figuring out exactly what is going on, and it sure is a whole lot easier with a snippet of the full program!

    OK, I got a working program. I'll share the code with you guys since I've got it done. :)

    [edit] code removed

    All critiques/comments are welcome, now that I have (hopefully) all of the work done. ;) FWIW, the program successfully provides the sample output given in the assignments.

    [edit] the instructor intends for the loop to terminate once finished by inputting a false value for boiling point; is there a more elegant way to end it?
     
  14. macrumors 603

    #14
    Look at the reference docs for scanf(). What does it return?

    What does it return when the input stream encounters EOF? (Assuming you know what EOF is.)

    From the Terminal or Xcode's Console window, EOF is when you enter Control-D on the keyboard.

    Reason for using EOF: it lets you run and test using input redirected from a file.
    Suppose you have a file, "temps.txt", containing test input values, one per line. Then this command:
    Code:
    ./yourProg <temps.txt
    
    will run yourProg from the current directory, and redirect its input to read the file temps.txt. If you had a different file, you can read it with:
    Code:
    ./yourProg <differentFile.txt
    
    http://en.wikipedia.org/wiki/End-of-file
    http://en.wikipedia.org/wiki/Redirection_(computing)
     
  15. macrumors 65816

    #15
    According to my understanding of what the man page says, it is returning zero in the case of my program above, when a false value is input? But the input is tested by the while loop...ahhh this is twisting my brain. :eek:


    Code:
    RETURN VALUES
         These functions return the number of input items assigned.  This can be
         fewer than provided for, or even zero, in the event of a matching failure.
         Zero indicates that, although there was input available, no conversions were
         assigned; typically this is due to an invalid input character, such as an
         alphabetic character for a `%d' conversion.  The value EOF is returned if an
         input failure occurs before any conversion such as an end-of-file occurs.
         If an error or end-of-file occurs after conversion has begun, the number of
         conversions which were successfully completed is returned.
     
  16. macrumors 65816

    #16
    Is it returning EOF because of the input failure (say, entering a zero)?
     
  17. macrumors 603

    #17
    No.

    It seems clear to me that you don't understand enough about C's Standard I/O input streams to do anything "elegant" about signaling the end of input, and having you read man pages to figure it out isn't working. That's my fault for making a guess.

    At this point, my suggestion is to forget anything I said about EOF. You're not in a position at this point to do much in the way of elegance.

    The simplest thing is just what you've done. If the just-input boiling point is zero or negative, stop.

    This is neither foolproof nor elegant, but it's within your current skill set, and that's all you really need right now. Later on, after learning about Standard I/O streams, you can revisit the issue. For now, ignore it.
     
  18. macrumors 65816

    #18
    Allrighty then, thanks.
     
  19. macrumors 68030

    jared_kipe

    #19
    You should be warned against posting any code you plan on using or submitting for homework.
     
  20. macrumors 65816

    #20
    OK then. Why didn't you say so earlier?
     
  21. macrumors 6502a

    #21
    Different treatment for active students versus everyone else? I very much disagree with such.
     
  22. macrumors 65816

    #22

    Thanks for the support, but I decided to remove the code anyway. Anyone who wants to see it again, feel free to PM me. :)
     
  23. macrumors 65816

    #23
    Prof introduced us formally to formatted I/O today; we'll be doing file I/O in the next class.
     
  24. macrumors 68030

    jared_kipe

    #24
    I said so as soon as I felt it was warranted. Or as soon as I scanned through the thread and realized you posted code and said it was for class.

    Were you not there on the first day of class when they presumably told all of the students about the no code sharing policies that most, if not all, classes have? Perhaps going as far as outlining the percentage of students caught cheating or outlining their aggressive software based analysis of your code to catch people reusing code from 25 years ago when the class was first taught. All the students attending probably even got warned that it doesn't matter if you are the "sharer" or the "borrower" of the code, both acts are considered cheating by most CS departments.
     
  25. macrumors 65816

    #25
    Thanks for your concern, but not only have I attended every single class and completed every single assignment so far as directed ON MY OWN (developed the algorithm, tested it, and wrote MY OWN code for it), there was never so much as a word about plagiarism regarding the posting of code on the internet. Policies differ, and just because the school(s) you are familiar with provide such warnings, it doesn't mean it applies to my situation. Our assignments typically are coming directly from various books (not the book we are using), and are not really Department property in any case.
     

Share This Page