-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
55 lines (40 loc) · 1.1 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
int* arrayAdd(int *arr1, int *arr2, int size);
void swapArr(int** arr1, int** arr2);
int main() {
int size = 0;
printf("Input size: ");
scanf("%d", &size);
int* ptr1 = malloc(sizeof(int) * size);
int* ptr2 = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
ptr1[i] = rand()%21;
ptr2[i] = rand()%21;
}
int* sumArr = arrayAdd(ptr1, ptr2, size);
for(int i = 0; i < size; i++) printf("%d ",ptr1[i]);
printf("\n");
for(int i = 0; i < size; i++) printf("%d ",ptr2[i]);
printf("\n\n");
swapArr(&ptr1, &ptr2);
for(int i = 0; i < size; i++) printf("%d ",ptr1[i]);
printf("\n");
for(int i = 0; i < size; i++) printf("%d ",ptr2[i]);
printf("\n\n");
for(int i = 0; i < size; i++) printf("%d ",sumArr[i]);
return 0;
}
int* arrayAdd(int arr1[], int arr2[], int size) {
int* sumArr = malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
sumArr[i] = arr1[i] + arr2[i];
}
return sumArr;
}
void swapArr(int** arr1, int** arr2) {
int* temp;
temp = *arr1;
*arr1 = *arr2;
*arr2 = temp;
}