![]() |
|
#1
|
|||
|
|||
Help Needed With Floor FunctionI HAVE NO ERRORS BUT MY CODE IS NOT WORKING CORRECTLY PLEASE HELP
BELOW IS MY CODE, I AM SO CONFUSED RIGHT NOW, I DON'T UNDERTAND WHY MY FUNCTION FLOOR IS NOT DOING WHAT IT IS SUPPOSED TO BE DOING. I AM SUPPOSED TO REVEIVE THE FLOOR VALUE ROUNDED IN THE AS SUCH: CPP / C++ / C Code:
Last edited by LuciWiz : 17-Nov-2004 at 00:46.
Reason: PLEASE INCLUDE YOUR c CODE BETWEEN [c] & [/c] TAGS!
|
|||
|
#2
|
|||
|
|||
|
Quote:
First of all IT'S NOT NECESSARY TO SHOUT! I do know it's frustrating when things don't work out the way that you thought they would the first time and you can't see why not. Been there ... done that. Now, If you want some help, the first thing to do is 1. Tell us what you did. 2. Tell us what you expected to get. 3. Tell us what you got. It's not clear to me what you don't understand. Is it that someone gave you some code using floor() and a little arithmetic to perform rounding and you don't understand the concept of rounding? Is it that you don't understand floor()? Is it that you don't know how to make a function in C that returns a value to a calling function? Or what??? Now, having said that, I'll look at one of your functions; let's try the roundToThousandths(). (By the way, you never call this function in main().) CPP / C++ / C Code:
First of all: The comments are irrelevant to this function (and incorrect for any version of floor() that I have ever seen). Incorrect comments are worse than no comments at all, since I often try to make the module behave as the comments indicate that it should. The function floor() that you are calling is part of the C Standard math library. It takes a double argument and returns a double value. The value that it returns is the largest integer value that is less than or equal to its argument. Or, putting it another way, the return value has the type "double", but its value is a "whole number". For now, we consider positive numbers only (since the rounding formulas you have don't work right for negative numbers). Now, you are proposing a formula for rounding to the nearest thousandths: CPP / C++ / C Code:
Since your program doesn't give the answers you expect, let's run through a few examples with "pencil and paper" (or in our heads, or in a text editor, or...) Suppose we have a number, say 1.2345678, and we want to round to the nearest thousandth. You know what the "right answer" is, right? (I say it's 1.235. What do you say?) Now look at the formula, and do things a step at a size: number = 1.2345678. number*1000 = 1234.5678 number*1000 + .5 = 1235.0678 floor(number*1000+.5) = 1235.000 floor(number*1000 +.5)/1000 = 1.235000 Get the idea: multiply so that the decimal point is to the right of whatever correct digit you want. Now add .5, so that if the fractional part is >=.5, the whole number part will be incremented. If the fractional part is < .5, the whole number will not be incremented. That's called rounding. Finally, divide by whatever you first multiplied by so that the decimal place in the result is correct. Try it again, with other numbers, say 1.234456787, for example. The "right answer" this time is 1.234000. See? Now look at your function: You calculate z according to the formula. But wait a minute: you have declared z to be an int. Why? Why not let z be a double, so that everything you did on pencil and paper will be the same. Ok, now all you have to do is return the value you just painstakingly calculated. But wait! Hold everything! You returned the number that it came in with! (Why, oh why did you do this?) Simply return z, and you are ready for testing. So, in general here's the plan: Figure out what you are supposed to do. Figure out how to do it. Do it. Test it. If Test results are not what you expected, well try, try again. Regards, Dave |
|
#3
|
|||
|
|||
|
:-) Hey Dave,
I was not trying to come off as shouting, but that code really was driving me crazy, it complied fine and everything, but all along it was the programmer who did not understand what the problem was asking in the first place. I would just like to say thank you for your help, some people just need things drawn out and explained in great detail, and I am one of them. Again thanks for your help. Nexa |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with coding expectation maximization needed. thanks | edge | C Programming Language | 8 | 08-Nov-2005 05:00 |
| Small Help Needed | p_kumar81 | C++ Forum | 35 | 09-Nov-2004 10:03 |
| Help With Site Needed | stu007 | Web Design Forum | 7 | 02-Feb-2004 08:46 |
| Free 1st month / Free setup / No credit card needed...Plans start at 4.95 | LarryIsaac | Web Hosting Advertisements & Offers | 0 | 11-Oct-2003 14:03 |
| Mailers Needed | joemailman | Open Discussion Forum | 2 | 18-Aug-2003 08:24 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The