473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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:
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
2 1747
whodgson
542 512MB
"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
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

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

Similar topics

10
by: 3A Web Hosting | last post by:
Hi Folks This is probably starring me in the face but how do I read the contents of a hard drive directory? I've been playing around with the opendir($dir) sample from php.net and can read from...
2
by: Nate | last post by:
Hello, I am trying to recover a SQL Server 7 database from another hard disk drive that has a corrupted Windows 2000 Advanced Server installation. I am not able to repair the corrupted Windows...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
2
by: Mike | last post by:
I need my textbox to work more smoothly with respect to line breaks. When I have data pulled from the database into a textbox there are hard line breaks at the end of each line (by definition how...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
16
by: Otie | last post by:
Hi, Is there a way for VB5 to determine exactly where on a hard drive a .exe file is stored upon the .exe file's first copying to the hard drive? What I need to know is the exact hard drive...
3
by: Kurt Mueller | last post by:
David, Am 07.10.2008 um 01:25 schrieb Blubaugh, David A.: As others mentioned before, python is not the right tool for "HARD REAL TIME". But: Maybe you can isolate the part of your...
0
by: TamusJRoyce | last post by:
http://nohardlockrwlocker.codeplex.com/ is a read-write locker I recently designed so hard locks are impossible. It is meant to replace ReaderWriterLock and ReaderWriterLockSlim. Upgrade locks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.