PLEASE HELP!!!

Pages: 12
I'm going to be up front, the following question is in fact a homework question. Here's the deal, though, the mathematics alone in this question are over my head and I just know that I stand zero chance of being able to figure this thing out on my own in the short amount of time I have.

Bottom line, I'll make a nice donation to this website if someone offers me a working solution.

For those still reading, the question is:

Write a program, that uses the following algorithms, to compute, and later output to the screen, the value of the exponential function f(x) = ex. To do this, you will use the power series representation of ex, where ex is the sum, from n = 0 to ∞, of xn / n! and n! = n • (n - 1) • (n - 2) • … • 2 • 1. The value x, is entered, via the keyboard, by the user.

Of course, any more traditional nudge in the right direction help is also appreciated. Although, I'm not convinced that will be enough.

Thanks, all!
Last edited on
I'm following this, as I have been looking for an answer to the exact same question for the last half an hour. I got someone to vaguely explain the math function to me, but even then, I don't know how to create a loop to output the answer without it becoming an endless loop! Because n = zero to INFINITY.... if you figure out the answer please let me know! I will be eternally grateful!!
closed account (D80DSL3A)
You won't loop until n = infinity. There's a maximum value. n! will overflow an integer value when n gets too high.

Calculating n! is very similar to camk16's last problem of summing a series, except instead of += you *=.
@fun2code

I appreciate your reply, fun2code, but as suspected, a nudge is proving thoroughly insufficient in this case (my case--hope it helped you, krisnorine). I still have no idea what to do.

Still, thanks for trying.
Last edited on
I'm going to substitute a sufficiently high value for "infinity" in order for the loop to effectively run, and see what happens...

Essentially we are trying to find: e^x, which is the sum series of x^n / n! where n = 0 to "inifinity".

An example of n! where n=5 is:

5*4*3*2*1 = 120

We did a sum series in the previous question of our assignment (I do believe we are working on the same assignment right now!) so I am going to try and use that same logic with this new equation, somehow.

To be honest, I don't entirely understand it either, and I don't imagine that my explanation was very clear.

Sorry I can't be much more help to you - perhaps we need to arrange a study group for the next assignment!!

Good luck!! :)

Try this one. I think it could be acceptable for homework. Function fi is to calculate xn / n!. Function f returns the sum of fi.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
double fi(int n, double x)
{
 int	i;
 double r=1;
 if (n==0)
  return 1;
 else {
  for (i=0;i<n;i++)
   r = r * x / (i+1);
  return r;
 }
}

double f(double x)
{
 double y = 0;
 double u;

 int	i = 0;
 do {		
  u = fi(i,x);
  if (u==0)
   break;
  else {
   y += u;
   i++;
  }
 } while (1);

 return y;
}
Last edited on
> ex is the sum, from n = 0 to ∞, of xn / n!

So ex is 1 + x + x2/2! + x3/3! + .... + xn/n! + ....

While computing each term in this series in a loop ( n = 1, 2, 3, ... ) and summing them up,
for some value of n, the term xn/n! would become too small to be representable, would be equal to zero.
We can stop at that point and print out the sum so far as the result.

Calculatiing the next term is easy: note that termn == termn-1 * x / n
For instance: x5/5! == x4/4! * x / 5
I tried doing this just now and it took me forever to figure out why I was getting the wrong answer. Do not have your factorial function return an int. Have it return a long or a long long instead. Same thing if you write your own power function.
@gf109 Uhh... did you ever get the right answer?
Last edited on
Yes. The problem with my code was that 13! (6,227,020,800) is larger than the maximum value for an int (2,147,483,647).
@gf109 ok...well... are you just taunting me or is there a different reason why you're telling me you have the solution but not showing it to me?
Last edited on
I just read JLBorges' post in its entirety and figured out that there was no need to use a power function or factorial function.

Calculatiing the next term is easy: note that termn == termn-1 * x / n
For instance: x5/5! == x4/4! * x / 5


That and the fact that I was coding in cpp.sh. It crashed when it entered an infinite loop during compilation for some reason. No idea why, it worked earlier and I didn't change the code, just wanted to run it again so that I could input a different number for X. So I'm too lazy to rewrite the code.

And JLBorges just gave you a much superior answer below.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    const double test[] = { -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0 } ;

    for( double x : test )
    {
        double exp_x = 1 ;

        double term = 1 ;
        double previous_term = 1 ;

        for( int n = 1 ; term != 0 ; ++n )
        {
            term = previous_term * x / n ;
            exp_x += term ;
            previous_term = term ;
        }

        std::cout << std::fixed << std::setprecision(8)
                  << std::setw(20) << exp_x << std::setw(20) << std::exp(x) << '\n' ;
    }
}

