answersLogoWhite

0


Best Answer

The operation you describe is not a recursive one, it is iterative (linear in nature). It's also better to return the index of the smallest value rather than just the value itself. In this way we know the position of the smallest value as well as the value itself.

To find the smallest element in an array, we start with the first element. Being the only value encountered so far, it must be the smallest, so we store its index. We then compare the value at the stored index to that of each of the remaining elements, one by one. When we find a smaller value, we update the stored index, because that value is the smallest encountered so far. When we reach the end of the array, the stored index will tell us which element was the smallest, so we simply return that index.

The algorithm can (and should) be generalised to cater for arrays of any length.

// Returns the index of the smallest value in array a of length n

int smallest (const int* const a, int n) {

if (n<1) return -1; // sanity check: the array must have 1 or more elements to be valid

int i, j;

i = 0; // assume first element holds smallest value until proven otherwise

for (j=1; j<n; ++j) if (a[j] < a[i]) i = j; // update i each time a smaller value is found at index j

return i; // a[i] holds the smallest value in the array

}

int main (void) {

const int max = 10; int a[max] = {5, 4, 6, 3, 0, 7, 2, 8, 1, 9};

int i;

i = smallest (a, max);

assert (i==4); // a[4] holds the smallest value

return 0;

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a recursive procedure that returns the smallest value in an array of 10 elements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number &gt; 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.


How do you copy all the elements from a given stack implemented in a linked list to a new one and returns the pointer to the new stack?

LIFO


What are the element of the visual basic?

A declared element is a programming element that is defined in a declaration statement. Declared elementsinclude variables, constants, enumerations, classes, structures, interfaces, procedures, procedure parameters, function returns, external procedure references, operators, properties, events, and delegates


What differences between reentrant function and recursive function?

A reentrant function is called by the program during execution and can be interrupted and recalled later. A recursive function can call itself during execution and repeats itself without interruption.

Related questions

CRITBINOM function returns the smallest value for which the cumulative is greater than or equal to a given value?

yes


What is a minimum function?

The minimum function is the function that takes two arguments and returns the smallest of the two. Alternatively the function can take any finite amount of arguments and return the smallest.


What is different between a subroutine and function in vb.net?

Function returns a value but sub procedure do not return a value.


What does vector's member size() do?

The vector size() member returns the current size of the vector, in elements.


What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number &gt; 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


What is recursive calls.which data structure is used in it?

A recursive call is any function that calls itself with modified parameters. There is no data structure associated with it, other than the call stack which is associated with all function calls, recursive or not. The stack is used to store the state of the current instance's local variables, the return address, and the arguments to be passed to the next instance. This allows recursive calls to "unwind" in the reverse order they were called. Recursive functions must have a terminating condition as all the recursive calls must eventually return to the original caller at some point. If they don't, they'll simply keep calling themselves until there's no more space on the stack. Recursive functions are commonly used in divide-and-conquer algorithms, gradually reducing larger problems down to smaller instances of the same problem until a terminating condition is satisfied. As the calls return, the larger problem is gradually solved until the function returns to the original caller, at which point the entire problem is fully resolved. Quicksort is a typical algorithm that makes use of recursion.


What are the Responsibilities of a nurse during laryngoscopy?

A nurse plays an important role in explaining the procedure to the patient, preparing the patient for the procedure, and assisting the physician in conducting the procedure. A nurse also assists patient recovery after the procedure, administering fluids and lozenges once the gag reflex returns, and monitoring vital signs.


What is the amazon UK street address for returns?

It's not just as simple as getting a return address. You need to go to the Amazon site, and follow the returns procedure. This will enable you to print out a returns label for your item so it goes back to the correct department. Go to the Amazon home-page and scroll to the bottom. There is a link there marked 'returns are easy'


How can a procedure be defined in C plus plus?

A procedure is simply a function in C++, therefore you define procedures just as you would any function. In some languages, a procedure is not a function as such, insofar as there is no return type. The C++ equivalent would therefore be a function that returns void.


What is the difference between near and far procedure in 8086 microprocessor?

Near calls and returns transfer control between procedures in the same code segment. Far calls and returns pass control between different segments.


Does the TI-89 have a built-in function that returns the number of elements in a list or matrix?

Yes, the in-built dim() function


What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.