search array element program in C++ : Two arrays are used in the program and the size of both arrays is given as 20
The first-array will store the element entered by the user, while the second-array will be used to store them, in the second-array if matches are found with the first-array, means only matched elements will be stored in the second array
The elements entered by the user are as follows-
int value[6],found[6];
value [] = 3,2,1,3,4,3
Then array value[6] entered by the user has store in these location-
int value[6]= {
' 3',' 2',' 1',' 3 ','4',' 3'
};
that is,
value[0] = 3
value[1] = 2
value[2] = 1
value[3] = 3
value[4] = 4
value[5] = 3
And if the user searches number is 3 and if matched, then the matched-element in will be store in second-array found[] like this-
Now to print second-array found[] elements, we use a loop that prints only to matched element, by doing this-
found[0] = 3
found[1] = 0
found[2] = 0
found[3] = 3
found[4] = 0
found[5] = 3
As you can see above, in the first-array value[] in which location, the number 3 element matches. He stores it in the same location in the second-array found[] and stores the value 0 in the location where the match is not found.
Now to print second-array found[] elements, we use a loop that prints only to matched element, by doing this-
if(found[i]!=0) // skip element if is equal to 0
value[0] = 3
value[3] = 3
value[5] = 3
Here is the complete program, below-
C++ search Array Element program
#include<iostream>
using namespace std;
int main()
{
clrscr();
int value[20],n,search,flag=0,i,count=0,found[20];
cout<<"Enter the size of Array: ";
cin>>n;
cout<<"Enter the Element in the Array\n";
for(i=0;i<n;i++)
{
cout<<"value["<<i<<"] = ";
cin>>value[i]; // storing value into value [] array(first array)
}
// array element displaying
cout<<"Your Entered Element Are\n";
for(i=0;i<n;i++)
cout<<value[i]<<" ";
cout<<endl;
// search process begin here
cout<<"Enter the Element to be search: "; cin>>search;
for(i=0;i<n;i++)
{
if(value[i]==search)
{
count++;
found[i]=value[i]; // assigning search element to found[] array(second array)
flag=1;
}
else
found[i]=0; // assigning 0 to found array
}
if(flag) {
cout<<count<<" Element Found in Array list Are: "<<endl;
}
else {
cout<<"Element Not Found";
}
// display found[] array element
for(i=0;i<n;i++)
{
if(found[i]!=0) // means only print element which is not 0
cout<<"value["<<i<<"] = "<<found[i]<<endl;
}
return 0;
}
OUTPUT
Enter the size of Array: 6
Enter the Element in the Array
value[0] = 3
value[1] = 2
value[2] = 1
value[3] = 3
value[4] = 4
value[5] = 3
Your Entered Element are
3 2 1 3 4 3
Enter the Element to be search: 3
Element Found in Array list Are:
value[0] = 3
value[3] = 3
value[5] = 3
when the user enters the size of the first array, then the size of the second array will also be the same,
Meaning the size of both arrays will be the same. The only difference is that while the first array value[] gets its size from the user while second array find[] will be get their size when Element searching process begins (in the body of the if-statement),
Don’t confuse here, we know both array size 20 already declared in the program but we talk about less of 20 which will be entered by the user in first array value[].
we can also perform same task with multidimensional array in C++