The insertion function inserts a value into an array at a specified position.

computer science

Description

Project 1 - Array Insertion

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:

  • You are not allowed to modify the function signature of insert_into_array
  • You may assume arr_len is always properly set.
  • You must handle positions that are outside of the bounds of the array. If the position specified is out of bounds, simply print Index out of bounds(followed by a newline). In the event this happens, insert_into_array should return the array passed into the function without modifying any values.
  • Be careful about accidentally overwriting the wrong value in the array. Hint: the insertion should be the last modification you perform on the array.
  • I would recommend you do this operation in-place (meaning you don’t duplicate the array), though this is not required. If you do not do this operation in-place, you will have to worry about the scope of the modified array.
  • You may use the reserved_print_arr()function to print arrays. It takes two arguments and has the following signature: void reserved_print_arr(int arr[], size_t arr_len)

Sample testing driver code

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);


Related Questions in computer science category