7-3(C++ coding) Write a function name swapFrontBack that takes as input an array
ID: 3838831 • Letter: 7
Question
7-3(C++ coding)
Write a function name 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 (algorithm needs to be swapped front and Back, not front and middle.)
Additional Notes :
-No global variables may be used in any of our assignments. If I see a global variable being declared, a zero points will be assigned.
Clarification:
-While variables may not be global, all functions and constants should be declared global.
-Beginning from Chapter 4, all assignment must contain functions, even if it is possible to do without one.
-If the assignment calls for just a function, you must code a driver to test the function. The driver and the function both must be submitted.
Explanation / Answer
Code:
// Program to swap first and last elements of the array
#include <iostream>
using namespace std;
// function declaration
void swapFrontBack(int[], int);
int main()
{
// variable declaration
int n, arr[100];
// Taking input from user on number of elements
cout << "Enter how many elements? ";
cin >> n;
cout << "Please start entering the elements"<<endl;
// Picking array elements as input
for(int i = 0; i < n; i++)
cin >> arr[i];
cout <<"Given array elements are: ";
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
swapFrontBack(arr, n);
cout<<endl;
cout << "Given array after swapping front and back is: ";
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// function to swap array elements front and back
void swapFrontBack(int a[], int n)
{
int temp;
if(n==0) {
cout << " ARRAY IS EMPTY"<<endl;
exit(1);
}
else if(n==1) {
cout << " ARRAY HAS ONLY ELEMENT, SO SWAP CANNOT BE PERFORMED"<<endl;
exit(1);
}
// swapping first and last element
temp = a[n-1];
a[n-1] = a[0];
a[0] = temp;
}
Execution and output:
Enter how many elements? 0
Please start entering the elements
Given array elements are:
ARRAY IS EMPTY
Enter how many elements? 1
Please start entering the elements
100
Given array elements are: 100
ARRAY HAS ONLY ELEMENT, SO SWAP CANNOT BE PERFORMED
Enter how many elements? 2
Please start entering the elements
100
1000
Given array elements are: 100 1000
Given array after swapping front and back is: 1000 100
Enter how many elements? 6
Please start entering the elements
10
20
30
4
5
6
Given array elements are: 10 20 30 4 5 6
Given array after swapping front and back is: 6 20 30 4 5 10
Enter how many elements? 7
Please start entering the elements
1
2
3
4
5
10
20
Given array elements are: 1 2 3 4 5 10 20
Given array after swapping front and back is: 20 2 3 4 5 10 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.