tables, vectors

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?
Last edited on
No, that one does not cut it. What if i==50 and tab1.size()==2? You cannot do tab1[50]. Besides, your tab3 is empty.

You are merging two lists. Your first example shows interlacing, but your second example shows concatenation. That is not consistent. Be more clear about what you have to do.

You print out something. Should the caller rather get the new list somehow?
The description I wrote at the beginning of the first post is almost a word by word translation.
The program has to interlace two 'int' tables, in one dimension.

And the examples I gave were the very ones the professors gave in the assignment description. If you understand any French I can paste the assignment as it is.

Also they say that the main function in our code can be empty. They're only interested in the way we write the function that does the interlacing. I only included the 'cout' command so that I can check if what I wrote works.

Truth is I don't really understand vectors and arrays. They're too confusing.
If tab1 = {1, 2, 3} and tab2 = {4, 5, 6}, we’ll get as a result a tab3 = {1, 4, 2, 5, 3, 6}.
If tab1 = {1, 2, 3} and tab2 = {4, 5, 6, 7, 8} => tab3 = {1, 2, 3, 4, 5, 6, 7, 8}

These seem contradictory. The first example interlaces the two arrays. The second simply concatenates them.

Which is it? Maybe you need to get clarification from your professor?
Fear not. Vectors are very easy compared to the rest of C++.

The problem in description is that the second example does not interlace at all.


You should put the test output into your main(). Then your function has to return a list, as it should according your description.

The input parameters should be const, because you will not change them.

How long should the result list be? You can compute that at start. Therefore, you can create a vector of right size().

How many interlaced pairs there will be? That too you can precompute. Loop that many times.

Concatenate the tail of the longer input to the result.

Return the result.
Oh, my gosh! I'm so sorry. I guess I was really tired last night, when I posted.

The second example is like this:
tab1 = {1, 2, 3}
tab2 = {4, 5, 6, 7, 8}

so tab3 = {1, 4, 2, 5, 3, 6, 7, 8}

Sorry, again.
Gonna start working on the code again now.
Thanks for your answers so far :D
Oh, oh, I figured it out.

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
#include <iostream>
#include <vector>
using namespace std;

vector<int> result(const vector<int>& tab1, const vector<int>& tab2)
{
    vector<int> tab3;
    for(size_t i = 0; i < max(tab1.size(), tab2.size()); ++i)
    {
        if(i < tab1.size()) tab3.push_back(tab1[i]);
        if(i < tab2.size()) tab3.push_back(tab2[i]);
    }
    return tab3;
}

int main()
{
    vector<int> tab1 = {1, 2, 3, 4};
    vector<int> tab2 = {5, 6, 7};
    vector<int> tab3 = result(tab1, tab2);
    for(int i: tab3)
        cout << i << ' ';
    cout << '\n';
}


I could also do it so that it asks the user to input the values, but it's not required of me.

Thanks.
Glad it worked out :)
Topic archived. No new replies allowed.