C++, Can anybody help me with this recursive function ? https://www.dropbox.com/s/8j48u3gq1wmqtgq/Screenshot%202015-11-28%2015.29.06.png?dl=0
#include <iostream> using namespace std; double n; int display(double n) { if (n == 0) { return 1; } else{ return float(1)/n + float(1)/display(n-1); } } int main() { cout << display(5); }
what I have so far
my version #include <iostream> using namespace std; double inverted_sum(int x){ if(x==1){ return 1; } return (float(1)/x + inverted_sum(x-1) ); } int main(){ cout<<inverted_sum(1); getchar(); } see the difference! and again, wrong section :P
`float(1)/display(n-1)` that was wrong should just be 'display(n-1)'
what is getchar(); ?
getchar waits for a character in the input/output terminal
so that the I/O terminal doesn't vanish immediately after displaying
but still how would you get all the values of the generated sequence ?
means? sequence will generate one output for one value of x ...
For your code, here's what it would return with each iteration: n=5: 1/5 n=4: 1/5+1/4 n=3: 1/5+1/4+1/3 n=2: 1/5+1/4+1/3+1/2 n=1 1/5+1/4+1/3+1/2+1/1 n=0: 1/5+1/5+1/3+1/2+1/1+1 As you can see, you have the 1 term duplicated. You'd avoid this by stopping when n=1 as @hartnn's code does. The other issue is it should be display(n-1) not float(1)/display(n-1) as @hartnn said.
so 5 should generate 1/5 ?
right ?
display(5)
5 should generate 1 +1/2+1/3+1/4+1/5 = 2.283
also the questions says to take the input as integer, so you probably should use cin and take an integer and pass it to your function before submitting the answer
one best way to verify whether you code is working or not, take an input and run the code in your mind, when input is 5, display function will first check, whether 5 == 1? it will be false, so it goes to return 1/5+display(4) before returning, display(4) needs to be calculated, so 4 ==1 ? false so return 1/4 + display(3) 3==1? false return 1/3 +display(2) 2==1? false return 1/2 + display(1) 1==1? true return 1 .......................... then all returns will execute, return 1/2 +1 return 1/3+1/2+1 return 1/4+1/3+1/2+1 return 1/5+1/4+1/3+1/2+1 which is returned in main
Since we're in math... hehe... Yeah the idea is you have this function: \[f(n)=\sum_{k=1}^n \frac{1}{k}\] But you can pull out the first term of the sum like this: \[f(n)=\frac{1}{n} + \sum_{k=1}^{n-1} \frac{1}{k}\] But then we see that summation is just f(n-1) so we plug it in: \[f(n)=\frac{1}{n}+f(n-1)\] So now if you just call your f(n) function, it will return 1/n and add that to the call of f(n-1), which then lets you step down until you get to the bottom. You just gotta be sure to tell it that f(1)=1 so that it stops, otherwise you'll find it trying to calculate 1/0, 1/-1, 1/-2, etc... (Not sure how C++ handles infinity, but I am imagining it's not too different from how Java handles floats) So at least this is the idea of what you're doing here in terms of probably more confusing math notation, or not, I don't know your background in math >_>
hmm so series in math are symbolized with summation ?
It's been a long time since I have done series and I dont remember. Bascally I dont know if he wants me to output each individual element (1/5 + 1/4 +....1/1) or all of them as a sum
he just wants the sum
fibonacci is a Series right ?
so fibonacci(5) = 12 ? or 0 1 1 2 3 5
sum of fibonacci terms till 5 gives 12 all the fibonacci terms till 5 gives 0 1 1 2 3 5 both are different questions. what you're asked is just the sum.
hmm is a Series different than a Sequence ?
what if I wanted to write a program that outputs each individual element @hartnn ? Do you thing thats possible with recursion ? I am just practicing to see if I can solve it in different ways
I see the confusion on the fibonnaci thing, f(5) doesn't mean 'add up all the fibonacci numbers that have value less than 5' Instead, it relies on the fact that fibonacci numbers are defined in a recursive way, when you write them out, you rely on the last two numbers to pick the next one, right? That relation looks like this: \[f(n)=f(n-1)+f(n-2)\] so I'll go ahead and write out the first few terms in this format and see if yo understand this recursion relation: f(0)=0 f(1)=1 f(2)=1 f(3)=2 f(4)=3 f(5)=5 f(6)=8 f(7)=13 f(8)=21 Se how f(7)=f(6)+f(5) ? Plug in the 5th, 6th, and 7th Fibonacci numbers to see: 13=8+5
hmmmm i see
but how I go about displaying the elements individually instead of some for the first problem ? for eg display(5) would result in 1/1 , 1/2 , 1/3 , 1/4 , 1/5
Anyways I found it
#include <iostream> using namespace std; double inverted_sum(int x){ if(x==1 || x==0){ return 1; } return (float(1)/x + inverted_sum(x-1) ); } int main(){ int i=1; while (i!=6){ cout << inverted_sum(i) - inverted_sum(i-1) << ' ' ; i++; } getchar(); }
#include <iostream> using namespace std; double inverted_sum(int x){ if(x==1){ return 1; } else if (x==0){ return 0; } return (float(1)/x + inverted_sum(x-1) ); } int main(){ int i=1; while (i!=9){ cout << inverted_sum(i) - inverted_sum(i-1) << ' ' ; i++; } getchar(); }
seriously ? if you don't want to evaluate the sum, why not simply do : ``` while (i!=6){ cout << "1/"<<i ; i++; } ```
I am practicing recursion
even the strating problem would be simpler with loop, and faster
yeah, i believe the question wants you print out the final value of sum after evaluating not just printing the string : "1 + 1/2 + 1/3 + ... "
Join our real-time social learning platform and learn together with your friends!