Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
...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:
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
$100 * 5/100 would give the amount of the discount in $.
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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

macrumors 68040
Jan 10, 2005
3,182
3
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.
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,731
8,407
A sea of green
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.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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

Moderator
Staff member
Aug 9, 2009
10,731
8,407
A sea of green
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)
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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.
 

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
Is it returning EOF because of the input failure (say, entering a zero)?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,731
8,407
A sea of green
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

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
Prof introduced us formally to formatted I/O today; we'll be doing file I/O in the next class.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.