The program is to be compiled on C Write a program that swaps the elements of an
ID: 3533751 • Letter: T
Question
The program is to be compiled on C
Write a program that swaps the elements of an array pairwise. Start from 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. After pairwise swap you get the following array In the array first two numbers 1 and 7 are swapped, 4 and 0 are swapped, 8 and 8 swapped and 2 and 4 are swapped. Sample execution of the program is given below.Explanation / Answer
#include<stdio.h>
void main()
{
int arr[10],i,temp;
printf("enter value in array ");
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
\ change position
for(i=0;i<10;i=i+2)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
printf(" change value in array is ");
for(i=0;i<10;i++)
printf("%d ",arr[i]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.