Must be written in C 89 Mode
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:
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);
#include <stdio.h>
int* insert_into_array(int arr[], size_t arr_len, int value, size_t pos){
if (pos < 0 || pos >= arr_len) {
printf("Index out of bounds\n");
return arr;
}
int i;
for (i=arr_len-1; ( i >pos); i--)
arr[i] = arr[i-1];
arr[pos] = value;
return arr;
}
void reserved_print_arr(int arr[], size_t arr_len){
for(int i=0 ; i<arr_len ; ++i){
printf("%d ", arr[i]);
}
printf("\n");
}
int main(void) {
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);
return 0;
}
=====================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Must be written in C 89 Mode Array Insertion Your task is to complete the implementation...
Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...
PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; } return count;...
PLEASE HELP ME WITH THIS TASK 3.1 (FLOWCHART) AND TASK 3.2 (SOURCE PROGRAM). THANK YOU! Hint: the number array in task 2 is work for task 3 in figure 1. Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count = 0; for (int i...
I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...
If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...
PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...
Given below is a partially completed C program, which you will use as a start to complete the given tasks. #define SIZE 9 #include <stdio.h> int compareAndCount (int[], int[]); double averageDifference (int[], int[]); void main() { } int compareAndCount(int arr1[SIZE], int arr2[SIZE]) { int count - @; for (int i = 0; i < SIZE; i++) { if (arri[i] > arr2[i]) count++; return count; Task 1: (10 pts) Show the design of the function compareAndCount() by writing a pseudocode. To...
The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...
I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...
Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example: multiply([ 1, 2, 3], 3) → 6 multiply([1, 1, 4 ,2], 4) → 8 multiply([7, 0, 0, 7, 0, 7],...