Write a program that declares 2 integer arrays x, y of size 10 each. The program then reads values for x and y arrays from the user (use some common values).
Finally the program finds the common values in x and y and prints them. If there is no common values in x and y print “There is nothing common between array x and y”.
#include <iostream>
using namespace std;
int main() {
int x[10], y[10];
int element, flag = 0;
cout << "Input X elements" << endl;
for (int i = 0; i < 10; i++) {
cin >> x[i];
}
cout << "Input Y elements" << endl;
for (int i = 0; i < 10; i++) {
cin >> y[i];
}
cout << "Common values:" << endl;
for (int i = 0; i < 10; i++) {
element = x[i];
for (int j = 0; j < 10; j++) {
if (element == y[j]) {
flag = 1;
cout << element << " ";
break;
}
}
}
if (flag == 0) {
cout << "There is nothing common between array x and y" << endl;
}
}
Comments
I like your source code.
Leave a comment