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
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...
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...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static...
Take a look at the file GenericMethods.java. There are three methods you must implement: ·public static <T extends Comparable<T>> int findMin(T[] arr): Iterate through the array to find the index of the smallest element in the array. If there are two elements with the smallest value, the method should return the index of the first one. The method should run in O(n). ·public static <T extends Comparable<T>> int findMinRecursive(T[] arr): Should behave just like findMin, but should be implemented recursively....
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...