473,378 Members | 1,447 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,378 software developers and data experts.

Two Hard-coded Arrays into One 2-Dimensional Vector

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:
Expand|Select|Wrap|Line Numbers
  1. vector<int> myVector(typeArray, typeArray + 4);
however, when I declare a 2-Dimensional vector:

Code:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. Code:
  2. #include <iostream>    
  3. #include <vector>
  4. #include <Windows.h>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main()            
  10. {    
  11.  
  12.     int typeArray[3] = {55,66,77};
  13.     int valArray[3] = {1,2,3};
  14.  
  15.     // for vector: 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS;
  16.     //  0 = VALUE all cells are initialized to
  17.     vector< vector <int> > myVector(3, vector<int> (2,0));
  18.  
  19.      for (int i = 0; i < sizeof(typeArray); i++)
  20.          {
  21.           for (int j = 0; j < sizeof(valArray); j++)
  22.              {
  23.              myVector[i][j];
  24.              }
  25.        // This "cout" statement doesn't work
  26.        //cout << "Vector is now: " << myVector[i][j] << endl;    
  27.          }
  28.  
  29.     system("Pause");
  30.     return 0; 
  31. }
  32.  
I don't get any errors, however, I don't know how to output it to the screen to see what it looks like. Please advise, thanks!
Mar 20 '12 #1
5 2631
Banfa
9,065 Expert Mod 8TB
Line 26 is fine, as long as it is inside the inner loop not outside it were it currently is.

The issue with your code is the loop control conditions e.g. i < sizeof(typeArray). These have nothing to do with the data being accessed which is a little silly because a vector knows how many entries it holds, this one should be i < myVector.size(); and the inner loop should use j < myVector[i].size();.

To initialise you vector you can make use of the vector::assign method.
Mar 20 '12 #2
Hi Banfa,

I apologize for a late response, I posted a reply about your putting the element before the dot operator, however, it doesn't seem to have taken....so, thank you for that information, I was not aware of that method.
To me, what it means is that the "pointer" of the loop with "j" should move to the second element in the vector up to the length of the entire vector, correct?
----------------------------------
I have been working on the code, and I have it corrected.
However, I have a new dimension to the program that I would appreciate your input on:
This is updated code from first post:
Expand|Select|Wrap|Line Numbers
  1.     // Example using "sizeof" for loop maximum if arrays are the same size
  2.     for (int i = 0; i < sizeof(typeArray)/sizeof(typeArray[0]); i++)
  3.       {
  4.          myVector[i][0] = typeArray[i];
  5.          myVector[i][1] = valArray[i];
  6.        }
  7.        // print vector to screen
  8.        for (int i = 0; i < myVector.size(); i++)
  9.           {         
  10.             for (int j = 0; j < myVector[i].size(); j++)
  11.              { 
  12.               cout << myVector[i][j] << ' ';
  13.               }         
  14.               cout << '\n';
  15.           }
  16.  
OUTPUT for First Code Submission:
55 1
66 2
77 3
------------------------------
Now, I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. I am looking for each row of columns AFTER the 55, 66, 77, 88 to be populated with each number of "valArray."
-----------------
Here is the code and output:
Expand|Select|Wrap|Line Numbers
  1.  int typeArray[4] = {55,66,77,88};
  2.     int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
  3.  
  4.     // 4 = LENGTH or NUMBER of ROWS; 13 = WIDTH or NUMBER of COLUMNS;
  5.     //  0 = VALUE all cells are initialized to
  6.      vector< vector <int> > myVector(4, vector<int> (13,0));
  7.  
  8.       for (int i = 0; i < myVector.size(); i++) 
  9.         {
  10.             myVector[i][0] = typeArray[i];
  11.         for (int j = 0; j < myVector[i].size(); j++) 
  12.           {
  13.              myVector[1][j] = valArray[j];
  14.           }
  15.        }
  16.      // print vector to screen with 4 ROWS, 13 COLUMNS
  17.         for (int i = 0; i < myVector.size();  i++)
  18.           {         
  19.             for (int j = 0; j < myVector[i].size(); j++)
  20.              { 
  21.               cout << myVector[i][j] << ' ';
  22.               }         
  23.               cout << '\n';
  24.           }
  25.  
