Write a C++ program to swap first and last element of an integer 1-d array.
Program Code:
#include<iostream>
using namespace std;
int main()
{
int array[25];
int number;
int i, j, temp;
cout<<"Enter number of elements you want to insert into given array : ";
cin>>number;
cout<<endl;
for(i=0;i<number;i++)
{
cout<<"Element "<<i+1<<":";
cin>>array[i];
cout<<endl;
}
temp = array[0];
array[0] = array[number - 1];
array[number - 1]= temp;
cout<<"Array after swapping its first and last element is :"<<endl;
for(j=0;j<number;j++)
{
cout<<array[j]<<" ";
}
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter number of elements you want to insert into given array : 5
Element 1: 15
Element 2: 14
Element 3: 22
Element 4: 34
Element 5: 55
Array after swapping its first and last element is :
55 14 22 34 15
Thanks
Mukesh Rajput
wrong code
ReplyDelete