Searching Element in Array list 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-

3,2,1,3,4,3

Let’s try this,

For example, we have declared two array,

int value[6],found[6];

Then array value[6] entered by the user has store in these location-

int value[6]= {' 3',' 2',' 1',' 3 ','4',' 3'};

means,

  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-

  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 number 0
  value[0] = 3
  value[3] = 3
  value[5] = 3

Here is the complete program, below-

#include<iostream.h>
#include<conio.h>

void 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;
  }

 getch();
}
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

optional- 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 finds its size in the first of the program itself, the second array finds its size element while searching (in the body of the if statement).