st12-recursion

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

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

1 CSC 1111 Introduction to Computing using C++ Recursion (Self-test questions)

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

View Full Document
2 What's the output? void foo(int x) { if (x <= 0) return; foo(x / 10); cout << x % 10; } foo(6201);
3 What's the output? void foo(int x) { if (x <= 0) return; cout << x % 10; foo(x / 10); } foo(6201);

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

View Full Document
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);
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);

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

View Full Document
6 Recursion Function Formulate a recursive solution to check if a string is a palindrome – a string that reads the same forward

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

View Full Document
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 "How Would You Move Mount Fuji?" by William Poundstone....
View Full Document

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 8

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

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

View Full Document
Ask a homework question - tutors are online