Thread: Please help!!!

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Unhappy Please help!!!

    i have a university assignment to do in C++ and i dunno what code to write can anyone help me?

    this is my assignment:

    develop a C program to approximately calculate the value of e(to the power x) applying this followong infinite series:

    e(to power x) = (sum of sign, with infinity above it, and (n=o below) (x(to power n/n factorial)

    in the range n=0,1...,10, and x=0.8

    what is this? please help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How about starting with a function to calculate factorial(n)

    Since math.h already has a pow() function, the rest is just a loop

    > i have a university assignment to do in C++
    > develop a C program
    Make your mind up - which is it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    3

    Exclamation argh

    Ok ur talkin to someone whos never done programming before. This is a random compulsory assignment. Iv been told we are programming in c++, and the bit i wrote was copied word for word from the sheet. so i assume they mean write a c++ program for ....

    so i need to start form the beginning basically...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    No basic instruction in C++ at all - like how to even compile the simplest of "hello world" programs?
    Do you have a compiler set up which you can use?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    3

    Exclamation

    I know the basic stuff u start with, like
    #include <stdio.h>
    main()
    {
    but they dont tell me about how to write the code for this particular equation...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Short of handing your homework to you on a plate (it's not likely to happen), this is as far as I can go showing you the way.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main() {
        double x = 0.8;
        double sum = 0.0;
        for ( int n = 0 ; n <= 10 ; n++ ) {
            double pow_term = pow(x,n);
            // double fact_term = .....
            cout << "n=" << n << " pow(x,n)= " << pow_term << endl;
            sum = sum + pow_term /* / fact_term */;
        }
        cout << "Sum = " << sum << endl;
        return 0;
    }
    All you need do is uncomment the fact_term bits and work out how to calculate factorial(n)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed