SICP++

Monday, June 23, 2008

question: find the 2nd highest number

Hi,

Here is a question requiring use of functional programming in C++

Question 1: write a function in c++ which takes as input 5 integers and returns the 2nd highest inegers.
e.g. func(10, 13, 2, 8, 23) returns 13
The constraint is that the C++ function should not use any kind of variables (local or global) what-so-ever.
This has to be done in purely functional way.
Also the solution should be easily extendable to a function with say 10 or 20 parameters.

Question 2: Change the above function such that the function returns 2nd highest, 3rd highest ... number based on an extra argument which is, say, the last argument to the function.
e.g. func(10, 13, 2, 8, 23, 2) returns 13
func(10, 13, 2, 8, 23, 3) returns 10
func(10, 13, 2, 8, 23, 1) returns 23

Question 3: Since the above can be computed at compile time, do the above exercises at compile time.

Please share your answers in the comments below.

Regards,
weima


PS: And if you find the question too stupid, please say so in the comments. :)

why sicp++

Hi,

Here i want to do a few selected exercises from sicp in C++. My aim is to be able to take the concepts taught in sicp, and apply it to my C++ programs.

I want to focus mainly on the chapters
  1. Building Abstractions with Procedures
  2. Building Abstractions with Data
I hope the concepts in these chapters help me immensly.

regards,
weima