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: -
#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.
2 1747
"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
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!
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: -
#include <iostream>
-
#include <vector>
-
#include <algorithm>
-
-
using namespace std;
-
-
const int typeSize = 4;
-
const int valSize = 14;
-
-
int main()
-
{
-
// arrays to be loaded into 2-dimensional int vector
-
int typeArray[typeSize] = {55,66,77,88};
-
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
-
vector< vector <int> > myVector(4, vector<int> (14,0));
-
-
// Example using a "for loop" assigning "typeArray" to the first COLUMN only
-
for (int i = 0; i < myVector.size(); i++)
-
{
-
myVector[i][0] = typeArray[i];
-
// Example using a "for loop" assigning "valArray" to each of the 4 ROWS beginning in the SECOND COLUMN
-
-
for (int j = 1; j < myVector[i].size(); j++)
-
{
-
myVector[i][j] = valArray[j - 1];
-
}
-
}
-
// print vector to screen with 4 ROWS, 14 COLUMNS
-
for (int i = 0; i < myVector.size(); i++)
-
{
-
for (int j = 0; j < myVector[i].size(); j++)
-
{
-
cout << myVector[i][j] << ' ';
-
}
-
cout << '\n';
-
}
-
-
system("Pause");
-
return 0;
-
}
-
-----------------------
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |