# how to find array element in c++

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,found;``
``value [] = 3,2,1,3,4,3``

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

``````int value= {
' 3',' 2',' 1',' 3 ','4',' 3'
};``````

that is,

``````  value = 3
value = 2
value = 1
value = 3
value = 4
value = 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 = 3
found = 0
found = 0
found = 3
found = 0
found = 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 = 3
value = 3
value = 3``````

Here is the complete program, below-

## C++ search Array Element program​

``````#include<iostream>
using namespace std;

int main()
{
clrscr();
int value,n,search,flag=0,i,count=0,found;

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

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 {
}

// 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 = 3
value = 2
value = 1
value = 3
value = 4
value = 3

3 2 1 3 4 3

Enter the Element to be search: 3
Element Found in Array list Are:
value = 3
value = 3
value = 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++

Like it?
{"ft_lang_label":"__label__en","ft_lang_prob":0.7815478,"math_prob":0.99133396,"size":3321,"snap":"2022-40-2023-06","text_gpt3_token_len":969,"char_repetition_ratio":0.18691589,"word_repetition_ratio":0.13309982,"special_character_ratio":0.3239988,"punctuation_ratio":0.10769231,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9953442,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-03T14:05:19Z\",\"WARC-Record-ID\":\"<urn:uuid:9e5234cc-ecc5-4176-94b6-3db7035fdbcf>\",\"Content-Length\":\"128132\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:77b5c1a3-ee4c-45dc-a8b2-cc4d32e92e76>\",\"WARC-Concurrent-To\":\"<urn:uuid:c100f335-3e98-4bed-9db0-06c2535264df>\",\"WARC-IP-Address\":\"104.21.49.35\",\"WARC-Target-URI\":\"https://cpphinditutorials.com/dev-cpp/how-to-find-array-element-in-cpp/\",\"WARC-Payload-Digest\":\"sha1:OY5S5RBKYWUP5C6GAL5QFR37PN4RCMDF\",\"WARC-Block-Digest\":\"sha1:EDIGTWZUW5LNRMXBLQWMVOVRHSELVIHZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500056.55_warc_CC-MAIN-20230203122526-20230203152526-00758.warc.gz\"}"}