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.
}
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
Get Answers For Free
Most questions answered within 1 hours.