# Rounding Numbers

Page 1 of 1

## 3 Replies - 43921 Views - Last Post: 30 March 2009 - 09:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=23026&amp;s=4917eec98622c11f1b9c8cb83fae9b91&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 elsa

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 18-January 07

# Rounding Numbers

Posted 18 January 2007 - 02:11 AM

hi..i need help in solving this program
the question that i have to solve says:
fuction floor can be used to round a number to a specific decimal place. the statement y=floor(x*10+0.5)/10; rounds x to the tenths position. write a program that defines four functions to round a number x in various ways:
1- roundToInteger(number)
2- roundToTenths(number)
3-roundToHundredths(number)
4- roundToThousandths(number)
for each value read, your program should print the original value, the number rounded to nearest tenths,hundredths,thousandths.
this is what i have tried to write..but ofcourse..and like usual..lol..i have lots of mistakes..i would really appreciate the help..
```#include<iostream>
using std::cout;
using std::cin;
using std::endl;

# include<cmath>

//declaration of functions
void roundToInteger(double);
void roundToTenths(double);
void roundToHundredths(double);
void roundToThousandths(double);

int main()
{
unsigned short choice;
do
{
float x;
cout<<”-1- round decimal to the nearest integer”<<endl;
cout<<”-2- round decimal to the nearest tenth”<<endl;
cout<<”-3- round decimal to the nearest hundredth”<<endl;
cout<<”-4- round decimal to the nearest thousandth”<<endl;
cout<<”please enter a decimal”<<endl;
cin>>x;
cout <<"You chose Menu Option #"<<choice<<endl;
}
while(selection<1 || selection>4);

switch(choice)
{
case 1 :
roundToInteger(number);
break;
case 2 :
roundToTenths(number);
break;
case 3 :
roundToHundredths(number);
break;
case 4:
roundToThousandths(number);
break;
default:
cout<<”you should never get here\a\a\a\a!!!”;
}
}while(choice != 5);
cout<<”you choose to quit\a\a\a"<<endl;
return 0;
}

//definition of functions
void roundToInteger(double number)
{
y= floor(x+0.5);
cout<<”the decimal ”<<x<<” rounded to the nearest integer = “<<number<<endl;
}
void roundToTenths( double number)
{
y=floor(x*10+0.5)/10;
cout<<”the decimal ”<<x<<” rounded to the nearest tenths = “<<number<<endl;
}
void roundToHundredths(double number)
{
y=floor(x*100+0.5)/100;
cout<<”the decimal ”<<x<<” rounded to the nearest hundredths = “<<number<<endl;
}
void roundToThousandths(double number)
{
y=floor(x*1000+0.5)/1000;
cout<<”the decimal ”<<x<<” rounded to the nearest thousandths = “<<number<<endl;
}
```

Is This A Good Question/Topic? 0

## Replies To: Rounding Numbers

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Rounding Numbers

Posted 18 January 2007 - 06:43 AM

Can you please post the errors you are receiving, along with any undesired behaviour the program is displaying?

first and foremost, I can see that:

1. You are switching on a variable named choice which you never get from the user.
2. You are calling the functions and passing them a parameter names number, but you have not declared a variable named number, or assigned it a value.
3. You prompt the user for a value of variable x that you never use.
4. Each function uses a variable named y that has not been declared or instantiated.
5. Your while loop is using a variable named selection as its control, but that variable has never been declared or instantiated.

### #3 vandiesel

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 17-January 07

## Re: Rounding Numbers

Posted 18 January 2007 - 07:47 PM

elsa, on 18 Jan, 2007 - 02:11 AM, said:

hi..i need help in solving this program
the question that i have to solve says:
fuction floor can be used to round a number to a specific decimal place. the statement y=floor(x*10+0.5)/10; rounds x to the tenths position. write a program that defines four functions to round a number x in various ways:
1- roundToInteger(number)
2- roundToTenths(number)
3-roundToHundredths(number)
4- roundToThousandths(number)
for each value read, your program should print the original value, the number rounded to nearest tenths,hundredths,thousandths.
this is what i have tried to write..but ofcourse..and like usual..lol..i have lots of mistakes..i would really appreciate the help..
```#include<iostream>
using std::cout;
using std::cin;
using std::endl;

# include<cmath>

//declaration of functions
void roundToInteger(double);
void roundToTenths(double);
void roundToHundredths(double);
void roundToThousandths(double);

int main()
{
unsigned short choice;
do
{
float x;
cout<<”-1- round decimal to the nearest integer”<<endl;
cout<<”-2- round decimal to the nearest tenth”<<endl;
cout<<”-3- round decimal to the nearest hundredth”<<endl;
cout<<”-4- round decimal to the nearest thousandth”<<endl;
cout<<”please enter a decimal”<<endl;
cin>>x;
cout <<"You chose Menu Option #"<<choice<<endl;
}
while(selection<1 || selection>4);
cin>>choice;  // you need this for the switch statement for a choice
switch(choice)
{
case 1 :
roundToInteger(number);
break;
case 2 :
roundToTenths(number);
break;
case 3 :
roundToHundredths(number);
break;
case 4:
roundToThousandths(number);
break;
default:
cout<<”you should never get here\a\a\a\a!!!”;
}
}while(choice != 5);
cout<<”you choose to quit\a\a\a"<<endl;
return 0;
}
// floor is not defined in any of these functions.
// neither is x and in you switch statement, you are not passing any value for number
//y needs to be defined as well. Is it and int or a float, maybe a double.
//definition of functions
void roundToInteger(double number)
{
y= floor(x+0.5);
cout<<”the decimal ”<<x<<” rounded to the nearest integer = “<<number<<endl;
}
void roundToTenths( double number)
{
y=floor(x*10+0.5)/10;
cout<<”the decimal ”<<x<<” rounded to the nearest tenths = “<<number<<endl;
}
void roundToHundredths(double number)
{
y=floor(x*100+0.5)/100;
cout<<”the decimal ”<<x<<” rounded to the nearest hundredths = “<<number<<endl;
}
void roundToThousandths(double number)
{
y=floor(x*1000+0.5)/1000;
cout<<”the decimal ”<<x<<” rounded to the nearest thousandths = “<<number<<endl;
}
```

i'm not going to completely rewrite this but you can see a few errors you made. Also, when ever you pass a variable, make sure they are like variables. Float to a float, int to an int, double to a double...etc. See what you can do and let us know how it goes. Look at your loops as well. Your Do While loop needs some work as well. selection is not defined.

This post has been edited by vandiesel: 18 January 2007 - 07:51 PM

### #4 lignja

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 30-March 09

## Re: Rounding Numbers

Posted 30 March 2009 - 09:02 AM

I'm programming something for numerical math and I encountered a problem becouse c++ is rounding numbers and the then something that should be equal isn't. To stop yapping here is the code:

```	for ( i=0;i<n-2;i++){
if((x[i+1]-x[i])!=(x[i+2]-x[i+1])){ /
cout<<"UNOS NIJE ISPRAVAN, NISTE UNIJELI EKVIDISTANTNE CVOROVE, MOLIMO PONOVITE UNOS!"<<endl;
cout<<x[i+1]-x[i]<<endl;
cout<<x[i+2]-x[i+1]<<endl;
unos(x,y,n);
break;
}
}
```

And this is the output I get:
```Unesite broj cvorova koji zelite unijeti (max 8):
4
Unesite cvorove(x) i vrijednosti funkcije u tim cvorovima (y):

Unesite x0:1.05
Unesite y0:11
Unesite x1:1.1
Unesite y1:12
Unesite x2:1.15
Unesite y2:13
Unesite x3:1.2
Unesite y3:14
UNOS NIJE ISPRAVAN, NISTE UNIJELI EKVIDISTANTNE CVOROVE, MOLIMO PONOVITE UNOS!
[b]0.0500001
0.05[/b]
Unesite cvorove(x) i vrijednosti funkcije u tim cvorovima (y):

Unesite x0:

```

As you see I get that differences are not equal even though they are in reality. Is there any way that I could get over this "bug".

I would really apriciate your help. Thanks!