OUTPUT for Second Code Submission:
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
--------------------------------
Mar 24 '12 #3
Banfa
9,065 Expert Mod 8TB
Basically this all comes down to how you initialise myVector.

Before you were initialising everything to the same size so vector< vector <int> > myVector(4, vector<int> (13,0)); was working because that is exactly what it does, initialise everything to the same size.

Now you want things to be different sizes (maybe).

I am not convinced at this time that you are using the correct format for your data, before you jump into creating an array to hold your data you should be asking what data do I want to hold for 1 thing in my array. Then if required you create a class (or struct) to hold that data and then you create an array of the class (or struct).

It appears from what you have posted as if you want a data type that contains a type variable with an array of values for the type. A couple of ways you could implement this are

Expand|Select|Wrap|Line Numbers
  1. // Option 1: data is contained in the class
  2. class MyThing
  3. {
  4. public:
  5. // Class constructors and data accessors
  6.  
  7. private:
  8.     int type;
  9.     vector<int> data;
  10. };
  11.  
Expand|Select|Wrap|Line Numbers
  1. // Option 2: class subclasses vector to hold the data providing a vector with an additional type
  2. class MyThing: public vector<int> data;
  3. {
  4. public:
  5. // Class constructors to allow calling of the vector class constructors
  6. // Data accessors to access type
  7.  
  8. private:
  9.     int type;
  10. };
  11.  
Then in your code you just create a vector of MyThing, remeber with vectors you can reserve capacity without actually using it immediately and then push data into the vector as required

Expand|Select|Wrap|Line Numbers
  1. // PSEUDO CODE
  2. std::vector<MyThing> myVector;
  3.  
  4. myVector.reserve(4); // Reserve space for 4 my things
  5.  
  6. // Could be done as a loop if you have an array of types
  7. myVector.push_back(MyThing(55, <DataForType55Thing>));
  8. myVector.push_back(MyThing(66, <DataForType66Thing>));
  9. myVector.push_back(MyThing(77, <DataForType77Thing>));
  10. myVector.push_back(MyThing(88, <DataForType88Thing>));
  11.  


The output of the current code shows very clearly the square nature of the data you have allocated which is a result of the way you declared myVector at line 6 as a vector of 4 vectors of 13 ints. If you want something less square you will need more lines of code. Declare a vector of 4 vectors but size those internal vectors later.

You get 66 in your output because the inner initialisation loop overwrites the data where the 66 is held.
Mar 26 '12 #4
Hello Banfa! Again, my response is late as I'm not sure how to be notified when I have a response? I'd like to get notified by email. I have checked into this site many times and I finally looked deeper and found your answer from 2 weeks ago. I will look into how to get notifications. However, if you know that would be really helpful....anyway, I need to look over your answer in detail, and then I will respond. Next time, it will be right away, thank you again!
Apr 9 '12 #5
Banfa
9,065 Expert Mod 8TB
To automatically subscribe to threads you start/reply to then
  1. Select "Edit Profile" from the menu at the top of the screen
  2. Select "Edit Options" from the meu display of the right hand side of the screen
  3. look for "Default Thread Subscription Mode" which is just off the bottom of the screen for me and select the option you want.

Note this is in theory I have emails switched off and even if they were on they g to an account a rarely (if ever) check so I don't actually know the operational state of our email notifications.
Apr 10 '12 #6

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....
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...
2
by: =?Utf-8?B?R2VvcmR5?= | last post by:
Hello everyone, I would really appreciate if someone helped me in this matter cause I am going to lose my mind... I am using a Sony Vaio Laptop with Windows XP professional (512MB Ram, 1,7 GHz CPU...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.