63 066
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #14048 in C++ for shalenee

Question #14048
(Demonstrating cancellation errors) A cancellation error
occurs when you are manipulating a very large number with a
very small number. The large number may cancel out the
smaller number. For example, the result of 100000000.0 +
0.000000001 is equal to 100000000.0. To avoid cancellation
errors and obtain more accurate results, carefully select
the order of computation. For example, in computing the
following series, you will obtain more accurate results by
computing from right to left rather than from left to
right:
1+1/2+1/3+...+1/n
Write a program that compares the results of the
summation of the preceding series, computing from left to
right and from right to left with n = 7000.
Expert's answer
#include <iostream>
using namespace std;

int main()
{

double leftToRight = 0, rightToLeft = 0;
int n = 7000;
cout
<< "SUM( 1 / i), i in (1 .. 7000)" << endl;
for(int i = 1, j
= n; i <= 7000; ++i, --j)
{
leftToRight += 1.0 / i;

rightToLeft += 1.0 / j;
}
cout << "Left to right: "
<< leftToRight << endl;
cout << "Right to left: "
<< rightToLeft << endl;
getchar();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions