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>
int main()
{
int x[10]{ 0};
int y[10]{ 0};
int total[10]{ 0 };
int total_len{ 0 };
//input of array elements
for (int i = 0; i < 10; i++) {
std::cout << "Enter x[" << i << "]=";
std::cin >> x[i];
std::cout << "Enter y[" << i << "]=";
std::cin >> y[i];
}
// checking array elements
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (x[i] == y[j]) {
total[total_len] = x[i];
total_len += 1;
}
}
}
// output list of common unique elements for x, y array
if (total_len > 0) {
std::cout << "List of common unique elements:" << std::endl;
bool detect = true;
for (int i = 0; i < total_len; i++) {
detect = true;
for (int j = 0; j < i; j++) {
if (total[i] == total[j]) {
detect = false;
}
}
if (detect==true) {
std::cout << total[i] << ", ";
}
}
}
else {
std::cout << "There is nothing common between array x and y";
}
return 0;
}
Comments
THIS HELP ME IN MY STUDY TOO MUCH
Leave a comment