How to Match Values Within an Array

Jun 27, 2011 at 2:09am
Hello,

I am trying to take an array of a given size (5), and write a function that will select matching values within that array...

...for example...if I have an array of the following integers, I would like the function to output the matching values...

array[5] = {3, 4, 4, 4, 5}

The Output should show that there are three matching values (4, 4, & 4)...

I've been playing with code from previous articles to no avail...here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int numbs[5],  value, idx, n = 5;
        
        cout << "Please enter in a series of numbers "<<endl;
        for(idx = 0; idx < n; idx++)
			cin >> numbs[idx];
		
		        
        for (int i = 0; i < n; i++)
        {
			bool matching = false;
			for (int j = 0; (j < i) && (matching == false); j++)
		
			if (numbs[i] == numbs[j]) matching = true;
			if (matching) cout<< numbs[i] << " ";
        }    


Please Advise...thanks!
Last edited on Jun 27, 2011 at 2:09am
Jun 27, 2011 at 3:06am
It seems that one way would be to use two loops and a separate
counter.
The first loop say (i) would iterate through the array first pointing at array[0], and the second loop say (j) would compare the second, third,fourth (and so on) elements with the first element. J would appear to always = i+1 at the start of its iteration. Then after the first iteration i would point to the array [1] element and j would point to array[2]. this element and all succeeding elements would be compared with array[1]. and so on. If the element being traversed by j was == the element pointed to by i then the i counter could be increased by one. After each iteration print out the number of duplicates and zero the counter.
hope this helps.
Last edited on Jun 27, 2011 at 3:08am
Jun 27, 2011 at 4:24am
Do you know how to search an array (linear search?)
Just do that, and each time you find the value you want, do what you want with it...
Jun 27, 2011 at 5:50pm
Thanks for the help...my problem was mostly syntax within the "for-loops"...

Here is the solution...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int numbs[5], idx, n = 5;
        
        cout << "Please enter in a series of numbers "<<endl;
        for(idx = 0; idx < n; idx++)
			cin >> numbs[idx];
		
		    int j=1;    
        for (int i = 0; (i < n - 1); i++)
        {
			
			bool matching = false;
			for (j = i + 1; (j < n) && (matching == false); j++)
			{
		
				if (numbs[i] == numbs[j]) {matching = true;}
				if (matching) 
					{
						cout<< numbs[i] << ", " << numbs[j];
						cout << "\nj is equal to: " << j << "\n";
						matching = false;
				
					}
			}
			
				cout << "Iteration of i: " << i << "\n";
        }  

Last edited on Jun 27, 2011 at 10:32pm
Jun 28, 2011 at 4:56pm
Hi,
got some question on my c++ project.
how can i validate or match the data that store in my array with a user input from a keyboard?
thx
Topic archived. No new replies allowed.