Answers
Please follow the below code and use comments to better understand the code.
SOURCE CODE:
#include <iostream>
#include <vector>
using namespace std;
//Returns the count of matched elements in the two vectors.
int posMatchCount(vector<int> v1,vector<int> v2)
{
int count=0;
int i=0;
while(i<10)
{
if(v1.at(i)==v2.at(i))
count++;
i++;
}
return count;
}
//Returns the count of mis-matched elements in the two vectors.
int MisMatchCount(vector<int> v1,vector<int> v2)
{
int count=0;
int i=0;
while(i<10)
{
int j=0;
while(j<10)
{
//Positions should not be same. so check for i!=j
if(v1.at(i)==v2.at(j) && i!=j)
count++;
j++;
}
i++;
}
return count;
}
//Testing our functions
int main()
{
vector<int> vector1 (10);
cout<<"Enter Vector 1 of 10 positive numbers: ";
// read 10 values into vector 1
for (int i=0; i<10; i++)
cin>>vector1.at(i);
vector<int> vector2 (10);
cout<<"Enter Vector 2 of 10 positive numbers: ";
// read 10 values into vector 2
for (int i=0; i<10; i++)
cin>>vector2.at(i);
int count1=posMatchCount(vector1,vector2);
int count2=MisMatchCount(vector1,vector2);
//Print the results
cout<<"Vectors match in "<<count1<<" Positions."<<endl;
cout<<"Vectors mismatch in "<<count2<<" Positions."<<endl;
return 0;
}
=====================================
SCREENSHOT FOR CODE:
=====================================
==========
SAMPLE OUTPUT:
====================================
Please comment down here in the comment box if you have any doubts.
Please hit the LIKE Button if you liked the answer.
====================================
CompiLed and executed in 31.415 sec Enter Vector 2 of 10 positive numbers: 0 0 Vectors match in Vectors mismatch in 1 Positions 0 0 0 5 4 6 Number 5 is Mismatch 1 Positions