Array Play in C Programming 1 Write a C function that inputs
Array Play in C Programming:
1. Write a C function that inputs a pointer to an array of ints and returns the maximum value in the array. Of course, some elements may be negative and there might be repeated elements. It might be that all elements are the same. If length is zero or less, return a zero. Write the function as a pure function. Of course, this function does not change the values in the array. Use subscripted variables in the function (brackets and indexes.) There is, of course, no need to sort the array.
Your function should follow this prototype:
int arrayMax(int array[], int length);
2. Write a function does the same as the above, but explicitly uses pointers (uses the dereferencing operator * to get to cells.) Do this by using pointer arithmetic (adding a loop counter to the base address). This should be a translation of the above function into different syntax. Don\'t use [] to access array elements.
Your function should follow this prototype:
int arrayMaxP(int *array, int length);
3. Write a function that finds the maximum and the minimum of an array of doubles. As above, some elements may be negative and there might be repeated elements. It might be that all elements are the same, in which case the max and min are the same value.
If length is one, max and min are the same value. If length is less than one, return 0 for the function and leave the caller’s variables unchanged. Otherwise, return 1 as the function value and use the pointers min and max to change the caller’s variables.
Write the function using any of the three methods you want. Write this as a pure function (so, no globals, no changes made to the array.)
Your function should follow this prototype:
int arrayMinMax(double array[], int length, double *min, double *max);
NOTE: Correctness will be tested with a number of test cases. I will use my own main() function for testing your functions. It will test some “boundary conditions” as well as the general cases.
Solution
// C code demostrating function to determine minimum and maximum element in array using pointer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Function to find highest (maximum) value in array
int arrayMax(int array[], int length)
{
int max = array[0]; // start with max = first element
for(int i = 1; i<length; i++)
{
if(array[i] > max)
max = array[i];
}
return max; // return highest value in array
}
int arrayMaxP(int *array, int length)
{
int max = *(array+0); // start with max = first element
for(int i = 1; i<length; i++)
{
if(*(array+i) > max)
max = *(array+i);
}
return max; // return highest value in array
}
int arrayMinMax(double array[], int length, double *min, double *max)
{
if(length < 1)
return 0;
*max = array[0]; // start with max = first element
*min = array[0]; // start with max = first element
for(int i = 1; i<length; i++)
{
if(array[i] > *max)
*max = array[i];
if(array[i] < *min)
*min = array[i];
}
return 1;
}
int main ()
{
int i;
srand(time(0));
int length = 10;
int array[length];
for(i=0; i<length; i++)
{
array[i] = (rand()%5000)-2500;
}
printf(\"Array: \");
for (i = 0; i < length; ++i)
{
printf(\"%d \",array[i]);
}
printf(\"\ \");
int maximum = arrayMax(array,length);
printf(\"Maximum using arrayMax function: %d\ \" ,maximum);
maximum = arrayMaxP(array,length);
printf(\"Maximum using arrayMaxP function: %d\ \", maximum);
double arr[length];
for(i=0; i<length; i++)
{
arr[i] = (rand()%5000)-2500;
}
double max = 0;
double min = 0;
int result = arrayMinMax(arr,length,&min,&max);
printf(\"\ Array: \");
for (i = 0; i < length; ++i)
{
printf(\"%lf \",arr[i]);
}
printf(\"\ Maximum using arrayMinMax function: %lf\ \", max);
printf(\"Minimum using arrayMinMax function: %lf\ \", min);
return 0;
}
/*
output:
Array: -60 1147 551 323 -2061 357 -1983 1506 2003 -523
Maximum using arrayMax function: 2003
Maximum using arrayMaxP function: 2003
Array: 16.000000 1225.000000 1294.000000 -1612.000000 -67.000000 -142.000000 2193.000000 -2313.000000 -1567.000000 1627.000000
Maximum using arrayMinMax function: 2193.000000
Minimum using arrayMinMax function: -2313.000000
*/


