Hi,

I am having a problem with the following function:

void calcEnergy( vector< vector<double> > &Energy, vector< vector<double> > &Data, const double Constant )
{
    double Time = 6;                                           // store the change in time
    double energy ;                                      // Temp variable to store energy
    double v1,v2;

    for ( size_t i = 1; i < Data.size(); i++ )              // ignore the first row!
    {
        vector< double > Rows;
        cout << Data[ i ][ 0 ] << "\t" << Data[ i ][ 1 ] << endl;
        Rows.push_back( Data[ i ][ 0 ] );                    // push on year
        Rows.push_back( Data[ i ][ 1 ] );                    // push on month
        Rows.push_back( Time );                               // push on the time difference
        v1 = Data[ i ][ 9 ];
        v2 = Data[ i+1 ][ 9 ];
        cout << v1 << "\t" << v2 << endl;
        energy = Constant * 6.0 * ( pow( v1, 3 ) + pow( v2, 3 ) );  // calc the energy
        cout << "Energy is " << energy << endl;
        Rows.push_back( energy );                            // push on the energy

        cout << "Row vect is \t";
        for (size_t j = 0; j < Rows.size(); j++ )
            cout << setw( 7 ) << Rows[ j ];
        cout << endl;


        Energy.push_back( Rows );                            // push on energy
    }
}

The Row vector output should be giving me something like...

2009 3 6 5252

but the operation squashes the time and energy together (why is it not pushing correctly?)...

2009 3 65252

and then bombs out when trying to push Rows onto Energy, probably due to a subscript error.

Please help!

Thanks :)

I think you are not cout-ing properly. Copy-paste the entire output or exactly which cout statement out do you have here?

Ok, I have attached a jpeg of the output

The vector Data is displayed in the middle. It's contents are used in vector Row.

The bottom third shows the output from the function which should be:

2009          3
(some value)   (some value)
Energy is .....X....
Row vector is            2009        3          6          ....X....

Is this your output or this this how the output should look like?

Its the output I'm getting, which is wrong. What I didn't show is that the program bombs out and cannot continue further than what I've shown.

That's it:

v2 = Data[ i+1 ][ 9 ];

Obvious subscript error i+1 on the last element ;)

Next time use code tag with the language specifier:
[code=cplusplus] source

[/code]
DaniWeb engine shows source line numbers in that case...

It's actually not an error :) I meant to do that. Everything is right except for the Rows vector ouput

Row vect is         2009         3          63583.21

which should be

Row vect is         2009         3            6            3583.21

Why did it Rows combine Time and energy? I pushed them on separately!

Alas, it IS an error:

for (size_t i = 1; i < Data.size(); i++) {
    ... // the last loop: i = Data.size()-1
    v2 = Data[i+1][9]; // i+1 == Data.size()
    // No such element in the Data vector!!!
    ...
}

;)

Oh :P I see. But still, even before the last iteration, why doesn't vector Row output properly?

EDIT: Oh wait, nevermind! It actually is :P Sorry! It's just that the width is too small to fit it all in.

Thanks so much for this help!!!!!!!!

It looks like your house is on fire but you are searching for your slipper ;)...
Good luck!

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.