compilation error

can someone just compile this and tell me what exactly the error do this code have?
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
#include "stdafx.h"
#include<iostream>
using namespace std;

double f1(int n,double *yy1,double *yy2,double *yy3)
	{
		double f1 = -0.01*yy1[n] + 10*yy2[n]*yy3[n];
		return f1;
	}

double f2(int n,double *yy1,double *yy2,double *yy3)
	{
		double f2 = 0.01*yy1[n] - 10*yy2[n]*yy3[n]-10000*yy2[n]*yy2[n];
		return f2;
	}

double f3(int n,double *yy1,double *yy2,double *yy3)
	{
		double f3 = 10000*yy2[n]*yy2[n];
		return f3;
	}

int main()
{
	int i=0;
	double *y1 = new double[i];
	double *y2 = new double[i];
	double *y3 = new double[i];
	double k11=0,k21=0,k31=0,k41=0,k12=0,k22=0,k32=0,k42=0,k13=0,k23=0,k33=0,k43=0,h=0.0001;
	//double y1[0]=1,y2[0]=0,y3[0]=0;
	//int nSize = 12;
	 // note: nSize does not need to be constant!
	//pnArray[4] = 7;
	//delete[] pnArray;
	
	for (i=0; i<3; i++)
	{
		y1[0]=1,y2[0]=0,y3[0]=0;
		k11 = -0.01*y1[i] + 10*y2[i]*y3[i];
		k21 = -0.01*(y1[i] + .5*h*k11) + 10*(y2[i] + .5*h*k11)*(y3[i]+.5*h*k11);
		k31 = -0.01*(y1[i] + .5*h*k21) + 10*(y2[i] + .5*h*k21)*(y3[i]+.5*h*k21);
		k41 = -0.01*(y1[i] + h*k31) + 10*(y2[i] + h*k31)*(y3[i]+h*k31);
		y1[i+1] = y1[i] + (h*(k11+2*k21+2*k31+k41))/6;
		//cout << "*" <<endl;

		k12 = 0.01*y1[i] - 10*y2[i]*y3[i]-10000*y2[i]*y2[i];
		k22 = 0.01*(y1[i] + .5*h*k12) - 10*(y2[i] + .5*h*k12)*(y3[i]+.5*h*k12)-10000*(y2[i]+.5*h*k12)*(y2[i]+.5*h*k12);
		k32 = 0.01*(y1[i] + .5*h*k22) - 10*(y2[i] + .5*h*k22)*(y3[i]+.5*h*k22)-10000*(y2[i]+.5*h*k22)*(y2[i]+.5*h*k22);
		k42 = 0.01*(y1[i] + h*k32) - 10*(y2[i] + h*k32)*(y3[i]+h*k32)-10000*(y2[i]+h*k32)*(y2[i]+h*k32);
		y2[i+1] = y2[i] + (h*(k12+2*k22+2*k32+k42))/6;
		//cout << "**" <<endl;

		k13 = 10000*y2[i]*y2[i];
		k23 = 10000*(y2[i]+.5*h*k13)*(y2[i]+.5*h*k13);
		k33 = 10000*(y2[i]+.5*h*k23)*(y2[i]+.5*h*k23);
		k43 = 10000*(y2[i]+h*k33)*(y2[i]+h*k33);
		y3[i+1] = y3[i] + (h*(k13+2*k23+2*k33+k43))/6;
		//cout << "***" <<endl;
		//fprintf (rkFiley1, " %lf\n",y1[i]1);
		//fprintf (rkFiley2, " %lf\n",y2[i]1);
		//fprintf (rkFiley3, " %lf\n",y3[i]1);
		//y1[i] = y1[i]1;
		//y2[i] = y2[i]1;
		//y3[i] = y3[i]1;
	}
	double t= 0.0003;
	//int i=3;
	
	while (t<=100)
	{
		t = t + h ;
		y1[i+1] = y1[i] + (h*(55*f1(i,&y1[i],&y2[i],&y3[i]) - 59*f1(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + 37*f1(i-2,&y1[i-2],&y2[i-2],&y3[i-2]) - 9*f1(i-3,&y1[i-3],&y2[i-3],&y3[i-3]))/24); 
		y1[i+1] = y1[i] + (h*(8*f1(i+1,&y1[i+1],&y2[i+1],&y3[i+1]) + 19*f1(i,&y1[i],&y2[i],&y3[i]) -+ 5*f1(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + f1(i-2,&y1[i-2],&y2[i-2],&y3[i-2]))/24); 

		y2[i+1] = y2[i] + (h*(55*f2(i,&y1[i],&y2[i],&y3[i]) - 59*f2(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + 37*f2(i-2,&y1[i-2],&y2[i-2],&y3[i-2]) - 9*f2(i-3,&y1[i-3],&y2[i-3],&y3[i-3]))/24); 
		y2[i+1] = y2[i] + (h*(8*f2(i+1,&y1[i+1],&y2[i+1],&y3[i+1]) + 19*f2(i,&y1[i],&y2[i],&y3[i]) -+ 5*f2(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + f2(i-2,&y1[i-2],&y2[i-2],&y3[i-2]))/24); 

		y3[i+1] = y3[i] + (h*(55*f3(i,&y1[i],&y2[i],&y3[i]) - 59*f3(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + 37*f3(i-2,&y1[i-2],&y2[i-2],&y3[i-2]) - 9*f3(i-3,&y1[i-3],&y2[i-3],&y3[i-3]))/24); 
		y3[i+1] = y3[i] + (h*(8*f3(i+1,&y1[i+1],&y2[i+1],&y3[i+1]) + 19*f3(i,&y1[i],&y2[i],&y3[i]) -+ 5*f3(i-1,&y1[i-1],&y2[i-1],&y3[i-1]) + f3(i-2,&y1[i-2],&y2[i-2],&y3[i-2]))/24); 
		i++;
	}
	cout << y1 << endl;
	cout << y2 << endl;
	cout << y3 << endl;
	return 0;
}

http://www.cplusplus.com/forum/beginner/1/

One issue I see immediately is how you are attempting to use arrays. There are places you only access one element (and probably out of bounds) where you might have meant to iterate over all the elements.
In the first few lines of main(), you allocate 0 spaces in all your calls to new (not to mention that if you had allocated memory, you forgot to free it at the end).

I really suggest you read over these pages:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/dynamic/
Post the error message.

Future tip, choose better variable names. These mean absolutely nothing to anyone else reading your code, and probably won't to you either in a week. Makes code much harder to read when all your variable names are non-descript and very similar to each other.
actually its a numerical method for solving differential equations named adams-moulton method...which is gaining its starting 3 values from runge kutta method..and all variables have their usual meanings y,t,f...etc.

it stops in between and asks me to debug it..on debugging it shows this....
"Unhandled exception at 0x0FF63BFC (msvcp110d.dll) in tut8 adams.exe: 0xC0000005: Access violation reading location 0x25ECDB56."

and asks break, continue or cancel.
The error is from dereferencing memory addresses that are out-of-bounds (your arrays). Since all of your dynamic arrays have size 0, any dereference will count as out-of-bounds access. Try changing line 25 to int i = 3;. And don't forget to free the memory at the end of the program.
the first for loop runs successfully because at i=8 it gets stuck, i checked it using the local debugger...
after changing i=3 it is stopping at i=10...
and one thing does it matter what i allocate the dynamic array in the starting because it again gets allocated it in the loop?
When it comes to undefined behavior, it tells you little if the program is stuck at I=8 or I=10. It could so happen that at those indices you run into read-only memory which causes the program to throw an exception if you attempt to write to it.

When I say allocation, I mean a call to new or malloc(). At the start of your program, you allocated memory for double arrays of size i. However, i has been set to zero, so all of your arrays are of size zero. There is nothing to access, and any attempts will be grabbing memory you should not be using.

I don't really know how much space you need, so I was probably wrong in suggesting you start with 3. According to the conditions of your while loop, you might need about 100/0.0003 units plus an additional 3 since you are continuing from 3 after your for loop. I am not familiar with the algorithm, so take this paragraph with a grain of salt.

Random note:
For your three functions: f1, f2, and f3, do you really need to pass pointers and indices? Really, you are only using one element out of the array in these functions, so they don't have to care about arrays:
1
2
3
double f1(double yy1, double yy2, double yy3){
    return -0.01*yy1+10*yy2*yy3;
}

And when you call it, just have a simple:
 
f1(yy1[i], yy2[i], yy3[i]);

In fact, now that I looked closer at your calls, I realize that is another point of accessing out-of-bound areas. For example, if the size of your array is 10, and i happens to be 6:
 
/*...*/f1(i,&y1[i],&y2[i],&y3[i])/*...*/

Then the execution in f1 becomes:
 
return -0.01*yy1[12] + 10*yy2[12]*yy3[12]; //Clearly out-of-bounds 

The reason why the access point is 12 is because you passed (yyN + i), so you don't actually start at the beginning. So within the function, you are using (yyN + 2*i).

Also, avoid creating variables that have the same name as the function. This will cause ambiguity and the program may or may not accidently use the function address instead of your variable.
Last edited on
ok...you pointed out a very informative thing about using the arrays in the functions...i corrected that..
also i changed the variables names in the functions...
also actually i need 100/0.0001 no of elements in total out of which i have got 3 from the for loop..and i want to start the while with t=0.0003..
the same thing is happening again it is stuck at i=8...what can i do to make it run through...
i want to sincerely thank you for your help, it is actually a very good learning conversation with you.
i have allocated 0 memory to the arrays then how does the first for loop runs successfully...shouldn't it stop in the first step only?
1
2
3
4
5
_OutIt __CLR_OR_THIS_CALL put(_OutIt _Dest,
		ios_base& _Iosbase, _Elem _Fill, double _Val) const
		{	// put formatted double to _Dest
		return (do_put(_Dest, _Iosbase, _Fill, _Val));
		}

the debugger shows an arrow at this snippet on the return statement...
shouldn't it stop in the first step only?

It might, but it might not. There is no way to know if the program will catch an out-of-bounds access. For example, I once had two arrays declared next to each other like this:
1
2
char arr1[N];
char arr2[M][L];

I wrote a function that was only supposed to mess with arr1; however, it went out-of-bounds and changed values in arr2. My program never crashed, and the only consequence was the display filled with nonsense information.
Out-of-bounds indices will let you access memory you shouldn't be using, but C++ does not forbid you from doing so. It is pretty much your job to be careful with what memory you dabble with.

As to why your program is still crashing, I wish I could pinpoint exactly where it is going wrong. It is a little hard to understand your program because there are so many variables at play at once. Can you put up your revised code (or edit your original post to show the new code)? And perhaps you could rearrange some things to make the code a little cleaner?

By the way, I noticed some -+ on lines 73, 76, and 79.

Edit:
Just noticed your post. That makes the problem appear to come from lines 82 to 84 since that is the only place you use anything from ios_base... But it shouldn't give you an error. Rather, it'd just print out the addresses y1, y2, and y3 have stored. But just in case, a way to print array values:
1
2
for(size_t I(0); I < array_size; ++I)
    cout << array[I] << '\n';

Of course, you'd have to edit the format to suit your needs.
Last edited on
ya sure i ll try to do that....and yes i also noticed that -+ and i corrected it..
i ll post the edited code in a while...
thanks..
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
#include "stdafx.h"
#include<iostream>
using namespace std;

double f1(double yy1,double yy2,double yy3)
	{
		double ff1 = -0.01*yy1 + 10*yy2*yy3;
		return ff1;
	}

double f2(double yy1,double yy2,double yy3)
	{
		double ff2 = 0.01*yy1 - 10*yy2*yy3-10000*yy2*yy2;
		return ff2;
	}

double f3(double yy1,double yy2,double yy3)
	{
		double ff3 = 10000*yy2*yy2;
		return ff3;
	}

int main()
{
	int i=0;
	double *y1 = new double[i];
	double *y2 = new double[i];
	double *y3 = new double[i];
	double k11=0,k21=0,k31=0,k41=0,k12=0,k22=0,k32=0,k42=0,k13=0,k23=0,k33=0,k43=0,h=0.0001;
	y1[0]=1,y2[0]=0,y3[0]=0;
	for (i=0; i<3; i++)
	{
		
		k11 = -0.01*y1[i] + 10*y2[i]*y3[i];
		k21 = -0.01*(y1[i] + .5*h*k11) + 10*(y2[i] + .5*h*k11)*(y3[i]+.5*h*k11);
		k31 = -0.01*(y1[i] + .5*h*k21) + 10*(y2[i] + .5*h*k21)*(y3[i]+.5*h*k21);
		k41 = -0.01*(y1[i] + h*k31) + 10*(y2[i] + h*k31)*(y3[i]+h*k31);
		y1[i+1] = y1[i] + (h*(k11+2*k21+2*k31+k41))/6;
		cout << y1[i+1] <<endl;

		k12 = 0.01*y1[i] - 10*y2[i]*y3[i]-10000*y2[i]*y2[i];
		k22 = 0.01*(y1[i] + .5*h*k12) - 10*(y2[i] + .5*h*k12)*(y3[i]+.5*h*k12)-10000*(y2[i]+.5*h*k12)*(y2[i]+.5*h*k12);
		k32 = 0.01*(y1[i] + .5*h*k22) - 10*(y2[i] + .5*h*k22)*(y3[i]+.5*h*k22)-10000*(y2[i]+.5*h*k22)*(y2[i]+.5*h*k22);
		k42 = 0.01*(y1[i] + h*k32) - 10*(y2[i] + h*k32)*(y3[i]+h*k32)-10000*(y2[i]+h*k32)*(y2[i]+h*k32);
		y2[i+1] = y2[i] + (h*(k12+2*k22+2*k32+k42))/6;
		cout << y2[i+1] <<endl;

		k13 = 10000*y2[i]*y2[i];
		k23 = 10000*(y2[i]+.5*h*k13)*(y2[i]+.5*h*k13);
		k33 = 10000*(y2[i]+.5*h*k23)*(y2[i]+.5*h*k23);
		k43 = 10000*(y2[i]+h*k33)*(y2[i]+h*k33);
		y3[i+1] = y3[i] + (h*(k13+2*k23+2*k33+k43))/6;
		cout << y3[i+1] <<endl;
		}
	double t= 0.0003;
	while (t<100)
	{
		
		y1[i+1] = y1[i] + (h*(55*f1(y1[i],y2[i],y3[i]) - 59*f1(y1[i-1],y2[i-1],y3[i-1]) + 37*f1(y1[i-2],y2[i-2],y3[i-2]) - 9*f1(y1[i-3],y2[i-3],y3[i-3]))/24); 
		y1[i+1] = y1[i] + (h*(8*f1(y1[i+1],y2[i+1],y3[i+1]) + 19*f1(y1[i],y2[i],y3[i]) - 5*f1(y1[i-1],y2[i-1],y3[i-1]) + f1(y1[i-2],y2[i-2],y3[i-2]))/24); 
		cout << y1[i+1] <<endl;

		y2[i+1] = y2[i] + (h*(55*f2(y1[i],y2[i],y3[i]) - 59*f2(y1[i-1],y2[i-1],y3[i-1]) + 37*f2(y1[i-2],y2[i-2],y3[i-2]) - 9*f2(y1[i-3],y2[i-3],y3[i-3]))/24); 
		y2[i+1] = y2[i] + (h*(8*f2(y1[i+1],y2[i+1],y3[i+1]) + 19*f2(y1[i],y2[i],y3[i]) - 5*f2(y1[i-1],y2[i-1],y3[i-1]) + f2(y1[i-2],y2[i-2],y3[i-2]))/24); 
		cout << y2[i+1] <<endl;

		y3[i+1] = y3[i] + (h*(55*f3(y1[i],y2[i],y3[i]) - 59*f3(y1[i-1],y2[i-1],y3[i-1]) + 37*f3(y1[i-2],y2[i-2],y3[i-2]) - 9*f3(y1[i-3],y2[i-3],y3[i-3]))/24); 
		y3[i+1] = y3[i] + (h*(8*f3(y1[i+1],y2[i+1],y3[i+1]) + 19*f3(y1[i],y2[i],y3[i]) - 5*f3(y1[i-1],y2[i-1],y3[i-1]) + f3(y1[i-2],y2[i-2],y3[i-2]))/24); 
		cout << y3[i+1] <<endl;

		t = t + h ;
		i++;
	}
	
	return 0;
}

sorry i could'nt do much because the equations are that long already...i cannot do any thing else to shorten it..otherwise i have made the changes u told me..
You still have not changed the first few lines of main() to allocate the necessary storage space. From one of your previous posts, you could at least try:
1
2
3
4
5
const double delta_t(1E-4); //shorthand for 0.0001
const size_t total_samples(100/delta_t); //100/0.0001 = 10^6 This actually seems a bit high for allocation. You may want to consider lowering it.
double *y1 = new double[total_samples];
double *y2 = new double[total_samples];
double *y3 = new double[total_samples];


size_t is a type alias in <cstddef> that is an unsigned integral type. I used it since you are you using a million spaces. unsigned long int will probably be fine, too.

Allocate the space you need (and free the memory at the end of the program), and see if it still crashes.
it is not crashing now....thanks man...but my values of y are coming out to be some
-1.#INF
i think that's something to do with the logic. is it?
I believe that has to do with division by 0.
k...it will chk that..thanx..u helped alot..
i will get back to you if i find something that you can help me out with...
hey daleth, i tried hard to find out why my values of y are showing like -1.#IND...but i m still confused. can u tell me how we can sort out outputs like that using visual studio like can we find where exactly what calculation is happening..?
Topic archived. No new replies allowed.