A program to swap two single dimensional arrays
C Programming Language 056
The following code shows how to write a program to swap two single dimensional arrays (Interchange their elements).
Program
#include <stdio.h>
void main()
{
int a[5], b[5], temp, i, m, n;
printf("Enter the sizes of a,b: ");
scanf("%d %d", &m, &n);
printf("a: ");
for (i = 0; i < m; i++)
{
scanf("%d", &a[i]);
}
printf("b: ");
for (i = 0; i < n; i++)
{
scanf("%d", &b[i]);
}
for (i = 0; i < m + n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
printf("\na: ");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\nb: ");
for (i = 0; i < m; i++)
{
printf("%d ", b[i]);
}
printf("\n");
}
Result
Enter the sizes of a,b: 2 1
a: 3 7
b: 1
a: 1
b: 3 7
Last Updated on
Comments