No this is not for class. Im just practicing c++ on my own.
Quite literally homework then.
How about writing a function that takes an integer and returns an integer? One could call it, for example, int u( int n );
Then again, that leads to recursion, which probably is not the theme of the chapter. Perhaps, a more appropriate function is int u( int n, int u1, int u2 );
The keyword in your link is iteration. I see no iteration in your code for cases that have 1<N.
Note, the u(2) is not a special case; only u(0) and u(1) are (assuming that the user types only positive numbers).
The wiki article literally has like 5 example programs you could look at. Though as mentioned earlier you are missing iteration which is the key element of these exercises. I personally would do recursion like mentioned before but that isn't the purpose of exercise since they explicitly said iteration so in that case you will need some sort of loop.
Thank you for making that wild assumption. If I've clearly said, "Im practicing c++ on my own" and even posted my own attempt at the code, then you should just trust me instead of going with the oh-so popular reply on this forum of "its homework". Is it that hard to believe that someone is genuinely trying to learn c++ on his own and found a website with example exercises and is having trouble with one of them?
I know I could do this with recursion, but I'd rather try to do it without it. I did exercise 8 (a similar problem) without recursion and it worked just fine. Im trying to do something similar here.
Thank you gilbilt and keskiverto for your responses. I'll play around a bit more based on your suggestions and re-post my revised code if I still need help.
P.S. I know the article has solutions, but I'd rather not look at them yet. I like to look at the solutions at the end once I've written my own code correctly. It saves me from the "hindsight bias".
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int N, zeroth=3, first=2, second, third;
for(int i = 1; i<10; i++)
{
cout << "Enter in an integer N: " << flush;
cin >> N;
if(N==0)
cout << "u(" << N << ") = " << zeroth << endl << endl;
elseif (N==1)
cout << "u(" << N << ") = " << first << endl << endl;
while(N>=2)
{
second = N*first + (N+1)*zeroth + N;
zeroth = first;
first = second;
cout << "u("<< N << ") = " << second << endl << endl;
break;
}
}
cin.get();
}
It works, but only if I enter in the numbers in order (i.e. 0, 1, 2...). If I skip around or go back to a different number it messes up and outputs the incorrect answer.
So If I enter in a 1 after entering in a 3, it outputs "u(1) = 56". What I dont understand is why it doesn't perform the else if statement that I have for the case where N==1?
It seems as though it skips over it if I go back to.
If I go straight into the loop with N>2 on the first entry, then also it outputs the incorrect answer. Not sure why again.
You don't seem to understand the meaning of "homework". Homework (according to me) is something that one does at home (or similar location) on their own with a goal to learn.
Unlike attendees of classes, you have an extra challenge. You are your own "professor", who is "reluctant to guide the student", but will immediately know if you cheat. Then again, your "class" does not give any credit or diploma, so the only thing that you can get out of the study is knowledge and skill. IMO, one should attend classes in order to genuinely learn, not to "just get credit points".
The exercise is in an "appendix to a book" in a "group" that has label "iteration". Books tend to be divided into parts, chapters, sections, paragraphs. Humans have innate need to categorize. I used the label "chapter" as it did seem "a scope of natural size" for the topic (of iteration).
Most of those posted solutions are recursive. There is one iterative solution, and it is actually the shortest of them all due to nearly obfuscatingly compact style.
The (typical) recursive solution for this problem operates "top down". The user simply calls answer = u( N );. That keeps the "main program" extremely simple. The called function, however, has to make recursive calls with smaller N and take care of the conditions that end the recursion.
The iteration works "bottom up". The "main program" essentially calculates the value of u(x) for every x in [0..N], and has to store enough intermediary results for the iteration to progress. The main program is more complex, but the u() is called much less than in recursion.
Lets count for N=5.
Iteration: v(0) and v(1) are trivial. v() is called 4 times.
Recursion:
u(5)#1 calls u(4)#2 and u(3)#3
u(4)#2 calls u(3)#4 and u(2)#5
u(3)#4 calls u(2)#6 and u(1)#7
u(2)#6 calls u(1)#8 and u(0)#9
u(2)#5 calls u(1)#10 and u(0)#11
u(3)#3 calls u(2)#12 and u(1)#13
u(2)#12 calls u(1)#14 and u(0)#15
15 calls of u(), if I'm not mistaken and 5 is still a small N. Furthermore, the u() is more complex than the v().
PS. Why did you have the "repeat 10 times" loop on line 11?
Homework (according to me) is something that one does at home (or similar location) on their own with a goal to learn.
I reckon you're in a minority with this definition. I left school over 20 years ago but when someone says the word "homework" I still think its work done at home, intended for school.
But maybe that's just me :)
I was using the value of N to figure out the value of "second". I understand now that I needed to use a different variable to calculate N, and also iterate that value up to N while also doing the other two iterations.
Here is my final code. The outside for-loop is just so i can test 10 values of N.
#include<iostream>
usingnamespace std;
int N;
longdouble ans;
longdouble u (int n){
longdouble rec;
if (n==0)
rec=3;
if (n==1)
rec=2;
if (n>1)
rec=n*u(n-1)+(n+1)*u(n-2)+n;
if (n<0)
rec=(u(n+2)-(n+2)-(n+2)*u(n+1))/(n+3);
return rec;}
int main () {
cout<<"Enter an integer N\t";
cin>>N;
ans=u(N);
cout<<"u(N) =\t"<<ans<<endl;
system("PAUSE");
return 0;}