#include<iostream>
using namespace std;
int main()
{
int x, n;
cout << "Enter x: ";
cin >> x;
<< "Enter Number of array elements: ";
cin >> n;
cout << "Enter elements of array:" << endl;
int* arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
if (x == arr[0])
{
arr[0] = arr[n - 1];
arr[n - 1] = x;
cout << "Array after swapping first and last elements: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
else cout << "Error";
delete[] arr;
return 0;
}
#include <iostream>
#include <vector>
class Data
{
private:
std::vector<int> elements;
std::size_t N;
public:
explicit Data(const std::size_t size)
: N{size}
{
elements.reserve(size);
elements.resize(size);
}
void fill()
{
std::cout << "Enter elements of array:" << std::endl;
auto index = std::size_t{};
for (auto &elem : elements)
std::cin >> elements.at(index++);
}
void process(const int x)
{
if (x == elements.at(0))
{
elements.at(0) = elements.at(N - 1);
elements.at(N - 1) = x;
std::cout << "Array after swapping first and last elements: ";
for (const auto &elem : elements)
std::cout << elem << " ";
}
else
std::cout << "Error";
}
};
int main()
{
auto x = int{};
auto n = std::size_t{};
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter Number of array elements: ";
std::cin >> n;
auto data = Data{n};
data.fill();
data.process(x);
return 0;
}