PDA

View Full Version : C question: How to test if an input value...




mac2x
Sep 25, 2010, 08:44 AM
...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. :o

Any hints are appreciated. :cool:



miles01110
Sep 25, 2010, 08:47 AM
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?

lee1210
Sep 25, 2010, 08:48 AM
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

mac2x
Sep 25, 2010, 08:51 AM
$100 * 5/100 would give the amount of the discount in $.

mac2x
Sep 25, 2010, 08:55 AM
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

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:


Get input
while input > 0
if
else if
else if...
...
else
get input
end while

miles01110
Sep 25, 2010, 08:56 AM
Get input
while input > 0
if
else if
else if...
...
else
get input
end while

:mad:

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

mac2x
Sep 25, 2010, 09:01 AM
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:

lee1210
Sep 25, 2010, 10:09 AM
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.

Sydde
Sep 25, 2010, 10:54 AM
Would using something like

abs( targetValue - inputValue )

save a step?

mac2x
Sep 25, 2010, 05:03 PM
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.

percentError = ( abs( actual - approx )/abs( actual )) * 100

chown33
Sep 25, 2010, 05:18 PM
percentError = ( abs( actual - approx )/abs( actual )) * 100

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.

Sydde
Sep 25, 2010, 06:02 PM
Small test programs are also useful for testing your assumptions about how something really works.

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.

mac2x
Sep 25, 2010, 06:31 PM
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?

chown33
Sep 25, 2010, 08:29 PM
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:
./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:
./yourProg <differentFile.txt

http://en.wikipedia.org/wiki/End-of-file
http://en.wikipedia.org/wiki/Redirection_(computing)

mac2x
Sep 25, 2010, 09:29 PM
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. :o


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.

mac2x
Sep 26, 2010, 02:18 PM
Is it returning EOF because of the input failure (say, entering a zero)?

chown33
Sep 26, 2010, 02:38 PM
Is it returning EOF because of the input failure (say, entering a zero)?

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.

mac2x
Sep 26, 2010, 04:21 PM
Allrighty then, thanks.

jared_kipe
Sep 26, 2010, 04:23 PM
You should be warned against posting any code you plan on using or submitting for homework.

mac2x
Sep 26, 2010, 05:28 PM
You should be warned against posting any code you plan on using or submitting for homework.

OK then. Why didn't you say so earlier?

lloyddean
Sep 26, 2010, 06:34 PM
Different treatment for active students versus everyone else? I very much disagree with such.

mac2x
Sep 27, 2010, 03:23 AM
Different treatment for active students versus everyone else? I very much disagree with such.


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

mac2x
Sep 27, 2010, 08:48 PM
Prof introduced us formally to formatted I/O today; we'll be doing file I/O in the next class.

jared_kipe
Sep 27, 2010, 11:47 PM
OK then. Why didn't you say so earlier?

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.

mac2x
Sep 28, 2010, 12:35 AM
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.

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.