Array Insertion Your task is to complete the implementation of the insertion function, insert_into_array:
int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos);
The insertion function inserts a value into an array at a specified position. All elements to the right of the inserted element are shifted over a position (upon inserting, all elements at the insertion position are shifted up an index, and the last element in the array is overwritten by the previous element). For instance, if we have the following array and insert the value 5 at position 2:
Before (top row indicates index):
0 1 2 3 4 5 6
------------------------------
| 3 | 1 | 0 | 9 | 11 | 7 | 6 |
------------------------------
After (top row indicates index):
0 1 2 3 4 5 6
------------------------------
| 3 | 1 | 5 | 0 | 9 | 11 | 7 |
------------------------------
The insertion function takes in 4 arguments: an array of integers, the number of elements in the array, an integer to insert, and a position to insert. The insertion function should return the same array that was passed into the array.
Other considerations:
size_t arr_len;
int *result;
printf("\nTest case 1\n");
int arr1[] = {3, 4, 5, 6};
arr_len = sizeof(arr1) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr1, arr_len);
result = insert_into_array(arr1, arr_len, 6, 2);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 2\n");
int arr2[] = {3, 4, 5, 6, 86, 10, 123, 134};
arr_len = sizeof(arr2) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr2, arr_len);
result = insert_into_array(arr2, arr_len, 6, 2);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 3\n");
int arr3[] = {3, 4, 5, 6, 86, 10, 123, 134};
arr_len = sizeof(arr3) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr3, arr_len);
result = insert_into_array(arr3, arr_len, 6, 0);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 4\n");
int arr4[] = {3, 4, 5, 6, 86, 10, 123, 134};
arr_len = sizeof(arr4) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr4, arr_len);
result = insert_into_array(arr4, arr_len, 6, arr_len-1);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 5\n");
int arr5[] = {3, 4, 5, 6, 86, 10, 123, 134};
arr_len = sizeof(arr5) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr5, arr_len);
result = insert_into_array(arr5, arr_len, 6, 15);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 6\n");
int arr6[] = {3, 4, 5, 6, 86, 10, 123, 134};
arr_len = sizeof(arr6) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr6, arr_len);
result = insert_into_array(arr6, arr_len, 6, -10);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
printf("\nTest case 7\n");
int arr7[] = {3};
arr_len = sizeof(arr7) / sizeof(int);
printf("Before insertion:\n");
reserved_print_arr(arr7, arr_len);
result = insert_into_array(arr7, arr_len, 6, 0);
printf("After insertion:\n");
reserved_print_arr(result, arr_len);
Get Free Quote!
370 Experts Online