Question

Question Recursive Maximum: In this question you must develop and use a recurive function to find...

 
Question
Recursive Maximum: In this question you must develop and use a recurive function to find the maximum value in array of integers. 
Complete the C program, Maximum.c, by implementing the recursive function maximumValue, and the regular function max, and completing the main function using the comments provided. Open the file Maximum.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. 
Note that the function max is not recursive. It only receives two integers and returns the one that is greater than or equal to the other one. 
However, function maximumValue is recursive. If it receives an array with only one element, i.e. base case, it should just return the value of that element. Otherwise, it should return the maximum of the first element and the maximum of the rest of the elements in the array
 
 
 
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define SIZE 20
 
// A regular function to check and return the maximum value between two integers
int max(int a, int b);
 
// A recursive function for recursively find and return the maximum value in an array of integers
int maximumValue(int a[], int size);
 
int main(void) {
 
        srand(time(NULL));
 
        int myArray[SIZE];
 
        // COMPLETE THIS PART
        // ******************
        // populate the array with positive random integers less than 100
 
        // COMPLETE THIS PART
        // ******************
        // Print out the elements of the array in one line
 
        // COMPLETE THIS PART
        // ******************
        // Find and print out the maximum value in the array by calling the recursive function maximumValue
 
}
 
int max(int a, int b) {
 
        // COMPLETE THIS PART
        // ******************
        // if a is greater than or equal to b, return a, otherwise return b
}
 
int maximumValue(int a[], int size) {
        // COMPLETE THIS PART
        // ******************
        // Base case and recursive part using an if-else statement.
        // Base case:
        //             If there is only one element in the current array, return it.
        // Recursive part:
        //             Call the max function with two parameters, the first element of the array and maximumValue of the rest of the array.
 
}

Homework Answers

Answer #1

Find the code for the above question below, read the comments provided in the code for better understanding. If found helpful please do upvote this.
Please refer to the screenshot of the code to understand the indentation of the code.

Copayble Code

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define SIZE 20

// A regular function to check and return the maximum value between two integers

int max(int a, int b);

// A recursive function for recursively find and return the maximum value in an array of integers

int maximumValue(int a[], int size);

int main(void)

{

    srand(time(NULL));

    int myArray[SIZE];

    // COMPLETE THIS PART

    // ******************

    // populate the array with positive random integers less than 100

    for (int i = 0; i < SIZE; i++)

    {

        myArray[i] = (rand() % (99 - 0 + 1)) + 0;

    }

    // COMPLETE THIS PART

    // ******************

    // Print out the elements of the array in one line

    printf("\nArray Contenets : ");

    for (int i = 0; i < SIZE; i++)

    {

        printf("%d ", myArray[i]);

    }

    // COMPLETE THIS PART

    // ******************

    // Find and print out the maximum value in the array by calling the recursive function maximumValue

    int maxval = maximumValue(myArray, SIZE);

    printf("\nMaximum value : %d", maxval);

}

int max(int a, int b)

{

    // if a is greater than or equal to b, return a, otherwise return b

    if (a > b)

        return a;

    return b;

}

int maximumValue(int a[], int size)

{

    // COMPLETE THIS PART

    // ******************

    // Base case and recursive part using an if-else statement.

    // Base case:

    //             If there is only one element in the current array, return it.

    // Recursive part:

    //             Call the max function with two parameters, the firast element of the array and maximumValue of the rest of the array.

    if (size == 1)

    {

        return a[0];

    }

    return max(a[size - 1], maximumValue(a, size - 1));

}

Screenshot of code

Output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In c++ Implement the following recursive function, that returns true if all elements in the given...
In c++ Implement the following recursive function, that returns true if all elements in the given array are smaller than the given value, otherwise false. Return false for the empty array and NULL array cases. bool all_smaller_r(int *a1, int size, int value) { }
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in...
Design ONE FUNCTION in a C++ code to find minimum, maximum and average of items in an array, then place them proper locations in the array. Follow these steps: 1. Create an array with 11 integers, which will be randomly selected from the range of 10 to 100. Only random numbers between 10 and 100 are allowed in the array. Print the items of the array on screen as one line. 2. Develop a function that takes the array as...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT