#include <iostream>
#include <limits>
#include <string>
void enter2dArray()
{
int N, M;
std::cout << "N: ";
std::cin >> N;
if (std::cin.fail() || N < 1) {
std::cin.clear();
N = 1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "M: ";
std::cin >> M;
if (std::cin.fail() || N < 1 || M < 1) {
std::cin.clear();
M = 1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Sequence of " << N * M << " integers: ";
int **arr = new int *[N];
for (int i = 0; i < N; ++i) {
arr[i] = new int[M];
for (int j = 0; j < M; ++j) {
std::cin >> arr[i][j];
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(1, '\n');
arr[i][j] = 0;
}
}
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "2D Array:\n";
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
std::cout << arr[i][j] << " ";
if (j + 1 < M)
std::cout << " ";
else
std::cout << std::endl;
}
}
for (int i = 0; i < N; ++i)
delete[] arr[i];
delete[] arr;
}
void replaceDigits(unsigned a = 9, unsigned b = 0)
{
int num;
std::cout << "Num = ";
std::cin >> num;
if (std::cin.fail()) {
num = -919939189; // default value
std::cin.clear();
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string buf = std::to_string(num);
const std::string sa = std::to_string(a);
const std::string sb = std::to_string(b);
std::cout << "Num1 = " << num << std::endl;
std::cout << "Str1 = " << buf << std::endl;
for (std::string::size_type n = buf.find(sa); n != std::string::npos;
n = buf.find(sa, n))
buf.replace(n, sa.size(), sb);
num = std::stoi(buf);
std::cout << "Str2 = " << buf << std::endl; // if num2 is not valid
std::cout << "Num2 = " << num << std::endl;
}
void replaceElems()
{
int N;
std::cout << "N: ";
std::cin >> N;
if (std::cin.fail() || N < 10) {
std::cin.clear();
N = 10;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Series of " << N << " integers: ";
int *row = new int[N];
for (int i = 0; i < N; ++i) {
std::cin >> row[i];
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(1, '\n');
row[i] = -i;
}
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "New series:";
for (int i = 0; i < N; ++i) {
if (i % 2 == 1)
row[i] = row[i - 1];
std::cout << " " << row[i];
}
std::cout << std::endl;
delete[] row;
}
int main()
{
enter2dArray();
std::cout << std::endl;
replaceDigits();
std::cout << std::endl;
replaceElems();
std::cout << std::endl;
}
Comments
Leave a comment