# Thread: Two Hard-coded Arrays into One 2-Dimensional Vector

1. ## Two Hard-coded Arrays into One 2-Dimensional Vector

Hello, I have been working on this issue for a long time, and I need your expertise.
I have 2 arrays, each of them are hard-coded with integer values.
I also have one 2-Dimensional vector and I want to put 1 array into the first column of the vector and the other array into the 2nd column of the vector. The reason is that I want to do math on the 2nd column of the vector only.

I am able to accomplish this with 3 arrays. Two of them are 1-Dimensional and the third array is 2-Dimensional.

I know how to pass ONE Array into ONE vector:
Code:
` vector<int> myVector(typeArray, typeArray + 4);`
however, when I declare a 2-Dimensional vector:
Code:
`vector< vector <int> > myVector(3, vector<int> (2,0))`
I am not seeing how to add TWO arrays or how to OUTPUT it to the screen.
Here is my code using DevCPP:
Code:
```#include <iostream>
#include <vector>
#include <Windows.h>
#include <algorithm>

using namespace std;

int main()
{

int typeArray[3] = {55,66,77};
int valArray[3] = {1,2,3};

// for vector: 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS;
//  0 = VALUE all cells are initialized to
vector< vector <int> > myVector(3, vector<int> (2,0));

for (int i = 0; i < sizeof(typeArray); i++)
{
for (int j = 0; j < sizeof(valArray); j++)
{
myVector[i][j];
}
// This "cout" statement doesn't work
//cout << "Vector is now: " << myVector[i][j] << endl;
}

system("Pause");
return 0;
}```
I dont' get any errors, however, I don't know how to output it to the screen to see what it looks like. Please advise, thanks!

2. Are you looking for something like this:
Code:
```int main() {
int typeArray[3] = {55,66,77};
int valArray[3] = {1,2,3};
vector< vector <int> > myVector(3, vector<int> (2, 0));

for (int i = 0; i < 3; i++) {
myVector[i][0] = typeArray[i];
myVector[i][1] = valArray[i];
}

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
cout << myVector[i][j] << ' ';
}
cout << '\n';
}

return 0;
}```

3. Hello oogabooga,

Thank you for the post. I just put it into my program and it's looking good. Let me populate all of the indices and run it.
After that, I am looking to expand on this program and would greatly appreciate your input. I'll be checking in soon, thanks!

4. ## Phase 2 of: Two Hard-coded Arrays into One 2-Dimensional Vector

OK, my code is revised and it is now working. I am ready for Phase 2 of this program.
However, before taking it there, I'd like to receive input on the following questions:

1. How does the "for loop" method differ from the "push_back", or "assign" methods?
For example, using "assign" to populate the vector with an array:

Code:
`myVector.assign (typeArray,typeArray + 3);`
I still don't know how I'd use this for a 2-Dimensional vector...
Please let me what you think of these alternatives just to be thorough.

Here's the REVISED code and output using DevCPP:
Code:
```#include <iostream>
#include <vector>
#include <Windows.h>
#include <algorithm>

using namespace std;  int main()
{
int typeArray[3] = {55,66,77};
int valArray[3] = {1,2,3};

// re: vector: 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS; //  0 = VALUE all cells are initialized to.
vector< vector <int> > myVector(3, vector<int> (2,0));
for (int i = 0; i < sizeof(typeArray)/sizeof(typeArray[0]); i++)
{
myVector[i][0] = typeArray[i];

for (int j = 0; j < sizeof(valArray)/sizeof(valArray[0]); j++)
{
myVector[j][1] = valArray[j];
}
}
cout << myVector[0][0] << " " << myVector[1][0]<< " " << myVector[2][0]<< endl;
cout << myVector[0][1] << " " << myVector[1][1]<< " " << myVector[2][1]<< endl;

system("Pause");
return 0;  }```
-----------------------------------
OUTPUT:

55 66 77
1 2 3

--------------------
please confirm about using either "assign" or "push-back" methods, and after that I'll submit the next phase which is one array is 4 elements, and the other is 13 elements. Thanks!

