1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <algorithm>
#include <iostream>
template <size_t N>
void setArray(int (&data)[N], int d = 0);
template<size_t N>
void fillArray(int (&data)[N]);
template<size_t N>
void doubleArray(const int (&datain)[N], int (&dataout)[N]);
template<size_t N>
void print1DArray(const int (&data)[N]);
template<size_t N, size_t M>
void print2DArray(const int(&data)[N][M]);
template <size_t N, size_t O, size_t P>
void copyTwoArrays(const int(&arr1)[N], const int(&arr2)[N], int(&out)[O][P]);
int main() {
int arrayData[10][4];
int array1[20];
int array2[20];
const int array3[4] { 11, 13, 15, 17 };
print1DArray(array3);
// a
setArray(array2);
// b
fillArray(array1);
print1DArray(array1);
// c
doubleArray(array1, array2);
print1DArray(array2);
// d
copyTwoArrays(array1, array2, arrayData);
print2DArray(arrayData);
}
template <size_t N>
void setArray(int (&data)[N], int d) {
std::ranges::fill(data, d);
}
template<size_t N>
void fillArray(int (&data)[N]) {
std::cout << "Please enter " << N << " integers: ";
for (auto& n : data)
std::cin >> n;
}
template<size_t N>
void print1DArray(const int (&data)[N]){
for (const auto& n : data)
std::cout << n << ' ';
std::cout << '\n';
}
template<size_t N>
void doubleArray(const int (&datain)[N], int (&dataout)[N]) {
for (size_t i {}; i < N; ++i)
dataout[i] = datain[i] << 1;
}
template <size_t N, size_t O, size_t P>
void copyTwoArrays(const int(&arr1)[N], const int(&arr2)[N], int(&out)[O][P]) {
static_assert(N * 2 == O * P);
auto p {&out[0][0]};
for (const auto& n : arr1)
*p++ = n;
for (const auto& n : arr2)
*p++ = n;
}
template<size_t N, size_t M>
void print2DArray(const int(&data)[N][M]) {
for (const auto& r : data)
print1DArray(r);
}
|