GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2004, 14:37
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road

Help Needed With Floor Function


I 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:
#include <cmath>
#include <iostream>
using namespace std;

//Function Prototypes
double roundToInteger(double number);
double roundToTenths(double number);
double roundToHundredths(double number);
double roundToThousandths(double number);



int main() 
{
  double number;
  
  cout << "Enter an Interger: " << endl;
  cout << "[Interger will be rounded to integer, tenths, hundredths, and thousandths value] " << endl;
  cin >> number;
  
  cout << "roundToInteger of your number is: " << roundToInteger(number) << endl;
  cout << "roundToInTenths of your number is: " << roundToTenths(number) << endl;
  cout << "roundToHundredths of your number is: " << roundToHundredths(number) << endl;
  cout << "roundToThousandths of your number is: " << roundToHundredths(number) << endl;
  
  return 0;
}

//*************************************
// function floor:                    *
// This function returns the floor    *
// of the integer passed to it.       *
//*************************************
double roundToInteger(double number)
{ 
	int w;

	w = floor(number * 1 + .5) / 1;

	return(number);

}
//*************************************
// function floor:                    *
// This function returns the floor    *
// of the integer passed to it.       *
//*************************************
double roundToTenths(double number)
{
  
	int x;
  
  x = floor(number) ;
  
  return(number);
}
//*************************************
// function floor:                    *
// This function returns the floor    *
// of the integer passed to it.       *
//*************************************
double roundToHundredths(double number)
{
  
	int y;
  
  y = floor(number * 100 + .5) / 100;
  
  return(number);
}
//*************************************
// function floor:                    *
// This function returns the floor    *
// of the integer passed to it.       *
//*************************************
double roundToThousandths(double number)
{
  
	int z;
  
  z = floor(number * 1000 + .5) / 1000;
  
  return(number);
}
Last edited by LuciWiz : 17-Nov-2004 at 01:46. Reason: PLEASE INCLUDE YOUR c CODE BETWEEN [c] & [/c] TAGS!
  #2  
Old 16-Nov-2004, 19:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Nexa
I 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:


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:
//*************************************
// function floor:                    *
// This function returns the floor    *
// of the integer passed to it.       *
//*************************************
double roundToThousandths(double number)
{
  
	int z;
  
  z = floor(number * 1000 + .5) / 1000;
  
  return(number);
}


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:
z = floor(number * 1000 + .5) / 1000;

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  
Old 17-Nov-2004, 01:37
Nexa Nexa is offline
Awaiting Email Confirmation
 
Join Date: Nov 2004
Location: Florida
Posts: 23
Nexa is on a distinguished road
:-) 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 GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with coding expectation maximization needed. thanks edge C Programming Language 8 08-Nov-2005 06:00
Small Help Needed p_kumar81 C++ Forum 35 09-Nov-2004 11:03
Help With Site Needed stu007 Web Design Forum 7 02-Feb-2004 09:46
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 15:03
Mailers Needed joemailman Open Discussion Forum 2 18-Aug-2003 09:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.