0

I'm a beginner in C++ (physics undergraduate) and am currently taking a course on computational physics. I have an exercise to numerically solve - using Euler's method - a 'flow problem' for a 3-level-system.

That is, I want to compute the values of a 2-dimensional array like so

double flow(double values, const double W12, const double W13, const double W23)
{
    for(int i = 1; i<STEPS; i++)
    {
        values[i][0] = -W12*values[i-1][0] + W13*values[i-1][2];
        values[i][1] = -W23*values[i-1][1] + W12*values[i-1][0];
        values[i][2] = -W13*values[i-1][2] + W23*values[i-1][1];

    return values;
    }
}

However, when compiling this, I get an error saying 'invalid types 'double[int]' for array subscript', and I don't know what the problem is exactly (as I said, beginner). Shouldn't this code take the values from the row above to compute the current row (3 values), then move to the next, etc.?

The complete code looks like this (Euler not included, still working on this. First I just want the values)

#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>

#define DIM 3       // for 3 level system
#define STEPS 1000 // 10000 steps

using namespace std;

double flow(double values, const double W12, const double W13, const double W23)
{
    for(int i = 1; i<STEPS; i++)
    {
        values[i][0] = -W12*values[i-1][0] + W13*values[i-1][2];
        values[i][1] = -W23*values[i-1][1] + W12*values[i-1][0];
        values[i][2] = -W13*values[i-1][2] + W23*values[i-1][1];

    return values;
    }
}



int main()
{
    const double E1 = 10; // define energy levels
    const double E2 = 20;
    const double E3 = 16;
    const double W12, W13, W23; // initialize spacings

double N1 = 1000; // initial fill values for energy levels N1-Ni
double N2 = 1000;
double N3 = 1000;

W12 = E2 - E1; // calculate spacings
W13 = E3 - E1;
W23 = E3 - E2;

double values [STEPS][DIM]; // setup a (3 x steps) array
values [0][0] = N1;         // fill the first row with our initial values
values [0][1] = N2;
values [0][2] = N3;

values.reserve(STEPS);          // reserve memory

//ofstream myfile;            // setup writing to file
//myfile.open("values.txt");  


flow(values, W12, W13, W23);


void printArray(double (&values)[STEPS][DIM])
{
    for(int x = 0; x < STEPS; x++){
        for(int y = 0; y < ROW; y++){
            cout << values[x][y] << " ";
        }
        cout << endl;
    }
}

// myfile.close();                      
return 0;
}

I realize this is probably a very noobish question, and the answer may be obvious, but I can't for my life figure it out.

Any help would be greatly appreciated :)

6
  • 5
    Where is your array in the function? Commented Oct 13, 2014 at 12:32
  • you are passing values as a variable, instead of a pointer. Commented Oct 13, 2014 at 12:34
  • So would it have to be flow(double (&values)[STEPS][DIM],...)? That works out, but then I get erros with 'a function definition is not allowed befoe '{' token' and 'expected '}''at end of input, which sounds like there are unmatched brackets, but there are none that I could find. Commented Oct 13, 2014 at 12:45
  • You should not declare void printArray(double (&values)[STEPS][DIM]) inside main function, you should declare it outside. Commented Oct 13, 2014 at 12:48
  • Fixed with some clean up : ideone.com/GLkMgA Commented Oct 13, 2014 at 13:01

2 Answers 2

1

You can try this code for the flow function. You can return an int, you'll have the array values modified by the function, there's no need to return an array. When an array is a parameter of a function (without the const keyword) then any changes made to the values in the array will be seen in the original calling function.

int flow(double values[STEPS][DIM], const double W12, const double W13, const double W23)
{
    for(int i = 1; i<STEPS; i++)
    {
        values[i][0] = -W12*values[i-1][0] + W13*values[i-1][2];
        values[i][1] = -W23*values[i-1][1] + W12*values[i-1][0];
        values[i][2] = -W13*values[i-1][2] + W23*values[i-1][1];
    }
    return 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass your array as an array, rather than as a plain double which is a scalar.

You already do something like this (passing as an array) for your printArray() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.