http://rextester.com/BAXO15552
That is why I calculate xn / n! with (x/1)(x/2)...(x/n).
Ok, I threw that into VS 2010 but it's not running for me.

I'm having a problem with this line:

for( double x : test )

There's squiggly red lines under the ':' a the ')' and the error is saying I'm missing a ';' before the ':' and the ')'.

I've made these changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    const double test[] = { -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0 } ;

    for( double x : test )
    {
        double exp_x = 1 ;

        double term = 1 ;
        double previous_term = 1 ;

        for( int n = 1 ; term != 0 ; ++n )
        {
            term = previous_term * x / n ;
            exp_x += term ;
            previous_term = term ;
        }

        cout << fixed << setprecision(8) << setw(20) << exp_x << setw(20) << exp(x) << endl;
    }
}


Because I wasn't taught to use std::, it's foreign to me, and I haven't much of an idea what it means or does.

Regardless, same problem, same line, same error message.
Last edited on
> Ok, I threw that into VS 2010 but it's not running for me.

Do you expect to learn anything useful by merely copying and pasting code?

Once you have understood the code, for legacy C++, write a classical for-loop instead of the range-based loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    const double test[] = { -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0 } ;

    // for( double x : test )
    //{
    //     // ...
    //}

    const int N = sizeof(test) / sizeof( test[0] ) ;
    for( int i = 0 ; i < N ; ++i )
    {
           double x = test[i] ;

           // ...
    }



> Because I wasn't taught to use std::, it's foreign to me, and I haven't much of an idea what it means or does.

See: http://www.cplusplus.com/forum/beginner/142171/#msg750694
> Do you expect to learn anything useful by merely copying and pasting code?

Of course those aren't my expectations.

The fact is, this question is way outside of my skill set and I'm finding that I wont be able to develop the proper skill set in a timely manner.

Plus, copy & pasting is not merely all I intend to do.

I believe that presented with the solution, I could best learn by dissecting it.

Besides, I can't see any argument for pouring through forums and reading article after article as being a better strategy.
Last edited on
> I believe that presented with the solution, I could best learn by dissecting it.

Ok, try this. (I don't have Visual Studio 2010, but this should compile cleanly with it):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include <iomanip>

int main()
{
    const double test[] = { -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0 } ;
    const int N = sizeof(test) / sizeof( test[0] ) ;

    for( int i = 0 ; i < N ; ++i )
    {
        double x = test[i] ;

        double exp_x = 1 ;

        double term = 1 ;
        double previous_term = 1 ;

        for( int n = 1 ; term != 0 ; ++n )
        {
            term = previous_term * x / n ;
            exp_x += term ;
            previous_term = term ;
        }

        std::cout << std::fixed << std::setprecision(8)
                  << std::setw(20) << exp_x << std::setw(20) << std::exp(x) << '\n' ;
    }
}
We haven't covered arrays yet, so I shouldn't use them. However, I have one more class before the assignment is due, I think we may be starting on them then.

This code doesn't allow the user to enter the numbers in the array via the keyboard anyways (if that's even possible).

Where do these numbers: -3.2, -0.5, 1.0, 2.3, 4.6, 6.2, 12.0, even come from? Are they just randomly chosen numbers for the purpose of your example or..?
Last edited on
What I found as an undergrad maths student.

I chose a scientific computing with java module in my second year and omg my grade sucked. The time I spent searching for any similar code online would have been better spent grinding it out. My grade may not have sucked so bad if I had followed this logic. Wait until you see the solution code lol, a few lines.

I also chose a masters level module c++ for finance in my third year (crazy ass I know, but programming is a skill I think is essential these days, to have a clue is useful not just for CV) and didn't do so bad so far. Just the anxiety of the result made me doubt whether I was near correct which wastes valuable time.

Some people on here are very helpful but like the sticky says they will likely not do homework for you. If your lecturer is giving you Taylor Series things then you must have covered it at some point, anyway it's not that hard once you know the algorithm, more memory and reproducing at first.

You usually get marks for clarity of code, developer documentation, comments, accuracy of output, etc etc. If they can see what you were trying they will not penalise, if you cannot understand your code then you risk 0.

So grab your books, learn what you don't get study that, ask questions, grind it out. Use the F10 to step through code to see what is happening.

Good luck.

Last edited on
Pages: 12