Q:

# C program to swap elements of two integer arrays using user define function

Given two integer arrays and we have to swap their elements by creating our own function (User Define Function) using C program.

Note: Total number of elements of both arrays should be same.

Here is the function that we have used in the program,

`void swapElements(int *arr1 , int *arr2 , int n)`

## Program to swap elements of two integer arrays using user define function in C

``````/*
* C program to swap elements of two integer arrays
*/

#include <stdio.h>

// funtion to swap the elements of the two arrays
void swapElements(int *arr1 , int *arr2 , int n)
{
int i=0,temp=0;

for(i=0 ; i<n ; i++)
{
temp    = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}
}

// main function
int main()
{
int i=0;

// define two 1d-arrays
int array_1[6] = {0,1,2,3,4};
int array_2[6] = {5,6,7,8,9};

// Passing two arrays and the number of
// elements to Function
swapElements(array_1,array_2,5);

printf("\nThe arrays after swap are..\n");

for(i=0 ; i<5 ; i++)
{
printf("\narra_1 [%d] : %d",i,array_1[i]);
}

printf("\n");

for(i=0 ; i<5 ; i++)
{
printf("\narray_2 [%d] : %d",i,array_2[i]);
}

return 0;
}``````

Output

```The arrays after swap are..

arra_1 [0] : 5
arra_1 [1] : 6
arra_1 [2] : 7
arra_1 [3] : 8
arra_1 [4] : 9

array_2 [0] : 0
array_2 [1] : 1
array_2 [2] : 2
array_2 [3] : 3
array_2 [4] : 4```