Plesese help: i have no ideas to do with this problem.

Write a program which allows the user to input several real numbers and then prints out the 2nd smallest number. The input sequence starts with a positive integer indicating the number of real numbers. For example, for the input sequence “4 [enter] 0.4 [enter] -0.2 [enter] 1.2 [enter] 4.5 [enter]”, your program should print out “0.4”.

Note: have to use dynamic memory management (new, delete, malloc, free, etc.), divide-and-conquer, and recursive calls. Divide the array into two (nearly) equal-sized subarrays. Given the smallest and the 2nd smallest from both subarrays, you can then decide the smallest and the 2nd smallest for the whole arrays.

So where exactly are you stuck?

Can you
- read a number from the user?
- read several numbers from the user?
- allocate memory
- yada yada

You can't just say "here's my homework - I'm stuck" without actually posting where you are stuck.

So where exactly are you stuck?

Can you
- read a number from the user?
- read several numbers from the user?
- allocate memory
- yada yada

You can't just say "here's my homework - I'm stuck" without actually posting where you are stuck.

u should use a sorting algorithm like selection sort...i think the looks for the nth smallest number...google it to be sure

Comments
So what does this have to do with the OP?

Doing a sort to work out something like this is excessive (although of course over a handful of numbers the extra processing is probably negligible).

A simple min routine will do the trick with the addition of an extra variable.

Initialise min and the extra variable to the first element in the array. Then scan the array looking for a new minimum value. Every time you find a new minimum value store the old minimum value in your extra variable before overwriting it with the new minimum value.

At the end of the loop the extra variable will contain the 2nd smallest value and it runs O(n) which is rather better than any sorting algorithm.

You can do a minimum search as recursion. Basically anything that can be done iteratively (with a loop) can be done recursively. There are very few, but not 0, things where it is especially easier one way or the other.

This article has been dead for over six months. Start a new discussion instead.