Want to print partial sum but doesn't work

A

Archie

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
 
Ö

Öö Tiib

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.
 
A

Archie

insert here such code:

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











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;
}
 
A

Archie

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???
 
Ö

Öö Tiib

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.
 
A

Archie

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top