Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Want to print partial sum but doesn't work

Reply
Thread Tools

Want to print partial sum but doesn't work

 
 
Archie
Guest
Posts: n/a
 
      06-30-2010
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
 
Reply With Quote
 
 
 
 
Tiib
Guest
Posts: n/a
 
      06-30-2010
On 30 juuni, 04:14, Archie <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Archie
Guest
Posts: n/a
 
      06-30-2010
On Jun 29, 9:23*pm, Tiib <(E-Mail Removed)> wrote:
> On 30 juuni, 04:14, Archie <(E-Mail Removed)> 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;
}





 
Reply With Quote
 
Archie
Guest
Posts: n/a
 
      06-30-2010
On Jun 29, 9:26*pm, Archie <(E-Mail Removed)> wrote:
> On Jun 29, 9:23*pm, Tiib <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On 30 juuni, 04:14, Archie <(E-Mail Removed)> 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???
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      06-30-2010
On 30 juuni, 04:31, Archie <(E-Mail Removed)> wrote:
> On Jun 29, 9:26*pm, Archie <(E-Mail Removed)> 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.
 
Reply With Quote
 
Archie
Guest
Posts: n/a
 
      06-30-2010
On Jun 29, 9:37*pm, Tiib <(E-Mail Removed)> wrote:
> On 30 juuni, 04:31, Archie <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Jun 29, 9:26*pm, Archie <(E-Mail Removed)> 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.
 
Reply With Quote
 
mingze zhang
Guest
Posts: n/a
 
      06-30-2010
On Jun 30, 9:31*am, Archie <(E-Mail Removed)> wrote:
> On Jun 29, 9:26*pm, Archie <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Jun 29, 9:23*pm, Tiib <(E-Mail Removed)> wrote:

>
> > > On 30 juuni, 04:14, Archie <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Problem - I want to print Current Output of Pdf file and shouldprint once.I get print dialog box but it is not working Lew Java 0 05-30-2007 01:31 PM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Re: Partial 1.0 - Partial classes for Python Thomas Heller Python 13 02-08-2007 08:12 PM
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM
partial sum pgn C++ 2 11-09-2005 07:57 PM



Advertisments