Tut_6 - return 0; else return -1 + mystery( a + b, b);...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming & Data Structures Tutorial 6 Q1. How does recursion differ from iteration? Q2. What does the following mystery recursive method do? // The mystery method…. .oooh! int mystery(int a, int b) { if ( b > 0 ) // b is positive { if( a < 0 ) // a is negative { if( a + b > 0 ) return 0; // base case else return -1 + mystery(a + b, b); // recursive case } else // a is positive { if( a - b < 0 ) return 0; // base case else return 1 + mystery( a - b, b ); // recursive case } } else // this part accounts for negative b { if( a < 0 ) // a is negative { if( a - b > 0 ) return 0; // base case else return 1 + mystery( a - b, b); // recursive case } else // a is positive { if( a + b < 0 ) // base case
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: return 0; else return -1 + mystery( a + b, b); //recursive case } } } Q3. Write a method that will recursively fill an array with data according to its position in the array. As a hint, here is the method header to start you off: int fill_array(int array[ ], int size) Q4. Using recursion, write a method that given 2 numbers, x and y will get the multiple of the 2 numbers. If you want to keep it simple you can assume that the numbers entered will always be positive. Q5. If you want extra practice with recursion, try writing the factorial method we did in class without looking at the solution in the notes. Remember: factorial n! is defined as n! = 1 if n = 0 n * (n-1)! if n &gt; 0...
View Full Document

Page1 / 2

Tut_6 - return 0; else return -1 + mystery( a + b, b);...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online