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

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

1. ### mac2x macrumors 65816

Joined:
Sep 19, 2009
#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.

Any hints are appreciated.

2. ### miles01110 macrumors Core

Joined:
Jul 24, 2006
Location:
The Ivory Tower (I'm not coming down)
#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. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#4
\$100 * 5/100 would give the amount of the discount in \$.

5. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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. ### miles01110 macrumors Core

Joined:
Jul 24, 2006
Location:
The Ivory Tower (I'm not coming down)
#6

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

7. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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.

8. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#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. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#9
Would using something like

abs( targetValue - inputValue )

save a step?

10. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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. ### chown33 macrumors 604

Joined:
Aug 9, 2009
#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. ### Sydde macrumors 68020

Joined:
Aug 17, 2009
#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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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.

 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.

 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. ### chown33 macrumors 604

Joined:
Aug 9, 2009
#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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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.

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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#16
Is it returning EOF because of the input failure (say, entering a zero)?

17. ### chown33 macrumors 604

Joined:
Aug 9, 2009
#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.

Joined:
Sep 19, 2009
19. ### jared_kipe macrumors 68030

Joined:
Dec 8, 2003
Location:
Seattle
#19
You should be warned against posting any code you plan on using or submitting for homework.

20. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#20
OK then. Why didn't you say so earlier?

21. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
#21
Different treatment for active students versus everyone else? I very much disagree with such.

22. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#23
Prof introduced us formally to formatted I/O today; we'll be doing file I/O in the next class.

24. ### jared_kipe macrumors 68030

Joined:
Dec 8, 2003
Location:
Seattle
#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. ### mac2x thread starter macrumors 65816

Joined:
Sep 19, 2009
#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.