Ввести два массива і створити третій спільний з елементів массивів
Ответы
#include <iostream>
using namespace std;
int main() {
int n = 7;
float mas1[n] = {1,44,7,2,65,5,8};
float mas2[n] = {34,5,66,2,3,5,7};
float *mas3 = new float[n];
//Алгоритм
for (int i = 0; i < n; i++) {
if( mas1[i] == mas2[i] ) {
mas3[i] = mas1[i];
n--; i--;
}
}
cout << "Результат:" << endl;
for (int i = 0; i < n; i++){
cout << mas3[i] <<" ";
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 7;
float a1[n];
cout << "First array: ";
for (int i = 0; i < n; i++) cin >> a1[i];
float a2[n];
cout << "Second array: ";
for (int i = 0; i < n; i++) cin >> a2[i];
int m = 0;
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = 0; j < i; j++) if (a1[j] == a1[i]) ++count;
for (int j = 0; count != 0 && j < n; j++) if (a2[j] == a1[i]) --count;
if (count == 0) ++m;
}
float *a3 = new float [n];
int z = 0;
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = 0; j < i; j++) if (a1[j] == a1[i]) ++count;
for (int j = 0; count != 0 && j < n; j++) if (a2[j] == a1[i]) --count;
if (count == 0) a3[z++] = a1[i];
}
for (int i = 0; i < n; i++) if (a3[i] == 0) {n--; i--; }
cout << "The common elements: ";
for (int i = 0; i < n; i++) cout << a3[i] << " ";
cout << '\n';
}