Answer to Question #188565 in C++ for Asad

Question #188565

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”.



1
Expert's answer
2021-05-03T10:27:06-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

ASAD
03.05.21, 19:30

THIS HELP ME IN MY STUDY TOO MUCH

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS