# Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector

• 03-24-2012
codechick
Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector
I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
I want COLUMN 0 to have: 55, 66, 77, 88.
I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:

Code:

```#include <iostream>    #include <vector>   using namespace std;   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};       // 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS;     //  0 = VALUE all cells are initialized to     // using 2 "for loops"     vector< vector <int> > myVector(4, vector<int> (13,0));     for (int i = 0; i < myVector.size(); i++)      {       myVector[i][0] = typeArray[i];         for (int j = 0; j < myVector[i].size(); j++)          {           myVector[1][j] = valArray[j];         }         }    // print vector to screen with 4 ROWS, 13 COLUMNS         for (int i = 0; i < 4; i++)           {                      for (int j = 0; j < 13; j++)             {                cout << myVector[i][j] << ' ';               }                        cout << '\n';           }     system("Pause");     return 0;  }```
------------------------------------
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
-----------------------------
Please advise how to populate rows correctly, thank you.
• 03-24-2012
jimblumberg
So do you want your output to be?
Quote:

55,1,2,3,4,5,6,7,8,9,10,10,10,11
66,1,2,3,4,5,6,7,8,9,10,10,10,11
77,1,2,3,4,5,6,7,8,9,10,10,10,11
88,1,2,3,4,5,6,7,8,9,10,10,10,11

Jim
• 03-24-2012
codechick
Hello Jim, yes that is exactly what I want it to look like, thanks.
• 03-24-2012
jimblumberg
Then to populate this vector you will loop through an outer loop 4 times and store the correct element 0 and then loop starting at 1 to populate the rest of the elements. Also your vector[x].size() should be 14 instead of 13.

Or you could populate the vector with your array.
Code:

```#include <iostream> #include <vector> using namespace std; const int typeSize = 4; const int valSize = 14; int main() {   int typeArray[typeSize] = {55,66,77,88};   int valArray[valSize] = {0,1,2,3,4,5,6,7,8,9,10,10,10,11};   // Create a vector of vector of ints populated by valArray   vector< vector <int> > myVector(typeSize, vector<int>(valArray, valArray + sizeof(valArray) / sizeof(int)));   for(size_t i = 0; i < myVector.size(); ++i)       myVector[i][0] = typeArray[i]; // print vector to screen with 4 ROWS, 13 COLUMNS   for (size_t i = 0; i < myVector.size(); i++)   {       for (size_t j = 0; j < myVector[0].size(); j++)       {         cout << myVector[i][j] << ' ';       }       cout << '\n';   }   cin.get();   return 0; }```
Notice the added element in the valArray array.

Jim
• 03-24-2012
codechick
Hi Jim, thank you for the revised code! Let me re-code my program and put together some questions I have about how you are doing it. e.g. assigning "valArray" in the 2-D vector declaration rather than loop through it. This is actually one of the biggest issues I had originally with how to populate a 2-Dimensional vector with two different arrays. Let me work on this, and I'll get back to you shortly, thanks!
• 03-24-2012
codechick
Hi Jim,
This code works perfectly. I am wondering if there is a way to make the "valArray" size 13 without the extra -0- and then manipulate the index by subtracting a -1-? I'm going to do math on the "valArray" indices and would prefer not to have the "-0-" if possible.

Otherwise, this is a really nice example of solid coding that helps put together a lot of pieces of info that I know and needed help from someone like you to understand and put together.

Thank you, please let me know about the first question.
I'm going to post again with changing the "typeArray" values to strings and outputting them with the "valArray" numbers.
So, if you're interested, please keep an eye out, thank will be tomorrow, thx!
• 03-25-2012
jimblumberg
Quote:

This code works perfectly. I am wondering if there is a way to make the "valArray" size 13 without the extra -0- and then manipulate the index by subtracting a -1-? I'm going to do math on the "valArray" indices and would prefer not to have the "-0-" if possible.
Yes but the you would not be able to use valArray to initialize your vector, you will need to use a loop after you define your vector.

Jim
• 03-25-2012
whiteflags
Quote:

Yes but the you would not be able to use valArray to initialize your vector
I disagree - Something like this could work perhaps.

I must admit I fail to see the shortcomings of the initial answer regardless.

Code:

```#include <vector> int main() {     typedef std::vector<int>::size_type typeArrSize;     int typeArray[] = {55,66,77,88};     int valArray[] = {1,2,3,4,5,6,7,8,9,10,10,10,11};     std::vector< vector<int> > my( sizeof typeArray / sizeof typeArray[0]       , vector<int>(valArray, valArray + sizeof valArray / sizeof valArray[0]) );     for (typeArrSize i = 0; i < my.size(); i++) {       my[i].insert(my[i].begin(), typeArray[i]);     } }```
• 03-26-2012
jimblumberg
That should also work, didn't think about insert(). :redface:

Jim
• 03-31-2012
codechick
Hello Jim and White Flags,

I just logged in tonight. Thank you for the responses and the alternate ways of initializing the vector. As mentioned, I have learned A LOT on this, thank you.
Tomorrow, I"m putting up the next piece of the program, so pls keep an eye out.