Check the textbook to see the problem
#include < iostream >
#include < cmath > // We use fmax function
#include < vector > // We use vector function
using namespace std;
vector< int > merge(vector< int >& a, vector< int >& b)
{
vector< int > c;
int larget_num = fmax(a.size(), b.size());
for(int i=0 ; i < larget_num; ++i)
{
if(i < a.size())
{
c.push_back(a[i]);
}
if(i < b.size())
{
c.push_back(b[i]);
}
}
return c;
}
int main()
{
vector< int > a = {1,1,1,1};
vector< int > b = {3,3,3,3,3,3};
vector< int > c = merge(a,b);
for(int i=0;i < c.size(); ++i)
{
cout << c[i] << endl;
}
}
Check the textbook to see the problem
#include < iostream >
#include < vector > // We use vector function
using namespace std;
bool equals(vector< int >& a, vector< int >& b)
{
if(a.size() != b.size())
{
return false;
}
for(int i=0; i < a.size(); ++i)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
int main()
{
vector< int > a = {1,1,1,1};
vector< int > b = {3,3,3,3,3,3};
bool same = equal(a,b);
cout << same << endl; // will return false
}
Check the textbook to see the problem
#include < iostream >
#include < vector > // We use vector function
using namespace std;
bool equals(vector< int >& a, vector< int >& b)
{
if(a.size() != b.size())
{
return false;
}
for(int i=0; i < a.size(); ++i)
{
if(a[i] != b[i])
{
return false;
}
}
return true;
}
void permutation(vector< int >& a) // Here you need to put & because you need to modify a vector at the end
{
vector< int > c; // initialize a new vector c
for(int i=0; i < a.size(); ++i)
{
c.push_back(a[(i+3) % a.size()]);
}
a = c;
}
bool same_elements(vector< int > a, vector< int > b)
{
// check if two vectors have the same number of items
if(a.size() != b.size())
{
return false;
}
for(int i=0; i < a.size(); ++i)
{
permutation(b);
if(equal(a,b) == true)
{
return true;
}
}
return false;
}
int main()
{
vector< int > a = {1,2,3,4};
vector< int > b = {4,1,2,3};
bool same = same_elements(a,b);
cout << same << endl; // will return true
}
Check the textbook to see the problem
#include < iostream >
#include < vector > // We use vectors
using namespace std;
// This function will check if an integer n is in a vector a
// Suppose a = {1,2,3} and n = 2. Then check(n,a) will return true.
bool check(int n, vector< int > a)
{
for(int i=0; i < a.size(); ++i)
{
// if one of items in a is equal to n then return true.
if(a[i] == n)
{
return true;
}
}
// if none of them equal to n then return false.
return false;
}
void remove_duplicates(vector< int >& a)
{
// initialzie a new vector c
vector< int > c;
for(int i=0; i < a.size(); ++i)
{
if(check(a[i], c) == false) // check if an integer a[i] is in a vector c. If it doesn't then push back to c.
{
c.push_back(a[i]);
}
}
a = c;
}
int main()
{
vector< int > a = {1,1,1,5,5,5,2,2,6,6,1,2,6};
remove_duplicates(a);
// This will print out 1, 5, 2, 6,
for(int i=0; i < a.size(); ++i)
{
cout << a[i] << ", ";
}
cout << endl;
}