In C programing write a function IntCopyArray which takes tw
In C programing write a function
IntCopyArray which takes two integer arrays as arguments and an integer n, and then copies the first n values from the first array into the second array so that when it is finished the first n elements of both arrays are the same. Note that this function does not need to return anything since arrays are passed as pointers and a change made to the array in a function changes the array and is visible anywhere that array is accessed (an array is not defined by the name, but what the name points to). The arrays must be declared as n or larger
Solution
// C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void IntCopyArray(int *array1, int *array2, int n)
{
int i, j;
// copies the first n values from the first array into the second array
for (i = 0; i < n; i++)
{
// copy
array2[i] = array1[i];
}
}
int main()
{
int array1[] = {4,1,5,2,3,7,8};
int array2[] = {3,6,2,7,7,0,10};
int size1 = sizeof(array1)/sizeof(array1[0]);
int size2 = sizeof(array2)/sizeof(array2[0]);
int i,n = 5;
IntCopyArray(array1,array2,n);
printf(\"array1: \ \");
for (i = 0; i < size1; ++i)
{
printf(\"%d \",array1[i]);
}
printf(\"\ \");
printf(\"array2: \ \");
for (i = 0; i < size2; ++i)
{
printf(\"%d \",array2[i]);
}
printf(\"\ \");
return 0;
}
/*
output:
array1:
4 1 5 2 3 7 8
array2:
4 1 5 2 3 0 10
*/
