GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2009, 07:19
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past
Exclamation

How to display and expand the elements in a 2D matrix presented in C++ vector form.


Hi,
I tried to present a 2D mathematic matrix with C++ vector and expand it by repeating the first and last row and column. An example for intuition, I initialized a 2D C++ vector vec_2d to be as following:
1, 2, 3, 4
5, 6, 7, 8
9,10,11,12
13,14,15,16,
and I want to expand it to be:
1, 1, 2, 3, 4, 4
1, 1, 2, 3, 4, 4
5, 5, 6, 7, 8, 8
9, 9,10,11,12,12
13,13,14,15,16,16
13,13,14,15,16,16
by first repeat the first and last rows and then the updated first and last columns.

The code is as following:
CPP / C++ / C Code:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<short>vec;
	vector< vector<short> >vec_2d;
	vector< vector<short> >vec_2d_padded;
	// Initialize a 2D C++ vector.
	for (int i=1; i<17; i++)
	{
		vec.push_back(i);
		if (i%4 == 0)
		{
			vec_2d.push_back(vec);
			vec.clear();
		}
	}
	// Expand by repeating the first and last row.

	vector< vector<short> >::iterator it;
	it = vec_2d.begin();
	vec_2d.insert(it,vec_2d.front());
	vec_2d.push_back(vec_2d.back());

	vector<short>::iterator it_1d;

	// Things seem to go bad here.
	for (it=vec_2d.begin(); it<vec_2d.end(); it++)
	{
		for (it_1d=(*it).begin(); it_1d=(*it).end(); it_1d++)
		{
			cout<<" "<<*it_1d;
		}
	}
	return 0;
}

I have written the code to expand the vector by repeating the first and last rows but I do not know how to repeat the first and last columns. Another problem is, I do not know how to check if I am right by printing all the elements in the vector, the code I have written for this purpose seems to work badly. Anyone can tell me why and help me out? Thank you in advance.
nanchuangyeyu
  #2  
Old 05-May-2009, 09:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to display and expand the elements in a 2D matirx presented in C++ vector form.


Quote:
Originally Posted by nanchuangyeyu
...2D mathematic matrix with ...

Change this
CPP / C++ / C Code:
        for (it_1d=(*it).begin(); it_1d=(*it).end(); it_1d++)
to this
CPP / C++ / C Code:
        for (it_1d = (*it).begin(); it_1d < (*it).end(); it_1d++) {

You really meant to use "<" in the loop control, as you did in the outer loop, didn't you?

Anyhow, the middle part of the for() must be an expression that has a boolean value or an expression whose value can be converted to a boolean. Your expression it_ld=(*it).end() has a value that is an iterator. You can't convert a iterator to a boolean even if you wanted to for some unearthly reason, and the compiler should give an error here.

The fact that VisualC++ version 6 does not even give so much as a warning is another reason that I really don't like to recommend that compiler as a learning tool. Later versions of the Microsoft compilers correctly flag this as an error, as do all other "decent and modern" C++ compilers (Borland, GNU, etc.).

With this correction, your output should look something like
Code:
1 2 3 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 13 14 15 16
Quote:
Originally Posted by nanchuangyeyu
...how to...print... all the elements in the vector

You can use index notation.

Now since vec_2d is a vector of vectors, each element of vec_2d is a vector.

That is:
vec_2d[0] is a vector
vec_2d[1] is a vector
etc.

In matrix terminology, vec_2d[i] is denoted the ith row of the matrix.

So you can print the matrix a row at a time with something like
CPP / C++ / C Code:
    int i;
    int j;
.
.
.
    for (i = 0; i < vec_2d.size(); i++) { // vec_2d.size() is the number of rows
        for (j = 0; j < vec_2d[i].size(); j++) {// vec_2d[i].size() is the number of columns on row i
            cout << "  vec_2d[" << i << "][" << j << "] = " << vec_2d[i][j];
        }
        cout << endl;
    }
    cout << endl;

The output, using your matrix would be something like
Code:
vec_2d[0][0] = 1 vec_2d[0][1] = 2 vec_2d[0][2] = 3 vec_2d[0][3] = 4 vec_2d[1][0] = 1 vec_2d[1][1] = 2 vec_2d[1][2] = 3 vec_2d[1][3] = 4 vec_2d[2][0] = 5 vec_2d[2][1] = 6 vec_2d[2][2] = 7 vec_2d[2][3] = 8 vec_2d[3][0] = 9 vec_2d[3][1] = 10 vec_2d[3][2] = 11 vec_2d[3][3] = 12 vec_2d[4][0] = 13 vec_2d[4][1] = 14 vec_2d[4][2] = 15 vec_2d[4][3] = 16 vec_2d[5][0] = 13 vec_2d[5][1] = 14 vec_2d[5][2] = 15 vec_2d[5][3] = 16

Now one final note: With a vector of vectors, it may be possible that not all of the 'rows' have the same number of elements. With matrices, they do. Let's say that a particular matrix has numrows rows and numcols columns.

Then, after noting the hints in the comments my little example, the print loop could be written a little more elegantly as something like
CPP / C++ / C Code:
    for (i = 0; i < numrows; i++) {
        for (j = 0; j < numcols; j++) {
            cout << "  vec_2d[" << i << "][" << j << "] = " << vec_2d[i][j];
        }
        cout << endl;
    }
    cout << endl;

The index notation does not test index values to see whether they are out of range, so the programmer must be certain that the sizes are correct, otherwise undefined behavior can result. Keeping loop limits as in the previous example (using .size() as the loop conditions) will avoid going out of range, so some people prefer the slightly less elegant method. Chacun à son goût!


Regards,

Dave
Last edited by davekw7x : 05-May-2009 at 10:05.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing All Array Elements to Zero vs. Other Numbers? BobbyMurcerFan C Programming Language 6 17-Dec-2004 21:00

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:23.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.