*Use C (Pls indicate which is libmaxarray.h and libmaxarray.c) Write a function
ID: 3860948 • Letter: #
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) the maximum value in the array and (ii) the index of the maximum value’s 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:
Note that this function takes an integer array as its first argument, the array’s 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 caller’s 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<stdio.h>
#include<stdlib.h>
#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]);
}
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
//#include "libmaxarray.h"
void getArr(int arr[], int arrSize);
void maxElem(int array[], int size, int *maxValPtr, int *maxIdxPtr);
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 maxElem(int array[], int size, int *maxValPtr, int *maxIdxPtr){
int i= 0;
*maxValPtr = array[0];
*maxIdxPtr = 0;
for(i=0; i<size; i++){
if(*maxValPtr < array[i]){
*maxValPtr = array[i];
*maxIdxPtr = i;
}
}
}
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]);
}
}
Output:
sh-4.2$ main
Input the array size:
10
Input INT1 INT2 ... INT10:
4
5
7
10
1
2
3
6
9
8
The maximum element of the array is 10.
The index of the first occurence of the maximum element is 3.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.