Write a function named swapFrontBack that takes as input an
Write a function named swapFrontBack that takes as input an array of integers and an integer that specifies how many entries are in the array. The function should swap the first element in the array with the last element in the array. The function should check if the array is empty to prevent errors. Test your function with arrays of different length and with varying front and back numbers
Solution
Here is code:
#include <iostream>
using namespace std;
void swapFrontBack(int array[],int size)
{
//checks array size is empty or not
if(size == 0)
{
cout << \"Array is empty\ \";
}
else
{
//swapping the array
int temp;
temp = array[size- 1];
array[size-1] = array[0] ;
array[0] = temp;
//prints the array
for(int i=0;i<5;i++)
{
cout << array[i]<< \" \";
}
cout << endl;
}
}
int main()
{
int arr[5] = {1000, 2, 3, 17, -55};
// call function with parameter as array and size
swapFrontBack(arr,5);
return 0;
}
Input array
Array is 1000, 2, 3, 17, -55
Output:
-55 2 3 17 1000
Input array:
Array is 10, 2, 3, 17, 50
Output:
50, 2, 3, 17, 10
Input array size is 0
output:
Array is empty

