Uncategorized

Discuss how to find the largest element in an array by thinking recursively. and Reply to these two peer posts

Please discuss how to find the largest element in an array by thinking recursively.&nbsp;and&nbsp;Reply to these two peer posts.&nbsp;From Ted&nbsp;The most logical way for me to find the largest element in an array is by thinking logically and developing an algorithm that would find exactly the former. For me that would involve using an algorithm that went through each arrayed item one at a time to determine the largest of the elements [i+1]. Something similar to: max( [], size, lg, i){if (i < size){if(lg <&nbsp;[i])max=arr[i];max(, size, lg, i+1);} max;}&nbsp;&nbsp;From Bill&nbsp;A function is recursive if it is called multiple times, to find the largest element in an arrayrecursively. We simply create a function that will call its self a number of times until the max is found. For instance with the example below the function calls itself four time seeking the largest number. &nbsp;Through searching the web there are multiple ways to work through this problem, however if dealing with a large amount of data this would not necessarily be an effective means.Chrisint maxArray(int myArray[5], int size){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (size == 1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return myArray[0];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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nbsp;&nbsp;&nbsp; int array[5] = { 5, 15, 25, 10, 20 };&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout << “The largest value is: “” << maxArray(array

Leave a Reply

Your email address will not be published. Required fields are marked *