Добавить mergedarray.cpp
This commit is contained in:
63
mergedarray.cpp
Normal file
63
mergedarray.cpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void show(int* a, int n) {
|
||||||
|
for (int i = 0; i < n; ++i, ++a) {
|
||||||
|
cout << *a << " ";
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
int M, N;
|
||||||
|
cout << "Enter the size of array A: ";
|
||||||
|
if (!(cin >> M) || M <= 0) {
|
||||||
|
cerr << "Error: size A must be a positive number!\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Enter the size of array B: ";
|
||||||
|
if (!(cin >> N) || N <= 0) {
|
||||||
|
cerr << "Error: size B must be a positive number!\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* A = new int[M];
|
||||||
|
int* B = new int[N];
|
||||||
|
|
||||||
|
for (int i = 0; i < M; i++) {
|
||||||
|
A[i] = rand() % 101 - 50;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
B[i] = rand() % 101 - 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sizeC = M + N;
|
||||||
|
int* C = new int[sizeC];
|
||||||
|
|
||||||
|
int* pA = A;
|
||||||
|
int* pB = B;
|
||||||
|
int* pC = C;
|
||||||
|
|
||||||
|
for (int i = 0; i < M; i++, ++pA, ++pC) {
|
||||||
|
*pC = *pA;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < N; i++, ++pB, ++pC) {
|
||||||
|
*pC = *pB;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Array A: ";
|
||||||
|
show(A, M);
|
||||||
|
cout << "Array B: ";
|
||||||
|
show(B, N);
|
||||||
|
cout << "C merged array: ";
|
||||||
|
show(C, sizeC);
|
||||||
|
|
||||||
|
delete[] A;
|
||||||
|
delete[] B;
|
||||||
|
delete[] C;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user