Newbie here...needs help

This is a discussion on Newbie here...needs help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, Im new at this C++ programming. I have a problem with the Homework assignment, can anyone shed some ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Newbie here...needs help

    Hey guys, Im new at this C++ programming. I have a problem with the Homework assignment, can anyone shed some light on this
    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 have to use a while structure
    thank you so much in advance fellas

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What bit are you having trouble with? post the code you've written so far, and someone will guide you.

    Please read this before you post the code though
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    I'm not even sure how to even start with this program. The question in the first thread is the question that I was assigned. I do not have a clue where to start.
    i'm having a real hard time with c++
    I wish someone lived down here on the Mississippi Gulf coast who could tutor me.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's plenty of tutorials around on the web, including one on this site. Also, get yourself a good book (do a forum search to find recommendations).

    Once you have written some code, people here will be more than willing to help you with it.

    Here's a starter
    Code:
    #include <iostream>
    using namespace std; // Read this
    
    int main()
    {
      int myValue;
      myValue = 11;
      cout <<myValue <<endl;
    }
    Don't worry if it doesn't all make sense yet, just stick with it, and it will get better
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Smile

    Thank you for your help

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Consider the problem you are working on. Try to break it down a bit. You know what the overall task is (to compute a series approximation to e), but try to break it into smaller more obtainable tasks.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    If I was to make suggestion maybe you should consider making a function to calculate 1!, 2!, 3!, 4! etc.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    17
    I think I have the answer but it maybe wrong, your mathematical constant had to be e=1+1/1+!2/1+!3/1+!4 thats what I came up with, since I continued getting errors when I attempted your problem without the correction. I would post the code, but please show effort.

    Another thing my formula is correct, you would get errors division by 0 since 1!+2, therefore you would use my formula e=1+1/1+!2... This is all the help I'm going to give. The rest should be explanitory.
    Last edited by Eric08; 07-06-2003 at 05:19 AM.

  9. #9
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    No, his original formula was correct.

    e=1+ 1/(1!)+1/(2!)+1/(3!)+1/(4!)+ ... +1/(infinity!)

    For both the loop in your program that calculates e, and the loop in your factorial function, I would suggest you look into something called a "for loop" (Sorry if you already knew that... you seem lost)
    Away.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Here is guys I finally got it.
    I do not know how do the codes, that Hammer is refferring to.
    Thanks for trying to help.

    code:

    #include <iostream>
    using namespace std;
    int num;
    int main()
    {
    cout<<"Please enter a positive number: ";
    cin>>num;

    if(num<0)
    cout<<"Must be a nongegative number: "<<endl;

    else
    {
    double e=1;
    for (int i=1;i<=10;i++)
    {
    double fact=1;
    for (num=1;num<=i;num++)
    fact=fact*num;
    e=e+(1/fact);
    }
    cout<<"The value of e is: "<<e<<endl;

    }

    return 0;
    }

    end code:

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    2
    for (num=1;num<=i;num++)

    This defeats the purpose of getting input for 'num'.

    Anyway I don't even understand the problem. What in the hell is that 1!+1 stuff anyway? What exactly is it that you are calculating?
    Code:
    #ifndef IOSTREAM
    #include <iostream>
    #endif
    
    using namespace std;
    
    int main()
    {
      int num = -1;
    
      while(num<0)
     {
       cout<<"Please enter a positive number: " << endl;
      
       cin>>num;
        
          if(num<0 || !cin.good()) // edit
       {
        cout << "number must be non-negative." << endl;
       }
    
     }
     
     // no clue here...
     
     double e=1;
     
       for (int i=1;i<=10;i++)
     {
       double fact=1;
      
          for (;num<=i;num++)
         {
            fact=fact*num;
            
            e=e+(1/fact);
         }
     // ... //
    }
    
      cout<<"The value of e is: "<<e<<endl;
    
     return e;
    }
    Last edited by mercury7; 07-06-2003 at 10:51 PM.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think the idea here may be to create a program that allows you to run a certain number of iterations to get an approximation of the value of e. You would have to choose how many iterations to run. The more you run, the slower it is, but the more accurate it is. I would create a function first that finds the factorial of a given number. That shouldn't be too hard. Then I would create a loop that ascends through positive integers, running the formula with each factorial. Here's an example:

    Code:
    double factorial(int num)
    {
        double count;
        count =1;
    
        for (int i=1;i<=num;i++)
        {
            count*=i;
        }
         return count;
    }
    
    int main()
    {
        int nIterations=20;
        double tempE=1;
    
        for (int i=1;i<=nIterations;i++)
        {
              tempE+=1/factorial(i);
        }
    
        cout << tempE << endl;
    
        return 0;
    }
    I'm not sure about the math really, but that should give you an approximation that's a bit less than the actual value. There may be some issues with factorial sizes, they can get quite big, but I think it's all good.

    An improvement could be to keep going through iterations and displaying the result until the user presses a key, that would allow them to stop when accurate enough.

    I hope this helps.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Oops, just reread the thread and realized the problem was already solved. Good work Diceman. Something else we could have all done better was to save time with the factorials. Why calculate 5! from the beginning if we just did 4! ? Just keep the factorial variable from one iteration to the next, and multiply by the next value. Eg:

    Code:
    double factorial;
    
    for (...)
    {
    factorial*=i;
    e+=1/factorial;
    }
    This would save a lot of time, allowing for more accuracy.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 02:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 08:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21