Dynamically Allocating for Multi-Dimensional Array

I have this piece of code that may or may not be correct (feel free to comment on it, but that's not my current issue), and I'm attempting to call it from another file. The code for that as well as my question are listed below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
ExplicitRKScheme::ExplicitRKScheme(int s, const double *aa, const double *ww,
	const double *cc)
{
	stage = s;
	a = aa;
	w = ww;
	c = cc;
	k.resize(s);
}

double ExplicitRKScheme::one_step_march(double h, double tau, double ytau)
{
	double ytauplush, tauplush = tau, sum = 0, ak = 0, y = ytau;
	vector<double> x;
	x.push_back(tauplush);
	x.push_back(y);
	k[0] = G[1](x);

	// implement here
	for (int i = 0; i < stage; i++)
	{
		vector<double> x;
		tauplush = tau + c[i] * h;
		x.push_back(tauplush);
		for (int j = 0; j < i; j++)
		{
			ak = ak + a[i][j] * k[i];
		}
		y = ytau + h * ak;
		x.push_back(y);
		k[i] = h * G[1](x);
		sum = sum + w[i] * k[i];
	}

	ytauplush = ytau + h * sum;

	return ytauplush;
}


When I try to compile this, I get a multitude of errors coming from the line where I create *a. Any advice on how to fix this is greatly appreciated. I've looked through forums for similar issues, but my understanding of pointers and memory allocation isn't good enough to figure this out on my own. I've thought about how to change my above loops to handle this with a 1-dimensional array, but I can't seem to get that either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	// Run again with Runge-Kutta (variable order)
	int stage = 4;
	double *weight = new double[stage] { (1 / 6), (1 / 3), (1 / 3), (1 / 6) }; // standard RK4 Butcher Table
	double *a = new double[stage][stage] { { 0,0,0,0 } {0.5,0,0,0} {0,0.5,0} {0,0,0,1,0 } };
	double *c = new double[stage] {0, 0.5, 0.5, 1};
	ts = new ExplicitRKScheme(stage, a, weight, c);
	ts->set_function_G(0, G0);
	ts->set_function_G(1, G1);
	for (int j = 1; j <= N; j++)
	{
		Y[j] = ts->one_step_march(h, t[j - 1], Y[j - 1]);
		t[j] = t[j - 1] + h;
	}

	delete ts;
	delete c;
	delete a;
	delete[]weight;

	fileout.open("solrk.txt");
	fileout << setprecision(10) << setiosflags(ios::scientific | ios::showpos);
	for (int j = 0; j <= N; j++)
	{
		fileout << t[j] << " " << Y[j] << " " << exp(t[j]) << endl;
	}
	fileout.close();
Something like this, perhaps?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    // Run again with Runge-Kutta (variable order)
    int stage = 4;

    std::vector<double> weight = { 1.0 / 6.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 6.0 };
    std::vector<double> a = {
        0.0, 0.0, 0.0, 0.0,
        0.5, 0.0, 0.0, 0.0,
        0.0, 0.5, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0
    };

    std::vector<double> c = { 0.0, 0.5, 0.5, 1.0 };

    ExplicitRKScheme ts(stage, a.data(), weight.data(), c.data();

    ts.set_function_G(0, G0);
    ts.set_function_G(1, G1);
    for (int j = 1; j <= N; j++)
    {
        Y[j] = ts.one_step_march(h, t[j - 1], Y[j - 1]);
        t[j] = t[j - 1] + h;
    }

    {
        std::ofstream fileout("solrk.txt");
        fileout << setprecision(10) << setiosflags(ios::scientific | ios::showpos);
        for (int j = 0; j <= N; j++)
        {
            fileout << t[j] << " " << Y[j] << " " << exp(t[j]) << endl;
        }
    } // fileout closes itself here, no need for an explicit call to close.
    


Don't use dynamic memory where you don't have to, and where you find it is necessary prefer to let containers do the memory management.

In C and C++, a 2-dimensional array is more or less a one dimensional array with a little syntactic sugar.
Topic archived. No new replies allowed.