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;
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.
#include<iostream>
#include<cmath>
usingnamespace 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.
#include<iostream>
#include<cmath>
usingnamespace 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.
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.
#include "stdafx.h"
#include<iostream>
#include<cmath>
usingnamespace 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)
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.