Write a C program to swap two arrays using pointers.
#include <stdio.h>
#include <stdlib.h>
/* swaps two arrays using pointers */
void swapTwoArrays(int *arr1, int *arr2, int n) {
int i, temp;
for (i = 0; i < n; i++) {
temp = *(arr1 + i);
*(arr1 + i) = *(arr2 + i);
*(arr2 + i) = temp;
}
return;
}
int main() {
int *arr1, *arr2, i, j, n;
/* get the order of the arrays from the user */
printf("Enter the order of the arrays:");
scanf("%d", &n);
/* dynamically allocate memory to store elements */
arr1 = (int *) malloc(sizeof(int) * n);
arr2 = (int *) malloc(sizeof(int) * n);
/* input elements for first array */
printf("Enter data for first array:\n");
for (i = 0; i < n; i++) {
printf("Array1[%d] : ", i);
scanf("%d", (arr1 + i));
}
/* input elements for second array */
printf("Enter data for second array:\n");
for (i = 0; i < n; i++) {
printf("Array2[%d] :", i);
scanf("%d", arr2 + i);
}
/* swap the elements in the given arrays */
swapTwoArrays(arr1, arr2, n);
/* print the elements in the first array */
printf("Elements in first array:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(arr1 + i));
}
printf("\n");
/* print the elements in the second array */
printf("Elements in second array:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(arr2 + i));
}
printf("\n");
return 0;
}
#include <stdlib.h>
/* swaps two arrays using pointers */
void swapTwoArrays(int *arr1, int *arr2, int n) {
int i, temp;
for (i = 0; i < n; i++) {
temp = *(arr1 + i);
*(arr1 + i) = *(arr2 + i);
*(arr2 + i) = temp;
}
return;
}
int main() {
int *arr1, *arr2, i, j, n;
/* get the order of the arrays from the user */
printf("Enter the order of the arrays:");
scanf("%d", &n);
/* dynamically allocate memory to store elements */
arr1 = (int *) malloc(sizeof(int) * n);
arr2 = (int *) malloc(sizeof(int) * n);
/* input elements for first array */
printf("Enter data for first array:\n");
for (i = 0; i < n; i++) {
printf("Array1[%d] : ", i);
scanf("%d", (arr1 + i));
}
printf("Enter data for second array:\n");
for (i = 0; i < n; i++) {
printf("Array2[%d] :", i);
scanf("%d", arr2 + i);
}
/* swap the elements in the given arrays */
swapTwoArrays(arr1, arr2, n);
/* print the elements in the first array */
printf("Elements in first array:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(arr1 + i));
}
printf("\n");
/* print the elements in the second array */
printf("Elements in second array:\n");
for (i = 0; i < n; i++) {
printf("%d ", *(arr2 + i));
}
printf("\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the order of the arrays:4
Enter data for first array:
Array1[0] : 10
Array1[1] : 20
Array1[2] : 30
Array1[3] : 40
Enter data for second array:
Array2[0] :110
Array2[1] :120
Array2[2] :130
Array2[3] :140
Elements in first array:
110 120 130 140
Elements in second array:
10 20 30 40
Enter the order of the arrays:4
Enter data for first array:
Array1[0] : 10
Array1[1] : 20
Array1[2] : 30
Array1[3] : 40
Enter data for second array:
Array2[0] :110
Array2[1] :120
Array2[2] :130
Array2[3] :140
Elements in first array:
110 120 130 140
Elements in second array:
10 20 30 40
No comments:
Post a Comment