sum of series

hi all, im tryin to write this program that calulates sum of the series
(1/y)*(2+1/3+4+1/5+....+x), where y and x and user input values. it keeps outputting two for all numbers.

heres my code:

#include<iostream>
#include<cmath>
using namespace std;

int myfunc(int n, int y)
{
n = 2;



for (int x = 0; x<=n; x=x+2)

{
double sum;
if(n%2==0)
{
sum = (1/y)*(x);

}
else
{
sum = (1/y)*((1/x));


}
}

cout << "sum of the series is " << sum << endl;

return sum;

}

int main()
{
int y;
int n;
do{
cout << "Please enter a value for n " << endl;
cin >> n;
cout << "Please enter a value of y " << endl;
cin >> y;


myfunc(n, y);
}
while (y!=0);{
return 0;
}
return 0;
}
Last edited on
you are changing n to 2 for any input. i.e. n is always 2 in loop.

Also why you used this

1
2
3
4
{
return 0;
}
return 0;
closed account (o3hC5Di1)
Hi there,

In your for loop you are re-assigning the value of sum with every iteration of the loop.
What you are aiming for is to increment the sum, so you would use the += combined operator, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int x = 0; x<=n; x=x+2)

{
double sum;
if(n%2==0)
{
sum += (1/y)*(x);

}
else
{
sum += (1/y)*((1/x));


}


As Akshit mentioned, you are also changing the value of n to 2 when you call the function, so no matter what the user inputs doesn't matter, you change it to 2.

Hope that helps.

All the best,
NwN
Last edited on
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
#include<iostream>
#include<cmath>
using namespace std;

int myfunc(int n, int y)
{
	n = 2;

	for (int x = 0; x <= n; x = x + 2)
	{
		double sum;
		if(n % 2 == 0)
		{
			sum = (1 / y) * x;
		}
		else
		{
			sum = (1 / y) * (1 / x);
		}
	}

	cout << "sum of the series is " << sum << endl;

	return sum;

}

int main()
{
	int y;
	int n;
	
	do
	{
		cout << "Please enter a value for n " << endl;
		cin >> n;
		cout << "Please enter a value of y " << endl;
		cin >> y;


		myfunc(n, y);
	} while (y!=0);{  //Right here
	return 0;
	}
	return 0;
}


I don't understand that bit. It's strange that this compiled up.

In NwN's code

initialize sum and declare it outside loop(you have to return it)

double sum=0;

According to your question the general formula is

(1/y)*(summation(2n+1/(1+2n)))

So above loop is not in accordance to formula
I don't understand that bit. It's strange that this compiled up.


Because there is no logical error in there.

warning - function type int returning double value.
Why returning is required?use void.
Last edited on
This is the code tidied up a bit:
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
#include<iostream>
#include<cmath>

using namespace std;

void myfunc(int n, int y) {
   n = 2;
   for(int x = 0; x <= n; x = x + 2)
   {
      double sum;
      if(n % 2 == 0)
         sum = (1 / y) * (x);
      else
         sum = (1 / y) * ((1 / x));
   }
   cout << "sum of the series is " << sum << endl;
}

int main() {
   int y, n;
   do {
      cout << "Please enter a value for n " << endl;
      cin >> n;
      cout << "Please enter a value of y " << endl;
      cin >> y;

   } while(y != 0);
   myfunc(n, y);
   return 0;
}


The issue is still that myfunc is wrong.

Akshit wrote:
Because there is logical error only in there.

No there isn't, you can have as many brackets as you want where you want them, as long as there is a matching opening and closing pair.

The following code is completely legal:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main() {
   int myvar;
   {
      myvar = 12;
      {
         std::cout << "Hello World!" << std::endl;
         std::cout << myvar << std::endl;
      }
   }
   return 0;
}


It's not the prettiest things, but I believe the technical name for brackets is the scope operator. They do come in handy, especially if you know what they do. It's a shame that some people only know them for loops or if statements.
Last edited on
sorry I wanted to write "because there is no logical error in there"
I forgot to write no in b/w.

And I use them(blocks) myself for scopes of classes and variables.

I asked him that why he posted return 0; two times in very first code.Was it typing mistake?
Last edited on
Try this

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
#include "stdafx.h"
#include<iostream>
#include<cmath>
using namespace std;

void myfunc(int n, int y)
{
	float sum=0.00;

	for (int x = 1; x <= n; x++)
	{
		if(x % 2 == 0)
			sum+=(float)1/(x+1);
		else
			sum+=x+1;
	}
	sum*=(float)1/y;
	cout << "sum of the series is " << sum << endl;

}

int main()
{
	int y;
	int n;
	char ch;
	
	do
	{
		cout << "Please enter a value for n " << endl;
		cin >> n;
		cout << "Please enter a value of y " << endl;
		cin >> y;
		if(y==0 || n==0)
			break;

		myfunc(n, y);
		cout<<"Want to check more?";
		cin>>ch;
	} while (ch!='n');
	return 0;
}


Not checked so maybe wrong for (1/y)*(2+1/3+4+1/5+....n terms)
Last edited on
ok so i modified ny program but it doesnt add the fractions like 1/3, 1/5 etc into the sum.
#include<iostream>
#include<cmath>
using namespace std;

int myfunc(int n, int y)
{

n=2;

double sum = 0;





if(n%2==0)
{

for (int x=1; x<=n/2; x++)
{
sum += (2*x)+(1/((2*x)+1));
}
}
else{
for(int x = 1; x<=((n-1)/2); x++)
{
sum = sum +(((n-1)/2)+1);
}
}

cout << "sum of the series is " << sum << endl;

return sum;

}

int main()
{
int y;
int n;
do{
cout << "Please enter a value for n " << endl;
cin >> n;
cout << "Please enter a value of y " << endl;
cin >> y;


myfunc(n, y);
}
while (y!=0);
return 0;
}
Use my code.Its working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	int y;
	int n;
	
	do
	{
		cout << "Please enter a value for n " << endl;
		cin >> n;
		cout << "Please enter a value of y " << endl;
		cin >> y;


		myfunc(n, y);
	} while (y!=0);
        
        //this should give an error because he got these brackets there without any if or while or anything. That's what I meant 
    {  //Right here
	     return 0;
	}
	
        return 0;
}
There is nothing wrong with having extra brackets. They're used to create scopes for things. No error is to be given for something that has a scope that the code created. Granted, there is no need for it here, and usually isn't a need elsewhere, but nothing wrong with it.
tested and turned out it is actually fine.
that surprised me to be honest.

@abaz01

Is my code working and giving proper result?
Topic archived. No new replies allowed.