# Write a recursive c function that finds the maximum value in an array

Jeff Mercado 4, I'm pretty sure any half-decent optimising compiler will remove the redundant assignment in the original code's ternary operator. Since you're pre-incrementing and starting at 1, in the first iteration i would already be 2 at the if statement. It doesn't really matter which one is used, in the context of a for loop, they are exactly the same, increment i. Now let us imagine that our function actually works. If it works we can use it to give the result of more complex cases. So we actaully have the exact answer for all cases in the top level recursion.

Our problem is getting smaller on each recursive call because each time we call the function we give it a smaller number. Try running this program in your head with the number 2. Does it give the right value? Now will it work for 3? Now since we know that factorial of 2 works, factorial of 3 also works.

We can prove that 4 works in the same way, and so on and so on.

## You are here

Forgetting the base case leads to infinite recursion. There are several significant problems with recursion. Mostly it is hard especially for inexperienced programmers to think recursively, though many AI specialists claim that in reality recursion is closer to basic human thought processes than other programming functions such as iteration.

There also exists the problem of stack overflow when using some forms of recursion head recursion. The other main problem with recursion is that it can be slower to run than simple iteration. Then why use it? It seems that there is always an iterative solution to any problem that can be solved recursively. Is there a difference in computational complexity? Is there a difference in the efficiency of execution? Yes, in fact, the recursive version is usually less efficient because of having to push and and pop recursions on and off the run-time stack, so iteration is quicker. On the other hand, you might notice that the recursive versions use fewer or no local variables.

So why use recursion? The answer to our question is predominantly because it is easier to code a recursive solution once one is able to identify that solution.

The recursive code is usually smaller, more concise, more elegant, possibly even easier to understand, though that depends on ones thinking style. But also, there are some problems that are very difficult to solve without recursion.So I can get down to finding the first item in the array, but I cant figure out how I would go about getting to the 2nd, 3rd, 4th, etc items in the array using a recursive function and then once I get down to the one int how would I go about comparing that int to another int in the array.

A recursive function definition has one or more base cases, data is a array of integers SORTED in ASCENDING order, toFind is the integer to search for, start is the minimum array index, end is the maximum array index OUTPUT: recursive step: otherwise, check value of current node, return true if match, otherwise recurse on children.

C program to find the maximum or the largest element present in an array. It also prints the location or index at which maximum element occurs in the array. The algorithm to find maximum is: first we assume that maximum element occurs at the beginning of the array and stores that value in a variable.

Write a function for each of the following in C programming language 1. Write a recursive function called sum_primes() that accepts an unsigned integer, n, as an argument, and returns the sum of all primes from 2 to n. C Program to Find Factorial of a Number Using Recursion. Example to find factorial of a non-negative integer (entered by the user) using recursion. function from the same function (recursive call).

In each recursive call, the value of argument n is decreased by 1. Find maximum value of Sum(i*arr[i]) with only rotations on given array allowed Rearrange positive and negative numbers using inbuilt sort function: Rearrange array such that even positioned are greater than odd: Explore more Order Statistics: Program to find sum of elements in a given array.

c++ - Finding the maximum value in a binary tree - Code Review Stack Exchange