Need a little help with the following:
function sum4 seems to always return value 1 no matter what the input.
Also could use some advice on the code and all.
Thanks :)
#include<iostream.h>
#include<conio.h>
float sum1(int);
float sum2(int);
float sum3(int);
float sum4(int);
float sum1(int x)
{
float S;
S=(x*(x+1))/2;
return S;
}
float sum2(int x)
{
int i,j;
float S=0;
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
S+=j;
}
return S;
}
float sum3(int x)
{
int i,j;
float S=0;
for(i=1;i<=x;i++)
{
for(j=2;j<=(2*i);j+=2)
S+=j*j;
}
return S;
}
float sum4(int x)
{
int i,j;
float S=0;
for(i=1,j=1;i<=x;i++,j+=2)
{
S+=1/(j*j);
}
return S;
}
void main()
{
clrscr();
int n,x;
float sum;
cout<<"Welcome to Series Calculator"<<endl;
cout<<"Enter 0 to clear console"<<endl;
cout<<"Enter 1 to exit program"<<endl;
cout<<"Enter 2 to calculate sum upto nth term of series 1+2+3+...+n"<<endl;
cout<<"Enter 3 to calculate sum upto nth term of series 1+(1+2)+(1+2+3)+...+n"<<endl;
cout<<"Enter 4 to calculate sum upto nth term of series 2^2+(2^2+4^2)+(2^2+4^2+6^2)+...+n"<<endl;
cout<<"Enter 5 to calculate sum upto nth term of series 1/(1^2)+1/(3^2)+1/(5^2)+...+n"<<endl;
cin>>x;
do
{
switch(x)
{
case 0:clrscr();
cout<<"Conosle has been cleared successfully"<<endl;
cout<<"Enter another choice"<<endl;
cout<<"Enter 1 to exit program"<<endl;
cout<<"Enter 2 to calculate sum upto nth term of series 1+2+3+...+n"<<endl;
cout<<"Enter 3 to calculate sum upto nth term of series 1+(1+2+)+(1+2+3)+...+n"<<endl;
cout<<"Enter 4 to calculate sum upto nth term of series 2^2+(2^2+4^2)+(2^2+4^2+6^2)+...+n"<<endl;
cout<<"Enter 5 to calculate sum upto nth term of series 1/(1^2)+1/(3^2)+1/(5^2)+...+n"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
case 1:cout<<"Exitting from program..."<<endl;
cout<<"Press any key to close...";
x=6;
break;
case 2:cout<<"Enter the number of terms upto which you want to calculate the sum of the series 1+2+3+..+n"<<endl;
cin>>n;
while(n<1)
{
cout<<"Please enter a valid number of terms"<<endl;
cin>>n;
}
sum=sum1(n);
cout<<"Sum of the series 1+2+3+..+n upto "<<n<<" terms is "<<sum<<endl;
cout<<"Enter another choice(1 to exit)"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
case 3:cout<<"Enter the number terms upto which you want to calculate the sum of the series 1+(1+2)+(1+2+3)+...+n"<<endl;
cin>>n;
while(n<1)
{
cout<<"Please enter a valid number of terms"<<endl;
cin>>n;
}
sum=sum2(n);
cout<<"Sum of the series 1+(1+2)+(1+2+3)+..+n upto "<<n<<" terms is "<<sum<<endl;
cout<<"Enter another choice(1 to exit)"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
case 4:cout<<"Enter the number terms upto which you want to calculate the sum of the series 2^2+(2^2+4^2)+(2^2+4^2+6^2)+...+n"<<endl;
cin>>n;
while(n<1)
{
cout<<"Please enter a valid number of terms"<<endl;
cin>>n;
}
sum=sum3(n);
cout<<"Sum of the series 2^2+(2^2+4^2)+(2^2+4^2+6^2)+..+n upto "<<n<<" terms is "<<sum<<endl;
cout<<"Enter another choice(1 to exit)"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
case 5:cout<<"Enter the number terms upto which you want to calculate the sum of the series 1/(1^2)+1/(3^2)+1/(5^2)+...+n"<<endl;
cin>>n;
while(n<1)
{
cout<<"Please enter a valid number of terms"<<endl;
cin>>n;
}
sum=sum4(n);
cout<<"Sum of the series 1/(1^2)+1/(3^2)+1/(5^2)+...+n upto "<<n<<" terms is "<<sum<<endl;
cout<<"Enter another choice(1 to exit)"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
default:cout<<"Invalid choice!!"<<endl;
cout<<"Enter another choice(1 to exit)"<<endl;
cin>>x;
while(x>5)
{
cout<<"Please enter a smaller value for choice(1 to exit)"<<endl;
cin>>x;
}
break;
}
}while(x<6);
getch();
}
Hi,
I admit I did not read all the code but the problem could be this: "j" is defined as an int, so when you do 1/(j*j) the result is an int. This means that 1/2 = 0 since the value is truncated, and the same applies for every other division. A solution could be this: (float)1/(j*j) where you are casting 1 to a float. In this way the precision of the operation is the best one among the precisions of the operands, and in this case the best precision is the floating point one.