Nested loop Problems

This is a discussion on Nested loop Problems within the C++ Programming forums, part of the General Programming Boards category; How can I use nested loop to solve the problem below? Calculate the valuse of x from the infinite series ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    Nested loop Problems

    How can I use nested loop to solve the problem below?

    Calculate the valuse of x from the infinite series
    x=4- 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + .......
    Print a table that shows the value of x approximated by 1,2,3,etc, terms.

    What I wrote is:

    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    void print(double x,int n)
    {
    cout << (long int) (x) << '.';
    for( ; n>0; n--)
    {
    x = x - (long int) (x);
    x = 10. * x;
    cout << int(x);
    }
    }
    double dec(double x, int n)
    {
    double p;
    p = pow(10.,n);
    return ((long int)(x*p + ((x>0.) ? 0.499999 : -0.499999)))/p;
    }
    int main()
    {
    int n;
    double f, x = 3.141515151515;

    for (cout<< '\n', n=0; n<15; n++)
    {cout << '\n' ;print(x, n); }
    cout << '\n';
    return 0;
    }

    However, my teacher said that it's completely wrong! So, can anyone tells me how to do the problem by using nested loop?
    thank you very much!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Why do you need two loops?

    Code:
    #include <stdio.h>
    
    // x=4- 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + .......
    // re-written as
    // x= 4/1 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + .......
    int main ( ) {
        double  denom = 1;
        double  plusminus = 1.0;
        double  x = 0;
        int     i;
        for ( i = 0 ; i < 10 ; i++ ) {
            x = x + 4.0 / ( denom * plusminus );
            printf( "Term=%d, result=%f, denom=%.0f\n", i, x, denom*plusminus );
            denom = denom + 2.0;
            plusminus = -plusminus;
        }
        return 0;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Thank you very much!!
    Last edited by DramaKing; 10-21-2001 at 12:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Nested loop timer
    By lineb in forum C Programming
    Replies: 1
    Last Post: 03-05-2006, 03:31 PM
  3. Having problems with my for loop
    By kalibaby in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2004, 05:41 PM
  4. Nested Loop
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-07-2002, 12:40 AM
  5. nested loop stuff
    By bob20 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 11:24 AM

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