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!
#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