![]() |
|
#1
|
|||
|
|||
Finding Minimum Value in Recursive ArraysI have an assignment that asks me to find the minimum value in an array.
Here are the instructions given by my instructor: Write a C++ program having a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of 1 element. I got the program to sort the elements from smallest to largest, but I believe my code contains logical errors. I'm having trouble with the latter part of the assignment. The part that says... "The function should stop processing and return when it receives an array of 1 element". Can someone help me with this situation? Here is my written code: CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysQuote:
Based on my interpretation of the instructions, the function accepts the array and a size. Use the size as an index into the array to test the last element. You can then subtract one from the size and call the function again. When the size gets to one, you start the return phase of the recursion... __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
Re: Finding Minimum Value in Recursive ArraysDo you think I shoud ask the user to input numbers?
For example, CPP / C++ / C Code:
Is that how we should approach the program?. |
|
#4
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysQuote:
CPP / C++ / C Code:
Once you have the program running, switch to input. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#5
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysPlease notice that you where given a clue about the function prototype ...
The function gets the array and the array size and returns the minimal value. You used void function, so you do not comply to all of the instructions. try something like : CPP / C++ / C Code:
And keep in mind that your function should stop when the array size is 1 (still, you need to check that last argument - don't forget it), so your recursive calls will decrement the array size value along your algorithm. Keep us posted, Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#6
|
|||
|
|||
Re: Finding Minimum Value in Recursive ArraysIn the function:
CPP / C++ / C Code:
Why is array_size "unsigned"?.. Can someone explain why it's unsigned? Also, how can I compare the elements so that I can display the smallest element of the array? |
|
#7
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysQuote:
Long answer -- Why not? Can the size of an array be negative? It doesn't have to be unsigned, but it doesn't hurt. Quote:
This continues the previous instance of the function (array size == 2). Compare the returned value with the current (2nd) element. return the lower of the two values. Continue.... __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#8
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysQuote:
1. Because the index of an array >=0. If you get used to passing the "logical" argument, then you may be warned by the compiler about possible logical errors (I'm using VC6 with warning level 4). 2. Because I think this is the correct way to define the requested function. About your other question : Comparing the elements. I see Walt answered you and I can only suggest you look at recursion examples. Post the code you wrote untill now. Best regards, Kobi. __________________
It's actually a one time thing (it just happens alot). |
|
#9
|
|||
|
|||
Re: Finding Minimum Value in Recursive ArraysI'm almost at the point where I can begin the program.. I'm still stuck on something. For example, the following..
Quote:
I'm not sure how to "switch to input".. Can someone show me how I would do this? Also, how many for loops would I need in this program? |
|
#10
|
||||
|
||||
Re: Finding Minimum Value in Recursive ArraysThu, you misunderstood what Walt meant.
He meant that, for testing, go with the sample array with size 6. Once you have successfully created the recursion function, and verified the results, you can get the input from the user. So, dont worry about input for now... Quote:
Anyway, Here is how you get the input for the array: (go with the sample array for testing...) For that, get the the input from the user about what is the size of the array. then create the array using the new operator. Get the input using a loop and a cin statement. Here is one example: CPP / C++ / C Code:
After doing this, the numbers will be stored in the array. ![]() Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 04:59 |
| Major problem with recursive function, help.. | kakamuti | C Programming Language | 4 | 19-Dec-2004 08:47 |
| Knight tour (arrays help needed) | dilmv | C++ Forum | 7 | 18-Oct-2004 15:31 |
| Using an array and finding the element number (subscript) | tommy69 | C Programming Language | 27 | 05-Apr-2004 13:23 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The