Question

In: Computer Science

/*You will be have to round numbers very often so you decided to create your own...

/*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

Solutions

Expert Solution

/*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


Related Solutions

you will create a proposal for your very own technical writing project. You may propose nearly any...
you will create a proposal for your very own technical writing project. You may propose nearly any type of technical writing project you like. It could involve something real (e.g. you could create a website for an organization you're a member of), some fictional audience (e.g. you could write a user manual for board game you create or a website for a fictional company), or even something fun . help need some type of clue
A very prominent virologist, you decided to play in your laboratory growing different viruses. You have...
A very prominent virologist, you decided to play in your laboratory growing different viruses. You have obtained one enveloped, icohahedral positive strand RNA virus. The replication of this virus is insensitive to any nucleic acid synthesis inhibitor you can think of. Your only problem is that the susceptible cells are human lung epithelial cells, which you do not have. During a recent meeting of the American Society of the Frustrated Virologists, you heard a paper describing how monkey kidney cells...
You operate your own small building company and have decided to bid on a government contract...
You operate your own small building company and have decided to bid on a government contract to build a pedestrian walkway in a national park during the coming winter. The walkway is to be of standard government design and should involve no unexpected costs. Your present capacity utilization rate is moderate and allows sufficient scope to understand this contract, if you win it. You calculate your incremental costs to be $268,000 and your fully allocated costs to be $440,000. Your...
You operate your own small building company and have decided to bid on a government contract...
You operate your own small building company and have decided to bid on a government contract to build a pedestrian walkway in a national park during the coming winter. The walkway is to be of standard government design and should involve no unexpected costs. Your present capacity utilization rate is moderate and allows sufficient scope to understand this contract, if you win it. You calculate your incremental costs to be $268,000 and your fully allocated costs to be $440,000. Your...
Problem 2: You operate your own small building company and have decided to bid on a...
Problem 2: You operate your own small building company and have decided to bid on a government contract to build a pedestrian walkway in a national park during the coming winter. The walkway is to be of standard government design and should involve no unexpected costs. Your present capacity utilization rate is moderate and allows sufficient scope to understand this contract, if you win it. You calculate your incremental costs to be $268,000 and your fully allocated costs to be...
You operate your own small building company and have decided to bid on a government contract...
You operate your own small building company and have decided to bid on a government contract to build a pedestrian walkway in a national park during the coming winter. The walkway is to be of standard government design and should involve no unexpected costs. Your present capacity utilization rate is moderate and allows sufficient scope to understand this contract, if you win it. You calculate your incremental costs to be $268,000 and your fully allocated costs to be $440,000. Your...
Create an example (make up your own numbers) for a virtual open economy with import as...
Create an example (make up your own numbers) for a virtual open economy with import as a function of total income. Find the equilibrium output level and the open economy multiplier.
Create an example (make up your own numbers) for a virtual open economy with import as...
Create an example (make up your own numbers) for a virtual open economy with import as a function of total income. Find the equilibrium output level and the open economy multiplier. ı sent this question before but I couldn't read most of the answer.
Business Case situation: You have decided to explore the idea ofopening your own pet food...
Business Case situation: You have decided to explore the idea of opening your own pet food and supplies retail store. You have 10 years of retail management experience, a college diploma in business and a strong love for animals. You currently live in the Toronto area – but are willing to move. You are leaning towards serving the higher end of the market – but are not sure. You want to provide a good range of products, maybe focused more...
Business Case situation: You have decided to explore the idea of opening your own pet food...
Business Case situation: You have decided to explore the idea of opening your own pet food and supplies retail store. You have 10 years of retail management experience, a college diploma in business and a strong love for animals. You currently live in the Toronto area – but are willing to move. You are leaning towards serving the higher end of the market – but are not sure. You want to provide a good range of products, maybe focused more...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT