Write a program that swaps the elements of an array pairwise. Start from the lef
ID: 3534121 • Letter: W
Question
Write a program that swaps the elements of an array pairwise. Start from the left
of the array, take 2 elements at a time and swap the two elements. Continue in this fashion
until you reach the end of the array. Declare an integer array of size 10 and place random
numbers in the range [0-9]. Print the original array, pairwise swap the elements of the array
and print the final array after swap. Consider the following example.
0 1 2 3 4 5 6 7 8 9
1 7 4 0 9 4 8 8 2 4
After pairwise swap you get the following array
0 1 2 3 4 5 6 7 8 9
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
void main()
{
int arr[10];
int i,temp;
for(i=0;i<10;i++)
arr[i]=rand();
printf("Original array: ");
for(i=0;i<10;i++)
printf("%d ",arr[i]);
printf(" ");
for(i=0;i<9;i++)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
printf("array after swap operation: ");
for(i=0;i<10;i++)
printf("%d ",arr[i]);
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.