Example(s):Palindrome, prime, armstrong, "linear search", reverse etc.
Example(s):1575, 1632, 1539 (Only one at a time)
Login
[lwa]
Solved Problem
#CPP#5617
Problem Statement - Searching elements from one array into another array
Write a program to search members of integer array 1 in array 2 and print the members of array 1 that are also found in array 1. Also print the total number of members found.
For e.g. if array 1 is [12,19,23,3,2] and array 2 is [13,12,2,3,14,21],
the output would be
12 3 2
count = 3
Solution
TC++ #5617
Run Output
12 3 2
count = 3
Notes
int ar1[]={12,19,23,3,2}; //This is array 1 which contains members to be searched int ar2[]={13,12,2,3,14,21}; //This is array 2 in which the search would be made int count=0; //This will keep the count of found items for(int i=0;i<5;i++) { //Outer loop for checking each member from ar1 for(int j=0;j<6;j++){ //Inner loop for checking match of ar1 to ar2 members if(ar1[i]==ar2[j]) { //If match is found cout<<ar1[i]<<” “; //Matched item is printed count++; //Match count is incremented
}
}
} cout<<endl; cout<<“count = “<<count<<endl; //Count is finally printed outside the loop