Hello again!
I keep coming back with my silly noobie questions. But I do hope one day I’ll be able to help others around here.
So, I’ve just finished studying tables, vectors, arrays and one of my assignments is to create a program that kind of mixes up the elements of 2 tables, creating a third table.
For example:
If tab1 = {1, 2, 3} and tab2 = {4, 5, 6}, we’ll get as a result a tab3 = {1, 4, 2, 5, 3, 6}.
So for tab3:
- the 1st element is the 1st element of tab1
- the 2nd element is the 1st element of tab2
- the 3rd element is the 2nd element of tab1
- the 4th element is the 2nd element of tab2
- the 5th element is the 3rd element of tab1
- the 6th element is the 3rd element of tab2
If tab1 = {1, 2, 3} and tab2 = {4, 5, 6, 7, 8} => tab3 = {1, 2, 3, 4, 5, 6, 7, 8}
If tab1 = {1, 2, 3} and tab2 is vide => tab3 will simply be {1, 2, 3}.
So they ask me to create a
function with a name given by them (let’s say called ‘result’ – the course is in a different language than English) with
two type int dynamic table parameters and which
returns an int dynamic table.
This is what I've been working on for the past 5 hours:
*it is only for the tab1 = {1, 2, 3} and tab2 = {4, 5, 6} example. I'm trying to make it work for at least one example and then try to expand it to every possibility.
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
|
#include <iostream>
#include <vector>
using namespace std;
void result(vector<int>& tab1, vector<int>& tab2);
int main()
{
vector<int> tab1({1, 2, 3});
vector<int> tab2({4, 5, 6});
result(tab1, tab2);
return 0;
}
void result(vector<int>& tab1, vector<int>& tab2)
{
vector<int> tab3;
for(size_t i(0); i < tab1.size() or i < tab2.size() ; ++i)
{
if((i==0))
{
tab3[i] = tab1[i];
tab3[i+1] = tab2[i];
}
else if((i==1))
{
tab3[i+1] = tab1[i];
tab3[i+2] = tab2[i];
}
else if((i==2))
{
tab3[i+2] = tab1[i];
tab3[i+3] = tab2[i];
};
cout << tab3[i] << ", ";
};
}
|
Does my code make any sense? Am I anywhere close to completing the program?
Any ideas what I could do to make it work?