In: Computer Science
/**Programming in C**/
Write a appendArray() function, whose parameters include two one-dimensional arrays of integers, arr1[] and arr2[], with sizes size1 and size2, respectively. The function should append all of arr2 to the end of arr1 and return the updated size of arr1. int appendArray(int arr1[], int arr2[], int size1, int size2){
}
Are there any restrictions to the usability of your function when called from main() that should be included with the description of the function in the comments?
check out the solution and do comment if any queries.
-------------------------------------------------
#include<stdio.h>
#include<conio.h>
// function definition
// i think there are no restricions to call this function from
'main' as long as the type of variables remains as 'int'
int appendArray(int arr1[], int arr2[], int size1, int size2)
{
// declaration
int i, j;
// loop starts from the end of arr1 and start of arr2
for(i=size1, j=0; j<size2; i++, j++)
{
// append arr2 elements to the end of arr1
arr1[i] = arr2[j];
}
// print the updated arr1 elements
// and also 'i' contains the size of updated arr1 as it increments
to get next arr1 element
printf("\nUpdated arr1 elements : \n\n");
for(i=0; i<size1+size2; i++)
{
printf("%d\t", arr1[i], i);
}
// returns the result as long as 'i' looped through arr1 to print
updated arr1
return i;
}
// main function
int main()
{
// declaration
int size1, size2, size;
// initialization
size1 = 5;
size2 = 3;
int arr1[] = {1, 2, 3, 5, 6};
int arr2[] = {4, 5, 6};
// function call
size = appendArray(arr1, arr2, size1, size2);
// prints the updated arr1 size
printf("\n\n\nUpdated size of arr1 is : %d", size);
getch(); // optional statement
return 0;
}
// main ends
-------------------------------------------------------------
-----------------------------------------------
OUTPUT ::