How to Match Values Within Arrays

Hi I'm trying to write a function that will match the values within an array and the other;

for for example.. if I have an array of size 16 called dimT=16 and an array of size 4 called dimP=4 where the arrays are T=[0,1,1,1,1,2, 0,1,0,1,1,1,1,2,0,0] and P=[1,1,1,2] then there'll be 2 matches of P in T in the 2 and 10 positions.

I just need my output to be the number of matches..
ps.I need to use while and not do while.

Here there's what I've done atm..

#include<iostream>
using namespace std;
int main()

{
int T[100], P[20], dimT, dimP;
cin>> dimT >> dimP;
int i=0;
while(i<dimT)
{
cin >>T[i];
i++;
}
int j=0;
while ( j<dimP)
{
cin>>T[j];
j++;
}

and here is where i stopped.

I am still a noob in c++ so any help would be great!

Thanks in advance!!!
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <sstream>

typedef std::vector<int> Array_t; // alias 

Array_t GetData(int n)
{
  Array_t v;
  cout << "Please enter " << n << " values" << endl;
  int i = 0; // variable we will use to read input
  while(v.size() < n) { cin >> i; v.push_back(i); }
  return v;
}

// function for getting matches
int GetMatches(const Array_t& smaller, const Array_t larger)
{
   int start = 0;
   int stop = smaller.size();
   int num_matches = 0;

   // if start+stop == larger.size() we need to quit the next time around
   while(start + stop <= larger.size())
   {
       unsigned i = start;      
       for(; i < start+stop; ++i)
       {
           if(smaller.at(i) != larger.at(i)) // if there is a mismatch i will be less than start+stop
              break;
       }
       // if i == start+stop it made it all the way though without a mismatch
       if(i == start+stop) { ++num_matches; }
       // increment start so we move to the next sequence
       ++start;
   }
   return num_matches;
}

std::string print(Array_t v)
{
  // a stringstream is like cout except it doesn't print to the screen
  std::stringstream ss; ss << "[";
  for(unsigned i = 0; i < v.size(), ++i) { ss << v[i]; if(i+1 < v.size()) { ss << ","; } }
  // convert the stringstream to a string
  return ss.str();
}

int main()
{
  int dimT, dimP;
  // read in the tow dimension
  cin >> dimT >> dimP;
  Array_t T = GetData(dimT);
  Array_t P = GetData(dimP)

  // this is basically an IF/ELSE statement 
  // -- if what is in () is true it calls what is before ":" --> GetMatches(T, P)
  // -- if what is in () is false, it calls what is after ":" --> GetMatches(P, T)
  int num_matches = (T.size() < P.size()) ? GetMatches(T, P) : GetMatches(P, T);

  cout << "The array " << print( (T.size() < P.size()) ? T : P) << " appeared " << num_matches << " in " << print( (T.size() < P.size()) ? P : T ) << endl;


Normally, it's not good to just give people code but if you are a beginner then if you take the time to study this and refer back to it when you work on things down the line you will benefit from it. You'll notice I used a thing called a "vector". It's basically a flexible size C-array that in the STL library of C++. It is highly recommended over C-style arrays.
Thanks you very much. but actually my professor wants us to solve it just with while ect.. because in this way it appears really difficult , at least for me, being a beginner =D
Conceptually this task can use an equivalent of std::string::find
See http://www.cplusplus.com/reference/string/string/find/
(Array of chars or array of ints -- the logic is same.)

The result array of matching offsets has at most dimT+1-dimP elements.

You can do everything "simple and basic" in main(), but I do recommend creating at least one simple "doesthisoffsetmatch" function.
Topic archived. No new replies allowed.