PDA

View Full Version : Operations in 2 dimensional array



Stanfillirenfro
3rd August 2012, 19:32
Hello!
I am facing a complex problem with a 2 dimensional array. I would be grateful if someone could help.
Here is my code:



std::string str1 = "ABABA";
std::string str2 = "BBBBBAAA";
double diag;
typedef std::pair<double, Direction> Cell;
matrix<Cell> m (str1.size()+1, str2.size()+1);

// Initialisation.
for(size_t i=0;i<m.size1();++i)
{
m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
}
for(size_t j=1;j<m.size2();++j)
{
m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
}
// Operations
for(size_t i = 1; i<m.size1(); ++i)
{
for(size_t j = 1; j<m.size2(); ++j)
{
diag = m.at_element(i-1, j-1).first;
diag += 1;
}
}

//Display
std::cout<<" ";
for(size_t j=0;j<m.size2 ();++j)
{
std::cout << str2[j] << " ";
}
std::cout << "\n";

for(size_t i=0;i<m.size1 ();++i)
{
if(i==0)
{
std::cout <<" ";
}
else
{
std::cout << str1[i-1]<<" ";
}

for(size_t j=0;j<m.size2();++j)
{
std::cout << m.at_element(i,j).first<<" ";
}
std::cout <<"\n";
}


The compilation is OK, but at the execution, I have only zeros in my cells.

Question: What could be the source of this mistake?
I would grateful to anyone you could help.

Thanks in advance.

spirit
3rd August 2012, 20:59
How does this affect to Qt? It's STL.

Stanfillirenfro
3rd August 2012, 21:13
I am using Qt. This is just a part of my program on which I am facing a problem.

amleto
3rd August 2012, 22:17
Your problem has nothing to do with Qt. This is a Qt programming forum...

Try here instead
http://www.qtcentre.org/forums/9-General-Programming

d_stranz
5th August 2012, 16:50
// Initialisation.
for(size_t i=0;i<m.size1();++i)
{
m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
}
for(size_t j=1;j<m.size2();++j)
{
m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
}


Even though this is the wrong place to answer a C++ / STL question...

Do you know what this matrix looks like after this initialization step? I do not know if the first index ("i") in at_element( i, j ) refers to row or column, so let's just assume it is the column index. It doesn't matter for this answer - the matrix is just transposed if i is the row index.

By the way, the std:: pair<double, Direction> constructor will put garbage into the elements of the pair. It will not initialize the double to zero or "Direction" to whatever it should hold by default.

And why do you use static_cast<float> when your Cell pair's "first" member is a double?

Your matrix is small enough that you can type it out:



Length of "ABABA" is 5 and length of "BBBBBAAA" is 8, so 5 columns and 8 rows.
Your first initialization loop goes from i = 0 - 4, and sets the double part of Cell to -i for all cells m[i, 0].
The second loop goes from j = 1 - 7 and sets the double to -j for all cells m[0, j].
The remainder of the cells are undefined.

i = 0 1 2 3 4
j = 0 0 -1 -2 -3 -4
j = 1 -1 ? ? ? ?
j = 2 -2 ? ? ? ?
j = 3 -3 ? ? ? ?
...
j = 7 -7 ? ? ? ?


Then, in your nested loops starting in line 17, you start with i = 1 and j = 1, and access m. For the first access m[0,0], this is in fact zero. For the next one m[0,1], you get -1.

So if this isn't what you intended to do when you wrote this code, you need to sit down with a pencil and a piece of paper and write out what you think your matrix [I]should look like, and figure out why your code doesn't do that.

But don't ask here. Do as Amleto said and move your discussion to the general programming section.