Hi everyone:

I have written C++ code for populating a matrix/2D array with calculation results. Each column of the array represents results for a distinct time step.

My problem arises because the rows of my results vector do NOT correspond to the rows of the matrix. The matrix includes extra rows (I need these to store other information), so the matrix rows do NOT correspond to the vector rows.

I have written code to specify where in the matrix to store each vector element, but it is generating incorrect results. Instead of putting each vector element in the right place in the matrix for each time step (column), my code is taking the LAST element of each vector and filling an entire matrix column with it.

I would very much appreciate any help in figuring out where I'm going wrong! My code is below:

for (int j = 1; j <= someTime; j++)   // Each j is a single time step
{ 
// Do calculation and get results vector (not included here)

  for (int i = 0; i < (d_x * d_x); i++)  // Each i is an element in the results vector
  {
    for (int k = 0; k < (((d_x + 2) * (d_x + 2)) - (d_x + 2)); k++)
    { // the code for k specifies where in the matrix each vector element should go.
      if ( k % (d_x + 2) != 0 && k > (d_x + 1))
      {
        if ( (k - d_x - 1) % (d_x + 2) != 0 )
        {
          solutionMatrix(k,j) = temperatureNew[i];  // Put each element of results vector (temperatureNew) in the appropriate place in the matrix.
        }
      }
    }
  }
}

It's because your variable/iterator "i" doesn't change until after your third loop completes everything it does. You need to find a way to change "i" more often.

Hint: It is possible to have more than 1 iterator and/or exit condition in a particular for loop. Have a look at the little program below. Before you compile and run it, try to guess what it does and what the output is. There is nothing tricky in it, I'm not doing anything you shouldn't already be familiar with.

#include <iostream>

using namespace std;

int main()
{
  for (int it1 = 0, it2 = 10; (it1 < it2) && (it2 < 15); it1+=2, it2++) {
    cout << "it1 = " << it1 << " it2 = " << it2 << endl;
  }
  cin.get();
  return 0;
}
commented: Thanks Fbody. Your suggestion is really helpful. +1

Hi Fbody:

Thanks a lot for your suggestion. I was not aware that it's possible to have more than one iterator in a loop! This is great to know.

In your example, it1 starts at 0 and it2 starts at 10. For it1 less than it2 and it2 less than 15, the cout expression is outputted. The it1 step is different than the it2 step.

I have been experimenting with this concept and I'm getting a little closer to what I need.... But I'm still not there! Some of the first elements from my vector are not in the matrix. The later vector elements are in the matrix, but they appear in the wrong (too early) rows. I will keep tinkering!

Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.