Understanding recursion in c++ - c++

I think I'm understanding the principle behind recursion, for example the stack like behaviour and the way the program "yo-yo's" back through the function calls, I seem to be having trouble figuring out why certain functions return the values that they do though, the code below returns 160, is this due to the return 5 playing a part, I think I'm right in saying it will go 4*2 + 8*2 + 12*2 etc.. I'm really doubting that when changing my values though.
Would anybody be able to offer a brief explanation as to which values are being multiplied?
cout << mysteryFunction(20);
int mysteryFunction (int n)
{
if(n > 2)
{
return mysteryFunction(n - 4)*2;
}
else return 5;
}

If you are interested in actual call stack:
mysteryFunction(20):
[n > 2] -> mysteryFunction(16) * 2
[n > 2] -> mysteryFunction(12) * 2
[n > 2] -> mysteryFunction(8) * 2
[n > 2] -> mysteryFunction(4) * 2
[n > 2] -> mysteryFunction(0) * 2
[n <= 2] -> 5
5 * 2 = 10
10 * 2 = 20
20 * 2 = 40
40 * 2 = 80
80 * 2 = 160
More generally: 20 = 4*5, so 5 * 2^5 = 5 * 32 = 160.

mysteryFunction(20) => 80 * 2 = 160
mysteryFunction(16) => 40 * 2 = 80
mysteryFunction(12) => 20 * 2 = 40
mysteryFunction(8) => 10 * 2 = 20
mysteryFunction(4) => 5 * 2 = 10
mysteryFunction(0) => 5

Recursion doesn't yo-yo, it just nests deeply.
In you case, the if statement results in either a) the function being called from within the function, or b) a return value... let's look at it running...
A- mysteryFunction(20)
B-- mysteryFunction(16)
C--- mysteryFunction(12)
D---- mysteryFunction(8)
E----- mysteryFunction(4)
F------ mysteryFunction(0) <-- this is the first time (n > 2) is false
Line F is the first time n > 2 is false, which means it returns a 5.
Line F was called by line E, and the value line E gets (5) is multiplied by 2 and returned. So line E returns 10.
Line E was called by line D... and the value it gets (10) is multiplied by 2 and returned, so line D return 20.
... and so on.
Quick version... let's order these to match the order they act on the value...
F: 5
E: F * 2 = 10
D: E * 2 = 20
C: D * 2 = 40
B: C * 2 = 80
A: B * 2 = 160

I will suggest you to read this article on Wikipedia about recursion: http://en.wikipedia.org/wiki/Recursion
In a nutshell a recursive function is one that calls itself until you reach a base case(this is the key). If you don't reach the base case your function will run forever(infinite loop). In the case of your function, get a piece of paper a follow its path picking any number as example, it is the best way to figure out how it works. The factorial is a good example:
the factorial of a number, let's say 5 is !5 = 5 * 4 * 3 * 2 * 1 which is 120. Try it, the principles for recursion is the same regardless the problem.
Here's an example for a factorial function.
Recursion in c++ Factorial Program

Just go through the code and substitute the values.
mysteryFunction(20) -> mysteryFunction(16) * 2
mysteryFunction(16) * 2 -> mysteryFunction(12) * 2 * 2
mysteryFunction(12) * 2 * 2 -> mysteryFunction(8) * 2 * 2 * 2
mysteryFunction(8) * 2 * 2 * 2 -> mysteryFunction(4) * 2 * 2 * 2 * 2
mysteryFunction(4) * 2 * 2 * 2 * 2 -> mysteryFunction(0) * 2 * 2 * 2 * 2 * 2
mysteryFunction(0) * 2 * 2 * 2 * 2 * 2 -> 5 * 2 * 2 * 2 * 2 * 2 -> 160

Related

sas generate 5 digit id code that first 3 must be letters and last 2 numbers

How can I generate in SAS and ID code with 5 digits(letters & Numbers)? Where the first 3 must be letters and last 2 must be numbers.
You can create a unique mapping of the integers from 0 to 26^3 * 10^2 - 1 to a string of the format AAA00. This wikipedia page introduces the concept of different numerical bases quite well.
Your map would look something like this
value = 100 * (X * 26^2 + Y * 26^1 + Z * 26^0) + a * 10^1 + b * 10^0
where X, Y & Z are integers between 0 and 25 (which can be represented as the letters of the alphabet), and a & b are integers between 0 and 9.
As an example:
47416 = 100 * (0 * 26^2 + 18 * 26^1 + 6 * 26^0) + 1 * 10^1 + 6 * 10^0
Using:
0 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
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You get:
47416 -> [0] [18] [6] (1) (6)
A S G 1 6
So 47416 can be represented as ASG16.
To do this programatically you will need to step through your number splitting it into quotient and remainder through division by your bases (10 and 26), storing the remainder as part of your output and using the quotient for the next iteration.
you will probably want to use these functions:
mod() Modulo function to get the remainder from division
floor() Flooring function which returns the rounded down integer part of a real numer
A couple of similar (but slightly simpler) examples to get you started can be found here.
Have a go, and if you get stuck post a new question. You will probably get the best response from SO if you provide a detailed question, code showing your progress, a description of where and why you are stuck, any errors or warnings you are getting and some sample data.

FIFO Page Replacement Algorithm - Counting Page Faults

I'm currently reading about Page Replacement Algorithms, and have been looking at a couple of examples with regards to the FIFO (First In, First Out) method.
My question is as follows; how do you count the number of page faults, as I have seen different practices.
For instance:
Example 1 (on page 9) and Example 2 take the exact same sequence. The first counts the number of page faults to be 12, whereas the second states the number is 15. They are using the same number of frames, 3.
The sequence is:
Sequence: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
-----------------------------------------
7 7 7 0 0 1 2 3 0 4 2 2 2 3 0 0 0 1 2 7
0 0 1 1 2 3 0 4 2 3 3 3 0 1 1 1 2 7 0
1 2 2 3 0 4 2 3 0 0 0 1 2 2 2 7 0 1
-----------------------------------------
PF (1): * * * * * * * * * * * * Total = 12 page faults
PF (2): * * * * * * * * * * * * * * * Total = 15 page faults
Hence, my question is; which method is the correct method? Do you count the first three instances as page faults?
If so, given the sequence:
Sequence: A B C D A E F G H I A J
-------------------------
A A A A A B C D E F G H
B B B B C D E F G H I
C C C D E F G H I A
D D E F G H I A J
-------------------------
PF (1): * * * * * * * * * * * Total = 11 page faults
PF (2): * * * * * * * Total = 7 page faults
Any help would be highly appreciated. Thank you guys!
"Hence, my question is; which method is the correct method? Do you count the first three instances as page faults?"
Yes. Page Fault occurs when you don't fined the referenced page in the frames. Therefore, the first entries are always PFs.

Recursion in c++

I got an exam two days from now and my professor gave us an old exam with the solutions however after going over this problem countless of times I can't figure out how in the world the answer is the answer.
int recursive (int n) {
if (n < 10) return n;
return 100 * recursive (n / 100) + 10 * (n % 10);
}
int main(){
cout << recursive (19683) << endl;
return 0;
}
The answer should print out 16030 but I have no idea of how it gets that. I do
100*196+10*3 = 19630
Then I do
100*1+10*3 = 130
which is completely wrong would appreciate it if someone knew how to get to that answer
The first call (recursive(19683)) returns:
100 * recursive(196) + 10*3
The second call (recursive(196)) returns:
100 * recursive(1) + 10*6
The third call (recursive(1)) returns 1 directly. Substituting back, one gets:
100 * (100 * 1 + 60) + 30 = 10000 + 6000 + 30 = 16030
Back in high school we were taught to be able to desk check our code. Desk checking is where you compute, by hand, the result of every step.
int recursive (int n) {
if (n < 10) return n;
return 100 * recursive (n / 100) + 10 * (n % 10);
}
Pass this 19683
recursive(19683)
19683 < 10 is false
return 100 * recursive(196) + 10 * (19683 % 10 -> 3)
recursive(196)
196 < 10 is false
return 100 * recursive(1) + 10 * (196 % 10 -> 6)
recursive(1)
1 < 10 is true, return 1
substitute recursive(1) = 1 into earlier equation...
return 100 * 1 * 60 -> 160
substitute recursive(196) = 160 into earlier equation...
return 100 * 160 + 10 * 3 -> 16030
recursive(19683) = 100 * recursive(196) + 10 * 3
recursive(196) = 100 * recursive(1) + 10 * 6
recursive(1) = 1
Now back-fill the answers
recursive(196) = 100 + 60
recursive(19683) = 100 * 160 + 30 = 16030
In order to understand what's happening, look at a a simpler example of recursion, such as reversing a string. There's a good explanation of how recursion works in the answers to this question: -
Reverse a string using recursion
Once that makes sense to you, you should find understanding the example question you pose much easier.

Tracing simple recursive function

I am getting closer to debunking this recursive mystery, there is only one thing left that I can not trace in this line of the code, and that is the final return value wich is 243 if i call rec() passing it the value 5. this should be the trace:
n: 4 *3: 12
n: 3 *3: 9
n: 2 *3: 6
n: 1 *3: 3
n: 0 *3: 0
n: 1 *3: 3
result: 243
Correct? how does it get the result of 243?
int rec(int n)
{
if (n == 0)
return 1;
return 3 * rec(n-1);
}
Your function computes : 3^n.
The number 3 is multiplied with the result of the n-1 calls.
f(n) = 3 * f(n-1);
f(0) = 1;
f(1) = 3 * f(0) = 3 * 1 = 3;
f(2) = 3 * f(1) = 3 * 3 = 9;
f(3) = 3 * f(2) = 3 * 3 * f(1) = 3 * 3 * 3 = 27
.
.
.
f(5) = 3 * 3 * 3 *3 * 3 = 243
This function computes
3^n where n >= 0
If you pass 5 it computes 3 * 3 * 3 * 3 * 3 * (1) = 243
It does only multipling on 3, four times:
return 3 * rec(n-1);
I think you wanted something like this:
return n * rec(n-1);

SAS: make values missing

I am trying to make some existing values to missing values (not deleting them).
Here is the basic structure of my data set.
I want to treat AGE and GENDER as missing whenever A is less than B. For example, when A=1 and B=3, I want to treat values of AGE and GENDER on the last two rows as missing (as shown on the data sets).
In my data both A and B go from 1 to 4 and have every combination of them.
Asterisks mean I have more data between them. Thanks in advance!
BEFORE
ID A B AGE GENDER
--------------
1 1 1 35 M
* * * * *
* * * * *
5 1 2 23 F
5 1 2 21 M
6 1 2 42 F
6 1 2 43 M
* * * * *
* * * * *
20 1 3 43 F
20 1 3 39 M
20 1 3 23 M
21 1 3 32 F
21 1 3 39 M
21 1 3 23 F
* * * * *
* * * * *
55 2 4 32 M
55 2 4 12 M
55 2 4 31 F
55 2 4 43 M
* * * * *
* * * * *
AFTER
ID A B AGE GENDER
--------------
1 1 1 35 M
* * * * *
* * * * *
5 1 2 23 F
5 1 2 . .
6 1 2 42 F
6 1 2 . .
* * * * *
* * * * *
20 1 3 43 F
20 1 3 . .
20 1 3 . .
21 1 3 32 F
21 1 3 . .
21 1 3 . .
* * * * *
* * * * *
55 2 4 32 M
55 2 4 12 M
55 2 4 . .
55 2 4 . .
* * * * *
* * * * *
How about now?
data temp;
retain idcount 0;
set olddata;
** Create an observation counter for each id **;
prev_id = lag(id);
if id ^= prev_id then idcount = 0;
idcount = idcount + 1;
run;
** Sort the obs by ID in reverse order **;
proc sort data=temp;
by id descending idcount;
run;
data temp2;
retain misscount 0;
set temp;
by id descending idcount;
** Keep the previous age and gender **;
old_age = age;
old_gender = gender;
** Count the number that should be missing **;
if a < b then nummiss = b - a;
else nummiss = 0;
** Set a counter of obs that we will set to missing **;
if first.id then misscount = 0;
** Set the appropriate number of rows to missing and update the counter **;
if misscount < nummiss then do;
misscount = misscount + 1;
call missing(age, gender);
end;
run;
proc sort data=temp2 out=temp3(drop=misscount nummiss idcount prev_id);
by id idcount;
run;