+1 (315) 557-6473 

Pointer Function Assignment Solution with C.


The problems:


PS2.1: Largest and smallest

Implement the function with this signature:

void large_and_small(const int* array, int length, int* largest, int* smallest)

Call this function with an array of integer values. It should find the smallest and the largest values in this array and place them in the largest and smallest variables. The length parameter is the length of the array being passed -- calculate this length before passing it, don't hard-code it. Use pointer arithmetic, not subscripting, to complete with your C assignment. Print the result of the call.

Testing

Test your function with the following arrays:

[72,90,100,36,21,15,76,-6,63,41,99,27,3,66,19,16,27,47,0,23]

[-72,-90,-100,-36,-21,-15,-76,-6,63,41,99,27,3,66,19,16,27,47,0,23]

PS2.2: Inner product

Implement the function with this signature:

double inner_product(const double* leftArray, const double* right_array, int length)

This function takes two arrays of length length and returns the inner product ( a[0] * b[0] )+ ( a[1] * b[1] ) + ... + ( a[length -1] * b[length -1] ).

You may not use array subscripts, only pointer arithmetic. Print the result of the call. You should check that the lengths are the same before making the call.

Testing

Test your function with the following arrays:

left: [15.26, 28.20, 6.71, 72.22, 92.85, 15.50, 85.64, 57.41, 44.40, 72.91]

right: [9.17, 4.8, 10.9, 10.12, 17.18, 3.9, 6.19, 5.4, 3.13, 6.1]

PS2.3: Same or not same?

Implement the function with the following signature:

bool compareArrays(int* left, int left_length, int* right, int right_length)

Return true if the arrays have identical values or false if they do not. You should first test whether the arrays have equal length since if they don't, they can't be equal.

You may not use array subscripts, only pointer arithmetic. Print the result of the calls.

Testing

Test your function with the following arrays:

left: [72, 90,100,36,21,15,76,-6,63,41,99,27,3,66,19,16,27,47,0,23]

right: [72, 90,100,36,21,15,76,-6,63,41,99,27,3,66,18,16,27,47,0,23]

left: [72, 90,100,36,21,15,76,-6,63,41,99,27,3,66,19,16,27,47,0,23]

right: [72, 90,100,36,21,15,76,-6,63,41,99,27,3,66,18,16,27,47,23]

Solution:

1.

#include < stdio.h> #include "ps2.h" /* Test the functions */ int main(int argc, char *argv[]) { int largest, smallest; const int ps2_1_arr_1[] = { 72, 90, 100, 36, 21, 15, 76, -6, 63, 41, 99, 27, 3, 66, 19, 16, 27, 47, 0, 23 }; const int ps2_1_arr_2[] = { -72, -90, -100, -36, -21, -15, -76, -6, 63, 41, 99, 27, 3, 66, 19, 16, 27, 47, 0, 23 }; const double ps2_2_arr_left[] = { 15.26, 28.20, 6.71, 72.22, 92.85, 15.50, 85.64, 57.41, 44.40, 72.91 }; const double ps2_2_arr_right[] = { 9.17, 4.8, 10.9, 10.12, 17.18, 3.9, 6.19, 5.4, 3.13, 6.1 }; int ps2_3_arr_1_left[] = { 72, 90, 100, 36, 21, 15, 76, -6, 63, 41, 99, 27, 3, 66, 19, 16, 27, 47, 0, 23 }; int ps2_3_arr_1_right[] = { 72, 90, 100, 36, 21, 15, 76, -6, 63, 41, 99, 27, 3, 66, 18, 16, 27, 47, 0, 23 }; int ps2_3_arr_2_left[] = { 72, 90, 100, 36, 21, 15, 76, -6, 63, 41, 99, 27, 3, 66, 19, 16, 27, 47, 0, 23 }; int ps2_3_arr_2_right[] = { 72, 90, 100, 36, 21, 15, 76, -6, 63, 41, 99, 27, 3, 66, 18, 16, 27, 47, 23 }; /* Test the large and small */ printf("Testing large_and_small\n"); large_and_small(ps2_1_arr_1, 20, &largest, &smallest); printf("ps2_1_arr_1 largest: %d\n", largest); printf("ps2_1_arr_1 smallest: %d\n", smallest); large_and_small(ps2_1_arr_2, 20, &largest, &smallest); printf("ps2_2_arr_2 largest: %d\n", largest); printf("ps2_2_arr_2 smallest: %d\n", smallest); printf("\n"); /* Test the inner product */ printf("Testing inner_product\n"); printf("ps2_2_arr_left x ps2_2_arr_right: %lf\n", inner_product(ps2_2_arr_left, ps2_2_arr_right, 10)); printf("\n"); /* Test the compare arrays */ printf("Testing compareArrays\n"); printf("ps2_3_arr_1_left compare to ps2_3_arr_1_right: %s\n", compareArrays(ps2_3_arr_1_left, 20, ps2_3_arr_1_right, 20) ? "true" : "false"); printf("ps2_3_arr_2_left compare to ps2_3_arr_2_right: %s\n", compareArrays(ps2_3_arr_2_left, 20, ps2_3_arr_2_right, 19) ? "true" : "false"); return 0; }

2.

#include "ps2.h" /* Find the largest and smallest value in the array */ void large_and_small(const int *array, int length, int *largest, int *smallest) { int i; for (i = 0; i < length; i++) { if (i == 0 || *array > *largest) *largest = *array; if (i == 0 || *array < *smallest) *smallest = *array; array++; } } /* Multiply each element of both arrays then sum it up */ double inner_product(const double *left_array, const double *right_array, int length) { double result; int i; result = 0; for (i = 0; i < length; i++) { result += (*left_array) * (*right_array); left_array++; right_array++; } return result; } /* Return true if the arrys have identical values or false if they do not */ bool compareArrays(int *left, int left_length, int *right, int right_length) { int i; if (left_length != right_length) return false; for (i = 0; i < left_length; i++) { if (*left != *right) return false; left++; right++; } return true; }

Testing large_and_small

ps2_1_arr_1 largest: 100

ps2_1_arr_1 smallest: -6

ps2_2_arr_2 largest: 99

ps2_2_arr_2 smallest: -100

Testing inner_product

ps2_2_arr_left x ps2_2_arr_right: 4158.761200

Testing compareArrays

ps2_3_arr_1_left compare to ps2_3_arr_1_right: false

ps2_3_arr_2_left compare to ps2_3_arr_2_right: false