In: Computer Science
Computer Science Question 1: DO NOT USE ANY NON-STANDARD LIBRARY. o All the required libraries have already been included. O DO NOT INCLUDE ANY OTHER LIBRARY INTO THE CODE. o DO NOT ALTER THE NAMES OF THE C FILES PROVIDED. o DO NOT ALTER THE NAMES AND PROTOTYPES OF THE FUNCTIONS.DO NOT ALTER ANY OF THE CODE! JUST ADD WHAT NEEDS TO BE ADDED FOR IT TO WORK! Please copy and paste the whole solution. Please read the code below and complete what it is asking. This is the 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 // 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.
Below is the solution with output screenshot
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
int r;
for(int i=0;i<SIZE;i++)
{
r = rand() % 100;
myArray[i] = r;
}
// COMPLETE THIS PART
// ******************
// Print out the elements of the array in one line
printf("myArray = [ ");
for(int i=0;i<SIZE;i++)
{
printf("%d, ",myArray[i]);
}
printf("]\n");
// COMPLETE THIS PART
// ******************
// Find and print out the maximum value in the array by calling the recursive function maximumValue
int max_val = maximumValue(myArray,SIZE);
printf("\nMax Value in array is : %d",max_val);
}
int max(int a, int b) {
// COMPLETE THIS PART
// ******************
// if a is greater than or equal to b, return a, otherwise return b
if(a>b)
return a;
else
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.
if(size == 1)
return a[size-1];
return max(a[size-1], maximumValue(a,size-1));
}
Output :