Question: *Use C (Pls indicate which is libmaxarray.h and libmaxarray.c) Write a function that takes an array and its size as inputs, and then finds (i)

*Use C (Pls indicate which is libmaxarray.h and libmaxarray.c)

Write a function that takes an array and its size as inputs, and then finds (i) the maximum value in the array and (ii) the index of the maximum values first occurrence. For example, if the array contains the values (2, 3, 8, 6, 8), then the maximum element of the array will be 8, and the index of the first occurrence of 8 is 2 (indices start from 0). To find the maximum element of the array and the index, you must make a function with the following prototype:

 void maxElem(int array[], int size, int *maxValPtr, int *maxIdxPtr); 

Note that this function takes an integer array as its first argument, the arrays size as its second argument, and two integer pointer arguments. The pointers maxValPtr and maxIdxPtr point to integer variables in the caller. After calling maxElem, the callers integer variables should be updated with the maximum value in the array and the index of the first occurrence of the maximum value in the array, respectively. This problem demonstrates that variables defined in the caller can be changed in callee functions by using pointers.

Note: You must write your function prototype in a header file named libmaxarray.h and you must write your function definition in a source file named libmaxarray.c. We are pro- viding you the main source file maxarray.c, which you can use to test your function. Do not change anything in maxarray.c.

(maxxarray.c)

#include #include #include "libmaxarray.h"

void getArr(int arr[], int arrSize);

int main(void) { int len, i, maxVal, maxIdx; int *arrayIn;

/* get input */ printf("Input the array size: "); scanf("%d", &len); if (len < 1) { printf("Array size must be 1 or larger. "); return 0; }

/* dynamic memory allocation */ arrayIn = (int*) malloc(len * sizeof(int));

/* read array from stdin */ getArr(arrayIn, len);

/* determine maxVal and maxIdx */ maxElem(arrayIn, len, &maxVal, &maxIdx);

/* print result */ printf("The maximum element of the array is %d. ", maxVal); printf("The index of the first occurence of the maximum element is %d. ", maxIdx);

/* free dynamically allocated memory */ free(arrayIn);

return 0; }

void getArr(int arr[], int arrSize) { int i;

if (arrSize == 1) printf("Input INT1: "); else if (arrSize == 2) printf("Input INT1 INT2: "); else printf("Input INT1 INT2 ... INT%d: ", arrSize);

for (i = 0; i < arrSize; i++) { scanf("%d", &arr[i]); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!