#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;
}