Four methods for exchanging content between two equal-size arrays

Source: Internet
Author: User

Method One:

This method mainly uses logical operations to exchange each element of the array, and then swaps the entire array element with a for loop
#include <stdio.h>
int main ()
{int i,j,k;
int a[10];
int b[10];
int c[10];
printf ("Please enter the contents of a array: \ n");
for (i=0;i<10;i++)
{scanf ("%d", &a[i]);}
printf ("Please enter the contents of the B array: \ n");
for (j=0;j<10;j++)
{scanf ("%d", &b[j]);}
for (k=0;k<10;k++)
{A[k]=a[k]^b[k]; Logical operations exchange The values of each element, "^" can also be changed to "+,_" can also be exchanged
B[K]=A[K]^B[K]; Example: A[k]=a[k]+b[k];
A[K]=A[K]^B[K]; B[K]=A[K]-B[K];

} A[k]=a[k]-b[k];


printf ("An array after interchange is: \ n");
for (i=0;i<10;i++)
{printf ("%d", A[i]);}
printf ("\ n");
printf ("B array after interchange: \ n");
for (j=0;j<10;j++)
{printf ("%d", B[j]);}
printf ("\ n");
return 0;
}
Method Two://This method is to use the pointer function to exchange the values of each element,
#include <stdio.h>
int main ()
{int i,j,k;
int a[10];
int b[10];
int c[10];
printf ("Please enter the contents of a array: \ n");
for (i=0;i<10;i++)
{scanf ("%d", &a[i]);}
printf ("Please enter the contents of the B array: \ n");
for (j=0;j<10;j++)
{scanf ("%d", &b[j]);}
for (k=0;k<10;k++)
{swap (&a[k],&b[k]); Call pointer function to exchange elements
}
printf ("An array after interchange is: \ n");
for (i=0;i<10;i++)
{printf ("%d", A[i]);}
printf ("\ n");
printf ("B array after interchange: \ n");
for (j=0;j<10;j++)
{printf ("%d", B[j]);}
printf ("\ n");
return 0;
}


int swap (int *a,int* b)
{int t;
T=*a;
*a=*b;
*b=t;
}
Method three//introduce a third array to exchange the elements of the array
#include <stdio.h>
int main ()
{int i,j,k;
int a[10];
int b[10];
int c[10];
printf ("Please enter the contents of a array: \ n");
for (i=0;i<10;i++)
{scanf ("%d", &a[i]);}
printf ("Please enter the contents of the B array: \ n");
for (j=0;j<10;j++)
{scanf ("%d", &b[j]);}

for (k=0;k<10;k++)//introduce variable, swap
{
C[K]=A[K];
}
for (i=0;i<10;i++)
{
A[i]=b[i];
}
for (j=0;j<10;j++)
{
B[J]=C[J];
}
printf ("An array after interchange is: \ n");
for (i=0;i<10;i++)
{printf ("%d", A[i]);}
printf ("\ n");
printf ("B array after interchange: \ n");
for (j=0;j<10;j++)
{printf ("%d", B[j]);}
printf ("\ n");
return 0;
}
Method four//introduce a third variable to exchange the elements of each array using the strcpy function
#include <stdio.h>
int main ()
{char str1[20];
Char str2[20];
Char str3[20];
Puts ("Please enter the str1 character: \ n");
Gets (STR1);
Puts ("Please enter the str2 character: \ n");
Gets (STR2);
strcpy (STR3,STR1);
strcpy (STR1,STR2);
strcpy (STR2,STR3);

Puts ("str1 array after interchange: \ n");
Puts (STR1);
printf ("\ n");
Puts ("str2 array after interchange: \ n");
Puts (STR2);
printf ("\ n");
return 0;
}


This article is from the "0-point Time" blog, please make sure to keep this source http://10741764.blog.51cto.com/10731764/1698474

Four methods for exchanging content between two equal-size arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.