By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

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

P: 8
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>    
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int typeArray[4] = {55,66,77,88};
  9.     int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
  10.  
  11.     // 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS;
  12.     //  0 = VALUE all cells are initialized to
  13.     // using 2 "for loops"
  14.     vector< vector <int> > myVector(4, vector<int> (13,0));
  15.  
  16.   for (int i = 0; i < myVector.size(); i++) 
  17.     {
  18.       myVector[i][0] = typeArray[i];
  19.  
  20.       for (int j = 0; j < myVector[i].size(); j++) 
  21.         {
  22.            myVector[1][j] = valArray[j];
  23.          }
  24.  
  25.       }
  26.  
  27.  // print vector to screen with 4 ROWS, 13 COLUMNS
  28.         for (int i = 0; i < 4; i++)
  29.           {         
  30.             for (int j = 0; j < 13; j++)
  31.              { 
  32.               cout << myVector[i][j] << ' ';
  33.               }         
  34.               cout << '\n';
  35.           }
  36.     system("Pause");
  37.     return 0; 
  38. }
  39.  
--------------------------

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.
Mar 24 '12 #1
Share this Question
Share on Google+
2 Replies


100+
P: 542
"I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW."
This says you want to place 13 elements into 12 columns??
Forgetting about the 2_dim vector for a moment. You could use 2 nested loops-- the first to print 55,66,77,88, , , , , , , ...and so on in column#1 successively.
a statement beneath this first loop like
Expand|Select|Wrap|Line Numbers
  1. if(i>3)cout<<"  ";
may be worth having.
and the second to print space,1,2,3,4,5,6,7,8,9,10,10,10,11. the second array would need to hold 14 elements to include the first space.
Does this suggest how you might handle you 2_D vector? It does not for me!
Mar 26 '12 #2

P: 8
Hello whodgson, thanks for the tips. In answer to your question about "place 13 elements into 12 columns" you're correct, I need to change the vector length dimension to 14.
And, thank you for being direct about handling the 2_D vector.
This is actually just a trial example for a deck of cards in Blackjack. In this case, the 55,66,77,88 would be IDs for the suits which don't need a numeric value. I realize this wouldn't be the final data structure.
So, in the interest of this particular test case, I am posting one version of working code for this question with the output:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7.    const int typeSize = 4;
  8.    const int valSize = 14;
  9.  
  10. int main()
  11. {         
  12.       // arrays to be loaded into 2-dimensional int vector  
  13.       int typeArray[typeSize] = {55,66,77,88}; 
  14.       int valArray[valSize] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13}; // array is 1 element longer to begin populating vector in column 1      
  15.       vector< vector <int> > myVector(4, vector<int> (14,0));
  16.  
  17.     // Example using a "for loop" assigning "typeArray" to the first COLUMN only
  18.        for (int i = 0; i < myVector.size(); i++) 
  19.          {
  20.            myVector[i][0] = typeArray[i];
  21.     // Example using a "for loop" assigning "valArray" to each of the 4 ROWS beginning in the SECOND COLUMN 
  22.  
  23.          for (int j = 1; j < myVector[i].size(); j++) 
  24.            {
  25.              myVector[i][j] = valArray[j - 1];
  26.            }
  27.         }
  28.         // print vector to screen with 4 ROWS, 14 COLUMNS
  29.         for (int i = 0; i < myVector.size(); i++)
  30.           {         
  31.             for (int j = 0; j < myVector[i].size(); j++)
  32.              { 
  33.               cout << myVector[i][j] << ' ';
  34.               }         
  35.               cout << '\n';
  36.           }
  37.  
  38.     system("Pause");
  39.     return 0; 
  40. }
  41.  
-----------------------
OUTPUT:
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
. . . .
---------------------------

Please feel free to provide feedback on this example.
In the meantime, I am going to post again regarding assigning the string values to the ints. Thanks!
Apr 6 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.