简体   繁体   中英

Program to display a sum in C++?

I was given a task to write a program that displays:

在此处输入图片说明

I coded this:

#include<iostream.h>
#include<conio.h>
void main()
{
   clrscr();
   int a, n = 1, f = 1;
   float s = 0;
   cin >> a;
   while(n <= a)
   { 
        f = f * n;
        s += 1 / (float)f;
        n = n + 1;
   }

   cout << s;
   getch();
}

So this displays -

s = 1 + 1/2! + 1/3! + 1/4! .... + 1/a!, including odd and even factorials.

For the past two hours I am trying to figure out how can I modify this code so that it displays the desired result. But I couldn't figure it out yet.

Question:

What changes should I make to my code?

You have almost everything you need in place (assuming you don't want to make design changes based on the issues brought up in the comments).

All you need to change is what you multiply f by in each step. To build up n! you are multiplying by n in each step. To build up (2n)! you would multiply by 2*n*(2*n-1)

Edit: Your second theory about what the instructor wants would need only slightly more of a change. Your inner loop could be replaced by

   while(n < a)
   { 
        f = f * n * (n+1);
        s += 1 / f;
        n = n + 2;
   }

Edit2: To run your program I made several changes for I/O things you did that don't work in my copy of GCC. Hopefully those won't distract from the main point of the following code. I also added a second, more complicated and more accurate method of computing the answer to see how much was lost in floating point rounding.

So this code computes the answer twice, once by the method I suggested you change your code to and once by a more accurate method (using double instead of float and adding the numbers in the more accurate sequence via a recursive function). Then it display your answer and the difference between the two answers.

Running that shows the version I suggested gets all the displayed digits correct and is only wrong for the values of a I tried by tiny amounts that would need more display precision to notice:

#include<iostream>
using namespace std;

double fac_sum(int n, int a, double f)
{
  if ( n > a )
    return 0;
  f *= n * (n-1);
  return fac_sum(n+2, a, f) + 1 / f;
}

int main()
{

   int a, n = 1;
   float f = 1;
   float s = 0;
   cin >> a;
   while(n < a)
   { 
        f = f * n * (n+1);
        s += 1 / f;
        n = n + 2;
   }

   cout << s;
   cout << " approx error was " << fac_sum( 2, a, 1.0)-s;
   return 0;
}

For 8 that displays 0.54308 approx error was -3.23568e-08 I hope you understand the e-08 notation meaning the error is in the 8'th digit to the right of the .

Edit3: I changed f to float in this post because I had copied/tested thinking f was float , so parts of my answer didn't make sense when f was int

12! is the largest value that fits in an 32 bit integer. You should use double for all the numbers. For even factorials, starting with f = 1 (0!), f = f * (n-1) * n, where n = 2, 4, 6, 8, ... .

You need to accumulate the sum while checking the counter n and only calculate the even factorials:

int n;
double sum = 1;
cin >> n;
for(int i = 2; i < n; ++i{ 
   if(i % 2 == 0) sum += 1 / factorial(i);
}  

In your code:

 while(n <= a)
 { 
    f = f * n;
    // checks if n is even; 
    // n even if the remainder of the division by 2 is zero  
    if(n % 2 == 0){
        s += 1 / (float)f;
    }
    n = n + 1; 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM