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/4+......+1/k
Use C++ programming Language to write a program that computes the previous series from left to right, then from right to left, displays the result in both cases and compares them (print the difference between the two sum) .
Your program should :
.
1. Read the value of k from the user and give an error message for invalid inputs (k should be %26gt;0).
2. Test and run your program with k=10000, 20000, 30000 and bigger numbers.
3. Print the value of k for which the differe k
Please answer this quation as C++ program?
I write a problem and find that it is true that the two condition is different.
#include%26lt;iostream%26gt;
using namespace std;
int main()
{
for(long long k;cin%26gt;%26gt;k;){
if(k%26lt;=0) cout%26lt;%26lt;"invalid inputs\n";
long double s1=0,s2=0;
for(int i=1;i%26lt;=k;i++)
s1+=1.0/i;
for(int i=k;i%26gt;=1;i--)
s2+=1.2/i;
cout%26lt;%26lt;"s1="%26lt;%26lt;s1%26lt;%26lt;"\ns2="%26lt;%26lt;s2%26lt;%26lt;endl;
}
}
Reply:I have algorithm for this. It is as follows:
step-1: Read value(s) from user.
step-2:Put the check on the input that the inputted number should be greater than zero. Also put a condition that the program didnot break after the input is wrong, the program should display message that enter new input, and this continue until correct value is inputted.
step-3:Now make a harmonic series i.e make a function for this.
step-4:Calculate the value(s) and display them.
step-5:Also print value(s) of input variable for which it differ
Reply:Thats easy!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment