Need help with using arrays and vectors in functions

Hi, I need to put some equation into array(located inside function). This equation was made inside class (with method) and passed to vector. The equation is further used in another source code. The compiler doesn't give any error, but program can't start and then there is a windows error. If I write equation[1] mannually like other 2 it works perfectly. I'm new with C++, and learning it for few weeks now. These are the important parts of my code, I can post it whole if needed, please help.


This is method inside class:
1
2
3
4
5
     vector<double>eqP;
            virtual void SetParam(double d)
            {   double x[3]; double equationE
                equationE = (-0.5*x[0]*d);
                eqP.push_back(equationE);}


This is function which is used in another source code:
1
2
3
4
5
6
7
8
9
10

double equation[3];

void f(double t, double x[3]) // These pamarameters are passed inside this       
                              // function by the other source code (which is        
                              // using this function)
{
        equation[0] = eqP[0];
	equation[1] = 0.5*x[0]-0.25*x[1];
	equation[2] = 0.25*x[1]-0.1666*x[2];}


I will have to replace the other 2 equations as well, with eqP[1] and [2] but I want to try it by steps if it works. Also there will be more variables then d, in class method, these variables are calculated inside another method of class (this works fine).

Thank you for any help.

Last edited on
You'll have to post some more code, at least your main function.
Ok so this is my 1st main, it's an Runge-Kuttta program:
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include "Difer_Eq.h"
using namespace std;


void f(double t, double x[P]);
int main()
{
    int i,j;
  	int T = 5;
    double h = 0.1;
	int ch=((1/h)+1);
	double t[ch*T];
    double x[P];


    t[0] = 0;
    x[0] = 1;
    x[1] = 1;
    x[2] = 1;
    x[3] = 0;
    x[4] = 0;
    x[5] = 0;
    x[6] = 0;

    ofstream output;
    output.open ("outputgraph1.txt");

	for(i = 0; h*(i-1)< T; i++)
	{

		double x_new[P];
        double x_ar[P];
        vector<double> k1,k2,k3,k4, xnew;


		f(t[i],x);
		for(j = 0; j < P; j++)
		{   k1.push_back(h * equation[j]);
			x_ar[j] = x[j] + 0.5*k1[j];}


		f(t[i] + 0.5 * h, x_ar);
		for(j = 0; j < P; j++)
		{   k2.push_back(h * equation[j]);
			x_ar[j] = x[j] + (0.5 * k2[j]);}



		f(t[i] + 0.5 * h, x_ar);
		for(j = 0; j < P; j++)
		{   k3.push_back(h * equation[j]);
			x_ar[j] = x[j] + k3[j];}


		f(t[i] + h, x_ar);
		for(j = 0; j < P; j++)
		{k4.push_back(h * equation[j]);}


		for(j = 0; j < P; j++)
		{xnew.push_back(x[j] + ((k1[j] + 2*k2[j] + 2*k3[j] + k4[j])/6));}


		cout <<  t[i] << "\t";
		cout <<  x[0] << "\t\t";
		cout <<  x[1] << "\t";
		cout <<  x[2] << "\t";
		cout <<  x[3] << "\n";


        output << t[i] << "\t";
        output << x[0] << "\t\t";
	output << x[1] << "\t\t";
        output << x[2] << "\t";
        output << x[3] << "\n";


	x[0] = xnew[0];
	x[1] = xnew[1];
        x[2] = xnew[2];
        x[3] = xnew[3];
        x[4] = xnew[4];
        x[5] = xnew[5];
        x[6] = xnew[6];


        t[i+1] = t[i]+h;


        k1.clear();
        k2.clear();
        k3.clear();
        k4.clear();
        xnew.clear();
	}

    output.close();
	return 0;
}


The code uses this function, already mentioned it's called "Difer_Eq.h" (number of equation is not important....):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
#include "Elementys.h"

using namespace std;

void f(double t, double x[P])
{
        equation[0] = -0.5*x[0];         // here I need eqP[0] from elementys.h
	equation[1] = 0.5*x[0]-0.25*x[1];
	equation[2] = 0.25*x[1]-0.1666*x[2];
	equation[3] = 0;
	equation[4] = 0;
	equation[5] = 0;
	equation[6] = 0;
}


Then there is another main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <cmath>
#include "Elementys.h"
using namespace std;


int main ()
{
    int N;
    vector<object>PotVect;

for (N=0; N<6;N++)
{
  	object *point_pot = new p(5.0,5.0,5.0,5.0,5.0);   //there will be allways another input from text file
    point_pot -> Calc();
	point_pot -> SetParam(5);
	PotVect.push_back(*point_pot);

}
    return 0;
}


And finnally here are the objects, it's called "Elementys.h" (will be more of them later):
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
    const double Pi = 3.14159;
    const double Ro = 1000;
    const double v = 5.0;
    const double V = 5.0;
    const int P=7;

double equation[P];
double x[P];

vector<double>eqP;
class object
{    
protected:
    int type;            //These will be used later
    int Number;   
    int Group;
    
public:
    object() {}
    virtual ~object (){}

    virtual void Calc(){}
    virtual void SetParam(double s){}
};


class p : public object
    {
       protected:
            double d;
            double min;
            double max;
            double ksi_m;
            double l;

        public:
           p(double diam, double leng,double z_begin, double z_end, double ksi_loc)
           { d=diam;
             l=leng;
             z_begin=min;
             z_end=max;
             ksi_loc=ksi_m;}
        virtual ~p(){};


        double Re; double ka; double lambda;
        virtual void Calc()
           {
               Re=((d*v)/V);
               ka=(Pi*pow(d,2.0)/(4.0*Ro*l));
               if (Re < 2300.0)
               lambda = (64.0/Re);
               else
               lambda = (0.3164/(pow(Re,0.25)));
            }

            virtual void SetParam(double s)
            {   double equationE;
                equationE = (-0.5*x[0]*Re*s*d+ka*x[1]);        //I altered the equation a bit
                eqP.push_back(equationE);
                { cout << ka << "\t" << lambda << "\t" << Re <<"\t";}}

    };


It's not everything in one main function, but it will be in the end.
Everything works fine, however I need to create lot of objects with lot of different functions, and then pass them into RK program. I repeat that I'm new into C++ so maybe there are some stupid things in code (I believe you could write it in better way)
Last edited on
Ok, so I put the whole thing under one main function and now it can be compiled and started, however the term eqP[0]=-0.5*x[0]*Re*s*d+ka*x[1] is taken as zero, and it's not used with RK-system as an array. Any advice how to get it to work?
Last edited on
Topic archived. No new replies allowed.