Equation program

Oct 30, 2019 at 8:26pm
closed account (jhXS8vqX)
Hello I'm begginer in C++, and I need to make a program to calculate this equation. Can anybody help me? A and M reads from the keyboard
Equation: https://postimg.cc/WFbsFhF3

I try this code:
1
2
3
4
5
6
7
8
9
10
11
12
  int M=1;
  int A;
  cin>>A;
  cin>>M;
  float sum;
  for(int i = 1; i<=M; k++){
        
    k = k*i;

    float A1 = pow(A, k);

    sum =  A1 / k;}
Oct 30, 2019 at 8:45pm
The loop is k that goes from 0 to M. Not sure where you got "i" from.

You need a factorial function. The stuff in the loop is:
 
pow(A, k) / factorial(k)


As you're collecting a sum, you need to add up the vaiues in the loop.
Oct 30, 2019 at 9:10pm
closed account (jhXS8vqX)
Thank you . Can you post the code, please?
Oct 31, 2019 at 3:44pm
You need a factorial function. The stuff in the loop is:
pow(A, k) / factorial(k)


That will overflow the max value of float (or double) sooner than necessary. It's much more accurate and efficient to compute each term from the previous one:
T0 = 1, Ti+1 = Ti*(A/k)

You just have to be sure to start with a sum that's 1 and then loop for k=1 to M instead of k=0 to M.
Oct 31, 2019 at 6:08pm
Can you post the code, please?

You mean "Can you do my homework for me, please?"

Right?
Oct 31, 2019 at 6:38pm
if k is small you can just do a simple loop where you grow factorial running every loop iteration and same for the power, each loop increase a running power, and divide it and add. If the numbers can go out of range (and they will very quickly) you need to do something smarter. say K is 4. 1!,2!,3! are normal and then 4! is actually 3! ( 4^4/3!*4 = 4^3/3! right?) and so on. note that for 4, you can also divide top and bottom by 2, and so on... so some sort of ongoing fraction reduction strategy will let you run it out farther. If you need more than that, you will need to study it deeper.
Topic archived. No new replies allowed.