Answer to Question #231661 in C++ for xyz

Question #231661

Write a program to swap the numbers in even position using call by reference


1
Expert's answer
2021-08-31T17:35:48-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}


int main()
{
	int n;
	cout<<"Enter size of array : ";
	cin>>n;
	
	int arr[n];
	cout<<"Enter Array elements : ";
	for(int i=0; i<n; i++)
	{
		cin>>arr[i];
	}
	
	for(int i=1; i<n; i++)
	{
		if(i%2 != 0)
		{
			swap(arr[i], arr[i-1]);
		}
	}
	
	cout<<"Array after swapping even position elements : ";
	for(int i=0; i<n; i++)
	{
		cout<<arr[i]<<" ";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS