-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41.c
63 lines (53 loc) · 1.57 KB
/
41.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Question : Write a program to swap two arrays using pointers.
#include <stdio.h>
void swapArrays(int *arr1, int *arr2, int n) {
int temp;
for (int i = 0; i < n; i++) {
temp = *(arr1 + i); // Store the value of arr1[i] in temp
*(arr1 + i) = *(arr2 + i); // Copy the value of arr2[i] to arr1[i]
*(arr2 + i) = temp; // Assign the value of temp to arr2[i]
}
}
int main() {
int n;
// Input the size of the arrays
printf("Enter the number of elements in the arrays: ");
scanf("%d", &n);
// Declare two arrays
int arr1[n], arr2[n];
// Input elements for the first array
printf("Enter the elements of the first array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
// Input elements for the second array
printf("Enter the elements of the second array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr2[i]);
}
// Print the arrays before swapping
printf("\nBefore swapping:\n");
printf("First array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
// Swap the arrays
swapArrays(arr1, arr2, n);
// Print the arrays after swapping
printf("\nAfter swapping:\n");
printf("First array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr1[i]);
}
printf("\nSecond array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
return 0;
}