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
+ 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