MacRumors Forums C question: How to test if an input value...
 User Name Remember Me? Password

 Thread Tools Search this Thread Display Modes
 Sep 25, 2010, 08:44 AM #1 mac2x macrumors 65816   Join Date: Sep 2009 C question: How to test if an input value... ...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. __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
 Sep 25, 2010, 08:47 AM #2 miles01110 macrumors 604     Join Date: Jul 2006 Location: The Ivory Tower (I'm not coming down) 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? __________________ Got a problem? Check here first. 0
 Sep 25, 2010, 08:48 AM #3 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX 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 0
 Sep 25, 2010, 08:51 AM #4 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 \$100 * 5/100 would give the amount of the discount in \$. __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
Sep 25, 2010, 08:55 AM   #5
mac2x
macrumors 65816

Join Date: Sep 2009
Quote:
 Originally Posted by lee1210 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:

Code:
```Get input
while input > 0
if
else if
else if...
...
else
get input
end while```
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
0
Sep 25, 2010, 08:56 AM   #6
miles01110
macrumors 604

Join Date: Jul 2006
Location: The Ivory Tower (I'm not coming down)
Quote:
 Originally Posted by mac2x Code: ```Get input while input > 0 if else if else if... ... else get input end while```

Doesn't seem like a very efficient way to do it, even to my awful "programmer's" eye. Oh well... school is school.
__________________
Got a problem? Check here first.
0
 Sep 25, 2010, 09:01 AM #7 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 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. __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
 Sep 25, 2010, 10:09 AM #8 lee1210 macrumors 68040     Join Date: Jan 2005 Location: Dallas, TX 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. Last edited by lee1210; Sep 25, 2010 at 10:18 AM. 0
 Sep 25, 2010, 10:54 AM #9 Sydde macrumors 68000     Join Date: Aug 2009 Would using something like abs( targetValue - inputValue ) save a step? __________________ You got to be a spirit. You can't be no ghost. 0
 Sep 25, 2010, 05:03 PM #10 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 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` __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
Sep 25, 2010, 05:18 PM   #11
chown33
macrumors 603

Join Date: Aug 2009
Quote:
 Code: `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.

Last edited by chown33; Sep 25, 2010 at 05:32 PM.
0
Sep 25, 2010, 06:02 PM   #12
Sydde
macrumors 68000

Join Date: Aug 2009
Quote:
 Originally Posted by chown33 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.
__________________
You got to be a spirit. You can't be no ghost.
0
 Sep 25, 2010, 06:31 PM #13 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 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? __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE Last edited by mac2x; Sep 26, 2010 at 05:29 PM. Reason: please use the edit button 0
 Sep 25, 2010, 08:29 PM #14 chown33 macrumors 603   Join Date: Aug 2009 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
 Sep 25, 2010, 09:29 PM #15 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 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.``` __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
 Sep 26, 2010, 02:18 PM #16 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 Is it returning EOF because of the input failure (say, entering a zero)? __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
Sep 26, 2010, 02:38 PM   #17
chown33
macrumors 603

Join Date: Aug 2009
Quote:
 Originally Posted by mac2x 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.
0
 Sep 26, 2010, 04:21 PM #18 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 Allrighty then, thanks. __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
 Sep 26, 2010, 04:23 PM #19 jared_kipe macrumors 68030     Join Date: Dec 2003 Location: Seattle You should be warned against posting any code you plan on using or submitting for homework. 0
Sep 26, 2010, 05:28 PM   #20
mac2x
macrumors 65816

Join Date: Sep 2009
Quote:
 Originally Posted by jared_kipe 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?
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
0
 Sep 26, 2010, 06:34 PM #21 lloyddean macrumors 6502a   Join Date: May 2009 Location: Des Moines, WA Different treatment for active students versus everyone else? I very much disagree with such. 0
Sep 27, 2010, 03:23 AM   #22
mac2x
macrumors 65816

Join Date: Sep 2009
Quote:
 Originally Posted by lloyddean 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.
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
0
 Sep 27, 2010, 08:48 PM #23 mac2x Thread Starter macrumors 65816   Join Date: Sep 2009 Prof introduced us formally to formatted I/O today; we'll be doing file I/O in the next class. __________________ Retina MBP | 2.6 GHz i7 | 16 GB RAM Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE 0
Sep 27, 2010, 11:47 PM   #24
jared_kipe
macrumors 68030

Join Date: Dec 2003
Location: Seattle
Quote:
 Originally Posted by mac2x 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.
0
Sep 28, 2010, 12:35 AM   #25
mac2x
macrumors 65816

Join Date: Sep 2009
Quote:
 Originally Posted by jared_kipe 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.
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
0

 MacRumors Forums

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Linear Mode

 Similar Threads thread Thread Starter Forum Replies Last Post MiniJim Mac mini 2 Apr 23, 2014 07:58 PM macmesser OS X Mavericks (10.9) 0 Jan 3, 2014 08:13 PM mixduptransisto iMac 1 Oct 22, 2012 08:58 AM rainman7 OS X 10.8 Mountain Lion 4 Aug 18, 2012 12:09 AM zefer iOS 5 and earlier 3 Jul 25, 2012 08:53 PM