5. Your nested loops don't make any sense. Look back at the code I posted before. Notice that it only uses ONE loop, not two nested loops. In your code, the inner loop is being executed 3 times and each time is doing exactly the same thing, filling the 2nd "column" of the vector with values from valArray. In other words, you don't need the inner loop at all (meaning that you didn't understand the code I posted before).

You could use assign if you switched the dimensions of your vector-of-vectors, like this:
Code:
```    vector< vector <int> > myVector(2, vector<int> (3, 0));

myVector[0].assign(typeArray, typeArray + 3);
myVector[1].assign(valArray, valArray + 3);```
Note that the 2 and 3 have been switched in the definition of myVector.

push_back could be used if you didn't already set the size of the inner vector, since push_back actually adds an element to the end of the vector each time.

6. Hi oogabooga, thank you for clarifying the loop issue. You are right, I didn't quite get your code before, and my output wasn't coming out right either....?...However, I ran it again, and it's perfect, thank you.

I also like the "assign" method, thank you for posting that as well. For me, the aspect of adding the index to the vector BEFORE the dot operator has been really confusing. So, I want to clarify that "myVector[0]" is the first ROW and "myVector[1] is the second ROW, yes?

Next, I'm expanding this program to make the vector 4 ROWS and 13 COLUMNS.
I'm going to code it tomorrow morning following your advice. If it works correctly, I'll post it with the next part of the program which doing math with the
"valArray" elements only. Thanks again, you've been very helpful.

7. The example I gave in post #5 should be:
Code:
```    vector< vector <int> > myVector(2);  // create 2 "rows"

myVector[0].assign(typeArray, typeArray + 3);  // assign typeArray to first row
myVector[1].assign(valArray, valArray + 3);    // assign valArray to second row```
So, I want to clarify that "myVector[0]" is the first ROW and "myVector[1] is the second ROW, yes?
Yes, that's correct, although you could also correctly think of them as the first and second "columns" since it's arbitrary. More precisely, each of myVector[0] and myVector[1] holds a vector-of-ints, which you are imagining as "rows".

8. Originally Posted by oogabooga
Are you looking for something like this:
This should also work...

Code:
```int main()
{
int typeArray[3] = {55,66,77};
int valArray[3] = {1,2,3};
const size_t SizeOfTypeArray = sizeof(typeArray) / sizeof(typeArray[0]);
const size_t SizeOfValArray = sizeof(valArray) / sizeof(valArray[0]);

vector< vector <int> > myVector(3, vector<int> (2, 0));

std::copy(typeArray, typeArray + SizeOfTypeArray, myVector[0].begin());
std::copy(typeArray, valArray + SizeOfValArray, myVector[1].begin());
}```

9. Hi Elysia, thanks for the alternate code, it's always good to see different examples. I don't understand the comments underneath your code...? If I'm not mistaken, if you include "using namespace std;" eliminates the need to type "std::copy" yes?....And doesn't "SizeOf" have to have "()" at the end?...Just checking, as I am a recent graduate practicing my code while pursuing Programmer positions!....I am starting to work on the next piece of this project right now, so please keep an eye out.

10. Hello oogabooa, Thank you again for schooling me on this topic. I have learned more in these couple of posts than in many sites cuz of course what I'm looking for here isn't in either of my books...; ) So, u rock. I'm getting ready to launch the next phase right now, so pls keep an eye out (p.s. luv the quotes ; p )

11. ## Now, it's "Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector"

OK, now I'm making the arrays two different sizes and haven't figured out exactly how to populate the vector correctly.
Although I'm coding with "for loops" in this one, I also tried the"assign" method which was basically the same output.

Here's the code and output:
Code:
```int main()
{

int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};

// 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
//  0 = VALUE all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (13,0));

for (int i = 0;  i < 4;  i++)
{
myVector[i][0] = typeArray[i];

for (int j = 0;  j < 13;  j++)
{
myVector[1][j] = valArray[j];
}
}

// print to screen
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 13; j++)
{
cout << myVector[i][j] << ' ';
}
cout << '\n';
}```
Before I post the output, I realize that if I want "valArray" to populate to the right of COLUMN 0,0 I should put [j][1], however, I keep getting an error when I do that.
Here's the OUTPUT:
55 0 0 0 0 0 0 0 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 10 10 11
77 0 0 0 0 0 0 0 0 0 0 0 0
88 0 0 0 0 0 0 0 0 0 0 0 0
Press any key to continue . . .
-------------------------------------------

12. As we've discussed, there are many possible ways of doing this. Here's what I might do.
Code:
```#include <iostream>
#include <vector>
using namespace std;

#define ASIZE(a) (sizeof(a)/sizeof(*(a)))

typedef vector<int> VI;
typedef vector<VI>  VVI;

int main() {
int typeArray[] = {55,66,77,88};
int valArray[]  = {1,2,3,4,5,6,7,8,9,10,10,10,11};
VVI  v;

v.push_back(VI(typeArray, typeArray + ASIZE(typeArray)));
v.push_back(VI(valArray,  valArray  + ASIZE(valArray)));

for (size_t i = 0; i < v.size(); i++) {
for (size_t j = 0; j < v[i].size(); j++)
cout << v[i][j] << ' ';
cout << '\n';
}
}```