C++ 2D vector does not dispay the correct values

89 Views Asked by At

When data is copied from a vector to a 2D-vector, the data is copied and displayed correctly inside the loop but after the loop the data is displayed wrong. The data is also copied to a 2D-array where it displays correct.

Its about the positions [0][6], [0][7], [1][6] and [1][7]. The last row ([2][6], [2][7]) is correct. If nSets is changed then the last row is always correct but the last 2 positions of the previous rows are corrupted.

#include <iostream>
#include <vector>
using namespace std;

int nCol = 8;
int nSize = 4;
int nLookBack = 2;
int nSets = 3;
int nInputs = 4;
int nArray[3][8];


int main()
{
    // create dataset

    vector<int> nData(80);

    for(int i = 0; i < 80; i++)
       {
        nData[i] = i+1;
       }

    // show data

    for(int i = 0 ; i < 80; i+=nCol)
       {
         for(int j = 0; j < nCol; j++)
            {
              cout << nData[i+j] << "\t";
            }
      cout << endl;
       }

  cout << endl << endl;

  // +-----------------------------------------------------

  vector<vector<double>> nVector( nSets , vector<double> (nInputs));

  //copy from (vector) nData to nVector and to nArray
  cout << "from inside the loop :"  << endl;

  for(int i = 0; i < nSets  ; i++)
       {
        for(int j = 0; j< nLookBack ; j++)
           {
            // copy data in 2D-vector
             nVector[i] [(j * nSize ) + 0 ] = nData[ ((i+j) * nCol) + 0] ;
             nVector[i] [(j * nSize ) + 1 ] = nData[ ((i+j) * nCol) + 1] ;
             nVector[i] [(j * nSize ) + 2 ] = nData[ ((i+j) * nCol) + 2] ;
             nVector[i] [(j * nSize ) + 3 ] = nData[ ((i+j) * nCol) + 3] ;

             //show vector data from inside the loop

             cout << "[" << i << "][" << (j * nSize ) + 0 << "] = " << nVector[i] [(j * nSize ) + 0 ]  << "\t" ;
             cout << "[" << i << "][" << (j * nSize ) + 1 << "] = " << nVector[i] [(j * nSize ) + 1 ]  << "\t" ;
             cout << "[" << i << "][" << (j * nSize ) + 2 << "] = " << nVector[i] [(j * nSize ) + 2 ]  << "\t" ;
             cout << "[" << i << "][" << (j * nSize ) + 3 << "] = " << nVector[i] [(j * nSize ) + 3 ]  << "\t" ;

             // copy data in 2-D array
             nArray[i] [(j * nSize ) + 0 ] = nData[ ((i+j) * nCol) + 0] ;
             nArray[i] [(j * nSize ) + 1 ] = nData[ ((i+j) * nCol) + 1] ;
             nArray[i] [(j * nSize ) + 2 ] = nData[ ((i+j) * nCol) + 2] ;
             nArray[i] [(j * nSize ) + 3 ] = nData[ ((i+j) * nCol) + 3] ;
           }
       cout << endl;
       }

    // +-----------------------------------------------------

    // show data in 2D vector

    cout << endl << "2D-vector: "  << endl;

    for(int i = 0; i < nSets ; i++)
       {
        for(int j = 0; j < (nLookBack * nSize); j++)
          {
            cout  << " [" << i << "][" << j << "] = " << nVector[i][j] << "\t" ;
          }
        cout << endl;
       }

    // +-----------------------------------------------------

    // show data in 2-D array

    cout << endl << "2D-array: "  << endl;

    for(int i = 0; i < nSets ; i++)
       {
        for(int j = 0; j < (nLookBack * nSize); j++)
          {
            cout  << " [" << i << "][" << j << "] = " << nArray[i][j] << "\t" ;
          }

       cout << endl;
       }

    // +-----------------------------------------------------

    return 0;
   }

from inside the loop : [0][0] = 1 [0][1] = 2 [0][2] = 3 [0][3] = 4 [0][4] = 9 [0][5] = 10 [0][6] = 11 [0][7] = 12 [1][0] = 9 [1][1] = 10 [1][2] = 11 [1][3] = 12 [1][4] = 17 [1][5] = 18 [1][6] = 19 [1][7] = 20 [2][0] = 17 [2][1] = 18 [2][2] = 19 [2][3] = 20 [2][4] = 25 [2][5] = 26 [2][6] = 27 [2][7] = 28

2D-vector: [0][0] = 1 [0][1] = 2 [0][2] = 3 [0][3] = 4 [0][4] = 9 [0][5] = 10 [0][6] = 9 [0][7] = 10
[1][0] = 9 [1][1] = 10 [1][2] = 11 [1][3] = 12 [1][4] = 17 [1][5] = 18 [1][6] = 17 [1][7] = 18
[2][0] = 17 [2][1] = 18 [2][2] = 19 [2][3] = 20 [2][4] = 25 [2][5] = 26 [2][6] = 27 [2][7] = 28

2D-array: [0][0] = 1 [0][1] = 2 [0][2] = 3 [0][3] = 4 [0][4] = 9 [0][5] = 10 [0][6] = 11 [0][7] = 12
[1][0] = 9 [1][1] = 10 [1][2] = 11 [1][3] = 12 [1][4] = 17 [1][5] = 18 [1][6] = 19 [1][7] = 20
[2][0] = 17 [2][1] = 18 [2][2] = 19 [2][3] = 20 [2][4] = 25 [2][5] = 26 [2][6] = 27 [2][7] = 28

This problem happens in C++ Eclipse and in CodeBlocks. I hope somebody has an answer to this problem. I am out of clues.

Osiris

0

There are 0 best solutions below