help with a function

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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #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.

Hope this helps.
Thanks for the quick reply, this has indeed fixed the problem. Do comment if you have any advise on the program overall.
Last edited on
Topic archived. No new replies allowed.