In: Computer Science
/*You will be have to round numbers very often so you decided to
create your own function round_off()
that will receive the number to be rounded and the number of
decimal digits that the number should be rounded to and will
return
the value rounded to the specified number of decimal digits. You
need to create a program to test the function.
It will ask the user to enter the double precision real number to
be rounded and a whole number indicating the number of decimal
digits.
It will then display the original number with ten digits and the
rounded value (also a double precision real number) with the number
of
digits specified by the user plus 2 more.
This assignment will be completed in two steps:
1) First you will implement the algorithm shown below in which the
rounding will be done in main()
2) Once you have this working you will need to modify your solution
so you: Declare the prototype of the function above main()
Call the function in main() to do the rounding Define the function
below main()
*/
#include <iostream>
// to use cin and cout
#include <typeinfo>
// to be able to use operator
typeid
// Include here the libraries that your program needs to compile
using namespace std;
// Ignore this; it's a little function used for making
tests
inline void _test(const char* expression, const char* file, int
line)
{
cerr << "test(" << expression << ")
failed in file " << file;
cerr << ", line " << line << "."
<< endl << endl;
}
// This goes along with the above function...don't worry about
it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 :
_test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototype of the function here
int main()
{
// Declare variable value, valuero that hold double precision real
numbers
double value;
double valuero;
// Declare variable decdig that holds whole numbers
int decdig;
// Prompt the user to "Enter the real number: "
cout << "Enter the real number: ";
// Read from keyboard the value entered by the user and assign it
to side
cin >> value;
// Prompt the user to "Enter number of digits: "
cout << "Enter number of digits: ";
// Read from keyboard the value entered by the user and assign it
to decdig
cin >> decdig;
// Round the real number to the number of decimal digits specified
and assign the result to valuero
// Format the output to display the numbers in fixed format with ten decimal digits
// Display on the screen, using 23 columns, the message
// "The original number is ", value
// Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2
// Display on the screen, using 23 columns, the message
// "The rounded number is ", valuero
system("pause");
// Do NOT remove or modify the following statements
cout << endl << "Testing your solution"
<< endl << endl;
test(typeid(value) == typeid(1.));
//
Incorrect data type used for value
test(typeid(valuero) == typeid(1.));
// Incorrect data type used
for valuero
test(typeid(decdig) == typeid(1));
//
Incorrect data type used for decdig
/*
test(fabs(round_off(125.123456789,2) - 125.12 ) <
0.001);
// Incorrect rounding to two decimal
digits
test(fabs(round_off(125.123456789,4) - 125.1235) <
0.00001); //
Incorrect rounding to four decimal digits
test(fabs(round_off(125.987654321,0) - 126.) <
0.001);
// Incorrect rounding to no
decimal digits
test(fabs(round_off(125.987654321, 5) - 125.98765)
< 0.000001);
// Incorrect rounding to five decimal digits
*/
system("pause");
return 0;
}
//************************ Function definition
*************************
// Read the handout carefully for detailed description of the
functions that you have to implement
// Rounds the value received in the first parameter to the
number of digits received in the second parameter
/*You will be have to round numbers very often so you decided to
create your own function round_off()
that will receive the number to be rounded and the number of
decimal digits that the number should be rounded to and will
return
the value rounded to the specified number of decimal digits. You
need to create a program to test the function.
It will ask the user to enter the double precision real number to
be rounded and a whole number indicating the number of decimal
digits.
It will then display the original number with ten digits and the
rounded value (also a double precision real number) with the number
of
digits specified by the user plus 2 more.
This assignment will be completed in two steps:
1) First you will implement the algorithm shown below in which the rounding will be done in main()
2) Once you have this working you will need to modify your
solution so you: Declare the prototype of the function above
main()
Call the function in main() to do the rounding Define the function
below main()
*/
#include
<iostream>
// to use cin and cout
#include
<typeinfo>
// to be able to use operator typeid
#include <bits/stdc++.h>
// Include here the libraries that your program needs to compile
using namespace std;
// Ignore this; it's a little function used for making
tests
inline void _test(const char* expression, const char* file, int
line)
{
cerr << "test(" << expression << ")
failed in file " << file;
cerr << ", line " << line << "."
<< endl << endl;
}
// This goes along with the above function...don't worry about
it
#define test(EXPRESSION) ((EXPRESSION) ? (void)0 :
_test(#EXPRESSION, __FILE__, __LINE__))
// Insert here the prototype of the function here
string findSum( string s1,string s2 ){
string finalStr="";
if (s1.length() > s2.length())
swap(s1, s2);
int l1=s1.length();
int l2=s2.length();
reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
int carry=0;
for( int i=0;i<l1;i++ ){
if( s1[i]=='.' ){
finalStr+='.';
continue;
}
int
sum=(s1[i]-'0')+(s2[i]-'0')+carry;
finalStr+=(sum%10+'0');
carry=sum/10;
}
for( int i=l1;i<l2;i++ ){
if( s2[i]=='.' ){
finalStr+='.';
continue;
}
int sum=(s2[i]-'0')+carry;
finalStr+=(sum%10+'0');
carry=sum/10;
}
if( carry )
finalStr+=(carry+'0');
reverse(finalStr.begin(),finalStr.end());
return finalStr;
}
double round_off( long double num, int digit){
string numstr=to_string(num);//covert to string
int i,j;
int n=numstr.length();
for( i=0;i<n;i++ ){//find the decimal point
if( numstr[i]=='.' )
break;
}
if( i==n ) return num;
int remainingDigits=n-i-1;//digits after decimal
point
for( int ind=n-2;ind>=0 and
remainingDigits>digit ;ind-- ){
if(
numstr[ind]=='.' ) continue;
if(
numstr[ind+1]>'5' and numstr[ind+1]<='9' ){//if digit is x
and >5 and <9, and 10-x to make digit 0 and carry 1
numstr=findSum(numstr, to_string(10-(numstr[ind+1]-'0')) );
}else if( numstr[ind+1]=='5'
){//when next num is 5
if(
(numstr[ind]-'0')%2==1 ){//if current num is odd, change it to
even(same as add 5)
numstr=findSum(numstr, to_string(10-(numstr[ind+1]-'0')) );
}
}
numstr=numstr.substr(0,ind+1);
remainingDigits--;
}
return stod(numstr);//convert string to
double
}
int main()
{
// Declare variable value, valuero that hold double precision real
numbers
double value;
double valuero;
// Declare variable decdig that holds whole numbers
int decdig;
// Prompt the user to "Enter the real number: "
cout << "Enter the real number: ";
// Read from keyboard the value entered by the user and assign it
to side
cin >> value;
// Prompt the user to "Enter number of digits: "
cout << "Enter number of digits: ";
// Read from keyboard the value entered by the user and assign it
to decdig
cin >> decdig;
// Round the real number to the number of decimal digits specified
and assign the result to valuero
valuero=round_off(value,decdig);
// Format the output to display the numbers in fixed format with
ten decimal digits
// printf("%.10lf",valuero);
// Display on the screen, using 23 columns, the message
// "The original number is ", value
cout<<"The original number is ";
printf("%.10lf\n",value);
// Format the output to display the numbers in fixed format with
the number of decimal digits specified plus 2
// printf("%.lf",valuero);
// Display on the screen, using 23 columns, the message
// "The rounded number is ", valuero
cout<<"The rounded number is ";
cout<<valuero<<"00\n";
system("pause");
// Do NOT remove or modify the following statements
cout << endl << "Testing your solution"
<< endl << endl;
test(typeid(value) ==
typeid(1.));
// Incorrect data type used for value
test(typeid(valuero) ==
typeid(1.));
// Incorrect data type used for valuero
test(typeid(decdig) ==
typeid(1));
// Incorrect data type used for decdig
/*
test(fabs(round_off(125.123456789,2) - 125.12 ) <
0.001);
// Incorrect rounding to two decimal digits
test(fabs(round_off(125.123456789,4) - 125.1235) <
0.00001);
// Incorrect rounding to four decimal digits
test(fabs(round_off(125.987654321,0) - 126.) <
0.001);
// Incorrect rounding to no decimal digits
test(fabs(round_off(125.987654321, 5) - 125.98765)
<
0.000001);
// Incorrect rounding to five decimal digits
*/
system("pause");
return 0;
}
//************************ Function definition
*************************
// Read the handout carefully for detailed description of the
functions that you have to implement
// Rounds the value received in the first parameter to the number of digits received in the second parameter