Created
December 27, 2017 08:55
-
-
Save you74674/c7742b2ff47104b4d43e03b2044dd870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#include"swap.h" | |
void fake_swap(int a, int b) | |
{ | |
int tmp=a; | |
a=b; | |
b=tmp; | |
} | |
void fake_swap_array(int *A, int *B) | |
{ | |
int *tmp=A; | |
A=B; | |
B=tmp; | |
} | |
void print_array(int *a, int len) | |
{ | |
for(int i=0; i<len; i++) | |
printf("%d ", a[i]); | |
printf("\n"); | |
} | |
void print(int *A, int a, int *B, int b) | |
{ | |
printf("%d %d\n", a, b); | |
print_array(A, a); | |
print_array(B, b); | |
} | |
void read_array(int *a, int len) | |
{ | |
for(int i=0; i<len; i++) | |
scanf("%d", &(a[i])); | |
} | |
int main() | |
{ | |
int C; | |
scanf("%d", &C); | |
for(int c=0; c<C; c++) | |
{ | |
int a, b; | |
int *A, *B; | |
scanf("%d %d", &a, &b); | |
A=(int*)malloc(a*sizeof(int)); | |
B=(int*)malloc(b*sizeof(int)); | |
read_array(A, a); | |
read_array(B, b); | |
printf("original data: \n"); | |
print(A, a, B, b); | |
fake_swap(a, b); | |
fake_swap_array(A, B); | |
printf("data after fake swap: \n"); | |
print(A, a, B, b); | |
swap(&a, &b); | |
swap_array(&A, &B); | |
printf("data after true swap: \n"); | |
print(A, a, B, b); | |
free(A); | |
free(B); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment