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(n umber)
3-roundToHundredt hs(number)
4- roundToThousand ths(number)
for each value read, your program should print the original value, the number rounded to nearest tenths,hundredt hs,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<iostre am>
using std::cout;
using std::cin;
using std::endl;
# include<cmath>
//declaration of functions
void roundToInteger( double);
void roundToTenths(d ouble);
void roundToHundredt hs(double);
void roundToThousand ths(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”<<end l;
cout<<”-4- round decimal to the nearest thousandth”<<en dl;
cout<<”please enter a decimal”<<endl;
cin>>x;
cout <<"You chose Menu Option #"<<choice<<end l ;
}
while(selection <1 || selection>4);
switch(choice)
{
case 1 :
roundToInteger( number);
break ;
case 2 :
roundToTenths(n umber);
break;
case 3 :
roundToHundredt hs(number);
break;
case 4:
roundToThousand ths(number);
break;
default:
cout<<”you should never get here\a\a\a\a!!! ”;
}
}while(choice != 5);
cout<<”you choose to quit\a\a\a"<<en dl;
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 roundToHundredt hs(double number)
{
y=floor(x*100+0 .5)/100;
cout<<”the decimal ”<<x<<” rounded to the nearest hundredths = “<<number<<endl ;
}
void roundToThousand ths(double number)
{
y=floor(x*1000+ 0.5)/1000;
cout<<”the decimal ”<<x<<” rounded to the nearest thousandths = “<<number<<endl ;
}
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(n umber)
3-roundToHundredt hs(number)
4- roundToThousand ths(number)
for each value read, your program should print the original value, the number rounded to nearest tenths,hundredt hs,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<iostre am>
using std::cout;
using std::cin;
using std::endl;
# include<cmath>
//declaration of functions
void roundToInteger( double);
void roundToTenths(d ouble);
void roundToHundredt hs(double);
void roundToThousand ths(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”<<end l;
cout<<”-4- round decimal to the nearest thousandth”<<en dl;
cout<<”please enter a decimal”<<endl;
cin>>x;
cout <<"You chose Menu Option #"<<choice<<end l ;
}
while(selection <1 || selection>4);
switch(choice)
{
case 1 :
roundToInteger( number);
break ;
case 2 :
roundToTenths(n umber);
break;
case 3 :
roundToHundredt hs(number);
break;
case 4:
roundToThousand ths(number);
break;
default:
cout<<”you should never get here\a\a\a\a!!! ”;
}
}while(choice != 5);
cout<<”you choose to quit\a\a\a"<<en dl;
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 roundToHundredt hs(double number)
{
y=floor(x*100+0 .5)/100;
cout<<”the decimal ”<<x<<” rounded to the nearest hundredths = “<<number<<endl ;
}
void roundToThousand ths(double number)
{
y=floor(x*1000+ 0.5)/1000;
cout<<”the decimal ”<<x<<” rounded to the nearest thousandths = “<<number<<endl ;
}