Want to print partial sum but doesn't work

Discussion in 'C++' started by Archie, Jun 30, 2010.

  1. Archie

    Archie Guest

    I want my program to print a partial sum of fractions: 1 + 1/2 + 2/3
    + 3/4 + ... depending on the number of terms I enter.

    So I wrote this:


    #include <iostream>
    using namespace std;

    int main()
    {

    int terms;
    int terms_count = 1;
    float n;


    cout << "Enter the number of terms:\n";

    cin >> terms;


    while ( terms_count <= terms )
    {
    n = 1;
    int x = 1;
    int y = 2;
    n = n + ( x/y );
    x++;
    y++;

    }

    cout << "The partial sum is " << n << endl;

    return 0;
    }

    But this doesn't work. You enter the number of terms and nothing
    happens. So I must have an infinite loop of some kind.

    I'm not sure what's wrong here. I've initialized the variables in
    different places. Any comments would be welcome.

    Thanks,

    Archie
     
    Archie, Jun 30, 2010
    #1
    1. Advertising

  2. Archie

    Öö Tiib Guest

    On 30 juuni, 04:14, Archie <> wrote:
    > I want my program to print a partial sum of fractions: 1 +  1/2 + 2/3
    > + 3/4 + ... depending on the number of terms I enter.
    >
    > So I wrote this:
    >
    >    #include <iostream>
    >    using namespace std;
    >
    >    int main()
    >    {
    >
    >       int terms;
    >       int terms_count = 1;
    >       float n;
    >
    >       cout << "Enter the number of terms:\n";
    >
    >       cin >> terms;
    >
    >       while ( terms_count <= terms )
    >       {


    insert here such code:

    cout << "The terms_count is " << terms_count
    << "and that is never bigger than " << terms << endl;

    >           n = 1;
    >           int x = 1;
    >           int y = 2;
    >           n = n + ( x/y );
    >           x++;
    >           y++;
    >
    >       }
    >
    >       cout << "The partial sum is " << n << endl;
    >
    >    return 0;
    >    }
    >
    > But this doesn't work. You enter the number of terms and nothing
    > happens. So I must have an infinite loop of some kind.
    >
    > I'm not sure what's wrong here. I've initialized the variables in
    > different places.  Any comments would be welcome.


    See above, then run it and then explain to us why so.
     
    Öö Tiib, Jun 30, 2010
    #2
    1. Advertising

  3. Archie

    Archie Guest

    On Jun 29, 9:23 pm, Öö Tiib <> wrote:
    > On 30 juuni, 04:14, Archie <> wrote:
    >
    >
    >
    >
    >
    > > I want my program to print a partial sum of fractions: 1 +  1/2 + 2/3
    > > + 3/4 + ... depending on the number of terms I enter.

    >
    > > So I wrote this:

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

    >
    > >    int main()
    > >    {

    >
    > >       int terms;
    > >       int terms_count = 1;
    > >       float n;

    >
    > >       cout << "Enter the number of terms:\n";

    >
    > >       cin >> terms;

    >
    > >       while ( terms_count <= terms )
    > >       {

    >
    > insert here such code:
    >
    >     cout << "The terms_count is " << terms_count
    >          << "and that is never bigger than " << terms << endl;
    >
    >
    >
    >
    >
    > >           n = 1;
    > >           int x = 1;
    > >           int y = 2;
    > >           n = n + ( x/y );
    > >           x++;
    > >           y++;

    >
    > >       }

    >
    > >       cout << "The partial sum is " << n << endl;

    >
    > >    return 0;
    > >    }

    >
    > > But this doesn't work. You enter the number of terms and nothing
    > > happens. So I must have an infinite loop of some kind.

    >
    > > I'm not sure what's wrong here. I've initialized the variables in
    > > different places.  Any comments would be welcome.

    >
    > See above, then run it and then explain to us why so.- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    Oops, I forgot a line of code. It should be:

    #include <iostream>
    using namespace std;


    int main()
    {


    int terms;
    int terms_count = 1;
    float n;


    cout << "Enter the number of terms:\n";


    cin >> terms;


    while ( terms_count <= terms )
    {
    n = 1;
    int x = 1;
    int y = 2;
    n = n + ( x/y );
    x++;
    y++;
    terms_count++;

    }


    cout << "The partial sum is " << n << endl;


    return 0;
    }
     
    Archie, Jun 30, 2010
    #3
  4. Archie

    Archie Guest

    On Jun 29, 9:26 pm, Archie <> wrote:
    > On Jun 29, 9:23 pm, Öö Tiib <> wrote:
    >
    >
    >
    >
    >
    > > On 30 juuni, 04:14, Archie <> wrote:

    >
    > > > I want my program to print a partial sum of fractions: 1 +  1/2 + 2/3
    > > > + 3/4 + ... depending on the number of terms I enter.

    >
    > > > So I wrote this:

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

    >
    > > >    int main()
    > > >    {

    >
    > > >       int terms;
    > > >       int terms_count = 1;
    > > >       float n;

    >
    > > >       cout << "Enter the number of terms:\n";

    >
    > > >       cin >> terms;

    >
    > > >       while ( terms_count <= terms )
    > > >       {

    >
    > > insert here such code:

    >
    > >     cout << "The terms_count is " << terms_count
    > >          << "and that is never bigger than " << terms << endl;

    >
    > > >           n = 1;
    > > >           int x = 1;
    > > >           int y = 2;
    > > >           n = n + ( x/y );
    > > >           x++;
    > > >           y++;

    >
    > > >       }

    >
    > > >       cout << "The partial sum is " << n << endl;

    >
    > > >    return 0;
    > > >    }

    >
    > > > But this doesn't work. You enter the number of terms and nothing
    > > > happens. So I must have an infinite loop of some kind.

    >
    > > > I'm not sure what's wrong here. I've initialized the variables in
    > > > different places.  Any comments would be welcome.

    >
    > > See above, then run it and then explain to us why so.- Hide quoted text -

    >
    > > - Show quoted text -- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Oops, I forgot a line of code. It should be:
    >
    >   #include <iostream>
    >    using namespace std;
    >
    >    int main()
    >    {
    >
    >       int terms;
    >       int terms_count = 1;
    >       float n;
    >
    >       cout << "Enter the number of terms:\n";
    >
    >       cin >> terms;
    >
    >       while ( terms_count <= terms )
    >       {
    >           n = 1;
    >           int x = 1;
    >           int y = 2;
    >           n = n + ( x/y );
    >           x++;
    >           y++;
    >           terms_count++;
    >
    >       }
    >
    >       cout << "The partial sum is " << n << endl;
    >
    >    return 0;
    >    }- Hide quoted text -
    >
    > - Show quoted text -


    Basically, no matter how many terms I specify, I get:

    The partial sum is 1.

    Why???
     
    Archie, Jun 30, 2010
    #4
  5. Archie

    Öö Tiib Guest

    On 30 juuni, 04:31, Archie <> wrote:
    > On Jun 29, 9:26 pm, Archie <> wrote:
    >
    > > Oops, I forgot a line of code. It should be:

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

    >
    > >    int main()
    > >    {

    >
    > >       int terms;
    > >       int terms_count = 1;
    > >       float n;

    >
    > >       cout << "Enter the number of terms:\n";

    >
    > >       cin >> terms;

    >
    > >       while ( terms_count <= terms )
    > >       {
    > >           n = 1;
    > >           int x = 1;
    > >           int y = 2;
    > >           n = n + ( x/y );
    > >           x++;
    > >           y++;
    > >           terms_count++;

    >
    > >       }

    >
    > >       cout << "The partial sum is " << n << endl;

    >
    > >    return 0;
    > >    }

    >
    > Basically, no matter how many terms I specify, I get:
    >
    > The partial sum is 1.
    >
    > Why???


    For similar reasons (you have some lines extra or at wrong places).
    Use the same method that i suggested for debugging terms_count to
    output other values as well. It's not too hard.
     
    Öö Tiib, Jun 30, 2010
    #5
  6. Archie

    Archie Guest

    On Jun 29, 9:37 pm, Öö Tiib <> wrote:
    > On 30 juuni, 04:31, Archie <> wrote:
    >
    >
    >
    >
    >
    > > On Jun 29, 9:26 pm, Archie <> wrote:

    >
    > > > Oops, I forgot a line of code. It should be:

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

    >
    > > >    int main()
    > > >    {

    >
    > > >       int terms;
    > > >       int terms_count = 1;
    > > >       float n;

    >
    > > >       cout << "Enter the number of terms:\n";

    >
    > > >       cin >> terms;

    >
    > > >       while ( terms_count <= terms )
    > > >       {
    > > >           n = 1;
    > > >           int x = 1;
    > > >           int y = 2;
    > > >           n = n + ( x/y );
    > > >           x++;
    > > >           y++;
    > > >           terms_count++;

    >
    > > >       }

    >
    > > >       cout << "The partial sum is " << n << endl;

    >
    > > >    return 0;
    > > >    }

    >
    > > Basically, no matter how many terms I specify, I get:

    >
    > > The partial sum is 1.

    >
    > > Why???

    >
    > For similar reasons (you have some lines extra or at wrong places).
    > Use the same method that i suggested for debugging terms_count to
    > output other values as well. It's not too hard.- Hide quoted text -
    >
    > - Show quoted text -


    Thanks for that idea. I changed just one thing: I made the variables
    x, y, and n all doubles instead of ints and floats. That seems to do
    the trick.
     
    Archie, Jun 30, 2010
    #6
  7. Archie

    mingze zhang Guest

    On Jun 30, 9:31 am, Archie <> wrote:
    > On Jun 29, 9:26 pm, Archie <> wrote:
    >
    >
    >
    >
    >
    > > On Jun 29, 9:23 pm, Öö Tiib <> wrote:

    >
    > > > On 30 juuni, 04:14, Archie <> wrote:

    >
    > > > > I want my program to print a partial sum of fractions: 1 +  1/2 + 2/3
    > > > > + 3/4 + ... depending on the number of terms I enter.

    >
    > > > > So I wrote this:

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

    >
    > > > >    int main()
    > > > >    {

    >
    > > > >       int terms;
    > > > >       int terms_count = 1;
    > > > >       float n;

    >
    > > > >       cout << "Enter the number of terms:\n";

    >
    > > > >       cin >> terms;

    >
    > > > >       while ( terms_count <= terms )
    > > > >       {

    >
    > > > insert here such code:

    >
    > > >     cout << "The terms_count is " << terms_count
    > > >          << "and that is never bigger than " << terms << endl;

    >
    > > > >           n = 1;
    > > > >           int x = 1;
    > > > >           int y = 2;
    > > > >           n = n + ( x/y );
    > > > >           x++;
    > > > >           y++;

    >
    > > > >       }

    >
    > > > >       cout << "The partial sum is " << n << endl;

    >
    > > > >    return 0;
    > > > >    }

    >
    > > > > But this doesn't work. You enter the number of terms and nothing
    > > > > happens. So I must have an infinite loop of some kind.

    >
    > > > > I'm not sure what's wrong here. I've initialized the variables in
    > > > > different places.  Any comments would be welcome.

    >
    > > > See above, then run it and then explain to us why so.- Hide quoted text -

    >
    > > > - Show quoted text -- Hide quoted text -

    >
    > > > - Show quoted text -

    >
    > > Oops, I forgot a line of code. It should be:

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

    >
    > >    int main()
    > >    {

    >
    > >       int terms;
    > >       int terms_count = 1;
    > >       float n;

    >
    > >       cout << "Enter the number of terms:\n";

    >
    > >       cin >> terms;

    >
    > >       while ( terms_count <= terms )
    > >       {
    > >           n = 1;
    > >           int x = 1;
    > >           int y = 2;
    > >           n = n + ( x/y );
    > >           x++;
    > >           y++;
    > >           terms_count++;

    >
    > >       }

    >
    > >       cout << "The partial sum is " << n << endl;

    >
    > >    return 0;
    > >    }- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Basically, no matter how many terms I specify, I get:
    >
    > The partial sum is 1.
    >
    > Why???


    This may help you.

    http://cnx.org/content/m18717/latest/
     
    mingze zhang, Jun 30, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Billy
    Replies:
    2
    Views:
    528
    Billy
    Feb 1, 2006
  2. pgn

    partial sum

    pgn, Nov 9, 2005, in forum: C++
    Replies:
    2
    Views:
    394
  3. Replies:
    0
    Views:
    1,237
  4. keto
    Replies:
    0
    Views:
    1,045
  5. Walle Wallen
    Replies:
    6
    Views:
    159
    Rick DeNatale
    Mar 29, 2010
Loading...

Share This Page