Results 1 to 5 of 5

Thread: #%*& factorial of "e"

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    New England USA
    Posts
    13

    Thumbs down

    Okay this is due tomorrow, I've spent more time on it than any of my other assignments in C++ and VB classes combined and I'm at my wit's end.

    Assignment:

    Write a program that estimates the value of the mathematical constant "e" by using the formula:

    e = 1 + 1/1! + 1/2! + 1/3! + ...

    I'm using VC++ 6.0 that came with the book, it's a demo student addition with no help files.

    I have notes from class, and don't understand them which is a really bad thing if I'm going to ever actually BE a coder some day. But the notes aren't helping - here's what they say:

    #include <iostream.h>
    #include <iomanip.h>

    float EEE = 1, Factorial = 1;

    for (int i = 1; i < 6; i++);

    EEE += 1/(Factorial *= i);

    cout<<iosflags(ios::fixed | ios:showpoint)<<setprecision (10);
    cout<<"e = "<<EEE<<endl;

    Now obviously there are problems with my notes which I copied exactly from the chalkboard. There's no main(), and if any of the above are supposed to be functions there's no mention of that either.

    I've tried moving things around, placing the lines in and out of a main() routine, and get error after error after error.
    I tried adding #include <cmath.h>, said it was an invalid library. I tried #include <cmath> and didn't get that error but got a whole bunch of others.

    iosflags is not declared. ios::fixed is illegal. I don't even know what iosflags IS, so obviously I don't know how to declare it. The teacher isn't much help, and as I said there's no help file in the program, and "iosflags" doesn't appear anywhere in the book's index or glossary.

    If someone can please help me - even if it means someone else writing the program for me - BUT explaining each line so I can understand it! I'd really appreciate it.

    Thanks,

    the painfully frustrated Colora

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    you don't actually need any maths headers for this, what you are trying to do is estimate the value of e using something called a taylor series.

    the Idea is that

    e = 1 + (1/1!) + (1/2!) + (1/3!) + (1/4!) + ...
    the more terms you add the closer you will get to the actual value of e. (e is a number, it's about 2.718)

    the explamation marcks mean calculate the factorial of a number, the factorial of a number is 1*2*3*4*5*6*... all the way up to the number you want.

    I'd do something like this

    Code:
    #include <iostream.h>
    
    
    
    
    
    
    //This function Calculates the Value of e recursivley
    double Estimate_e(int NoOfTerms, int* LastTermFactorial)
    {
    
    double retval;  //this is the value we return
    
        //if NoOfTerms is 0 we want the first estimate
        if (NoOfTerms == 0)
        {
            *LastTermFactorial = 1;  //0! = 1
            return 1;               //THe First Estimate is i
        }
        else
        {
    
            //If the number of terms is not 0 we calculate the 
            //estimate based on the last estimate
    
            retval = Estimate_e(NoOfTerms - 1, LastTermFactorial);
            //retval is now the last estimate of e
            //LastTermFactorial points to (NoOfTerms - 1)!
    
    		//Multiply the value LastTermFactorial points to by NoOfTerms
    		//So Now it's NoOfTerms!
            *LastTermFactorial *= NoOfTerms;
    
    		return retval + (1/(double)(*LastTermFactorial));
    
    
    	}
    }
    
    //Overload the Function So We Can Acsess it without bothering about the factorial Pointer
    double Estimate_e(int NoOfTerms)
    {
        int temp;   //Temporary variable to hold factorials
        return Estimate_e(NoOfTerms, &temp);
    }
    
    void main()
    {
    
    	int NoOfTerms;
    
    	cout << "Enter the number of terms you want to estimate to" << endl;
    	cin >> NoOfTerms;
    
    	cout << "e = " << Estimate_e(NoOfTerms);
    
    	cin >> NoOfTerms;
    }
    If you havn't done pointers or overloading yet then Post again and I'll do it without them, but this is the best way of doing it.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    New England USA
    Posts
    13
    Well Sam, I don't know what happened but I found myself suddenly alone in our ICQ chat, and you'd disconnected. As I mentioned (for anyone else reading his reply), I haven't learned pointers yet. I tweaked a bit and used the code he offered:

    //E2_38b.cpp exercise 2.38b
    //estimate value of e

    #include <iostream.h>
    #include <iomanip.h>

    double Estimate_e(int NoOfTerms)
    {
    int i; //counter for number of terms;
    int iFactorial = 1; //the factorial of i
    double e = 1; //our estimate of e

    for (i = 1; i <= NoOfTerms; i++)
    {
    iFactorial *= i; //multiply iFactorial by i

    e += ( 1 / (double)iFactorial);
    }

    return e;
    }

    Which is good, except now instead of compiler errors I'm getting link errors! The last thing Sam said before he disappeared from ICQ was to close the workgroup and create a brand new one. I did, and got the same link errors. Then I opened up TurboC++ Lite from Borland, and tried it there (I'm using VC++ 6.0 Student Demo usually). This time I got a different link error:

    *Linker Error: Undefined symbol _main in module Turbo_C\COS.ASM

    I have no idea what it means, but repeated tries results in the same thing. Anyone have advice?

    Thanks to Sam for showing me where to put everything by the way - it actually makes sense! Maybe you should come to the college and teach the teacher how to teach, eh?

    Colora

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Code:
    #include <iostream.h>
    
    
    double Estimate_e(int NoOfTerms)
    {
    
        int i;  //counter for number of terms;
        int iFactorial = 1;   //the factorial of i
        double e = 1;  //our estimate of e
    
        for (i = 1; i <= NoOfTerms; i++)
        {
    
            iFactorial *= i;  //multiply iFactorial by i 
    
            e += ( 1 / (double)iFactorial);
    
        }
    
        return e;
    }
    
    void main()
    {
    
        int NoOfTerms;
    
        cout << "Enter the number of terms you want to estimate to" << endl;
        cin >> NoOfTerms;
    
        cout << "e = " << Estimate_e(NoOfTerms);
    
        cin >> NoOfTerms;
    }
    [Edited by Sam Finch on 10-29-2000 at 01:27 PM]

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Colora - all C/C++ programs (console ones anyway) begin with a function called "main". This is definite, and cannot be changed without a different OS (windows programs use "WinMain" to allow extra features). With C, all function names have a "_" prepended, which is what the linker looks for. BTW, the proper prototype for "main" is as follows:
    Code:
    int main(int argc, char **argv, char **envv);
    The int can usually be replaced with any other type, including void. All the arguments are optional end-backwards (you can't have "main(argc, envv)").
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width