st12-recursion

st12-recursion - and backward. e.g.: ABCDCBA AABBAA...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Recursion (Self-test questions)
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 What's the output? void foo(int x) { if (x <= 0) return; foo(x / 10); cout << x % 10; } foo(6201);
Background image of page 2
3 What's the output? void foo(int x) { if (x <= 0) return; cout << x % 10; foo(x / 10); } foo(6201);
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 What's the output? int foo(int x, int y) { if (x < 0 || y < 0) return 0; if (x > y) return x + foo(x – 1, y); else return y + foo(x, y – 2); } foo(3, 7);
Background image of page 4
5 What's the output? int foo(int a[], int idx1, int idx2, int x) { if (idx2 < idx1) return -1; int m = (idx1 + idx2) / 2; if (a[m] > x) return foo(a, idx1, m-1, x); else if (a[m] < x) return foo(a, m+1, idx2, x); else return m; } int bar[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256 }; cout << foo(bar, 0, 8, 4);
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Recursion Function Formulate a recursive solution to check if a string is a palindrome – a string that reads the same forward
Background image of page 6
Background image of page 7

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

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

Unformatted text preview: and backward. e.g.: ABCDCBA AABBAA Implement a recursive C++ function based on your solution. 7 Recursion Function int A[20], B[20]; Formulate a recursive solution to compare if arrays A and B are the same. Implement a recursive C++ function based on your solution. 8 5 pirates have 100 gold coins to divide. The senior pirate proposes how to divide the coins, and the pirates then get to vote. If at least half of the pirates agree to the proposal, the division is made; otherwise the senior pirate is killed and the process is repeated. If you are the senior pirate (pirate #5), what should you propose? Assume all the pirates are logical beings. An interview question quoted from the book &quot;How Would You Move Mount Fuji?&quot; by William Poundstone....
View Full Document

Page1 / 8

st12-recursion - and backward. e.g.: ABCDCBA AABBAA...

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

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