My program can compile but doesn't run at all, need help

I am a green hand on C++ and I write a program for calculation. It can compile but doesn't run at all, any suggestions or ideas to solve will be appreciated!




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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma once
#include "math.h"
#include "stdio.h"
#include "stdlib.h"
#include "iostream"

using namespace std;

double randgauss(double min, double max, double sigma, double centre)
{
	double random = (min + (max-min) * (double)rand()/RAND_MAX); //create random domain between [min,max]
	double tmp = (random-centre)/sigma; 
	double gauss= exp(-tmp*tmp/2); //gaussian formula
	return (gauss);
}

int main()
{
	const int N=49;
	const int M=100000; 
	const double dt=0.01;
	double arrayP[N][M];
	double arrayQ[N][M];
	double arrayR[N][M];
	double arrayS[N][M];
	double arrayD[N][M];
	double arrayT[N][M];
	double arrayX[N-1][M];
	double arrayf[N][M];
	double arrayY[1][N];
	double arrayYI[1][N];
	double arrayTI[N];

	int count;
	int count1;
	int i;
	int j;
	

	for ( count = 1; count <= N-1; count++)
		{
			for(count1=1;count1 <= M; count1++)
			{	
				arrayX[count][count1]=0.01;//randgauss(-2,2,0.3,0);
			}
		}


	for (count=1;count<=N;count++)
	{
		arrayQ[count][1]=0.5*count;
	}
		

	for (count=1;count<=M;count++)
	{
		arrayP[N][count]=0.01;
	}

	for (count=1;count<=M-1;count++)
	{
		arrayQ[N][count+1]=arrayP[N][count]*(count+1)*dt+0.5*N; 
		for (count1=1;count1<=N-1;count1++)
		{
			arrayR[count1][count]=arrayP[count1][count]*exp(-0.4*0.5*dt);
			arrayS[count1][count]=arrayQ[count1][count]+0.5*dt*arrayR[count1][count];
		}
		arrayS[N][count]=arrayQ[N][count]+0.5*dt*arrayP[N][count];


		if (arrayS[1][count]<=0.66666667)
		{
			arrayf[1][count]=8*arrayS[1][count]-4;
		}
		if (arrayS[1][count]>0.66666667)
        {
			arrayf[1][count]=2*arrayS[1][count]-2;
		}
		if (arrayS[2][count]-arrayS[1][count]<=0.66666667)
        {
			arrayf[2][count]=8*(arrayS[2][count]-arrayS[1][count])-4;
		}
		if (arrayS[2][count]-arrayS[1][count]>0.66666667)
        {
			arrayf[2][count]=2*(arrayS[2][count]-arrayS[1][count])-2;
		}  
   
		arrayT[1][count]=arrayR[1][count]+dt*(arrayf[2][count]-arrayf[1][count])+sqrt(dt)*0.8944*arrayX[1][count];
		arrayP[1][count+1]=arrayT[1][count]*exp(-0.4*0.5*dt);
		arrayQ[1][count+1]=arrayQ[1][count]+0.5*dt*arrayT[1][count];


		for(count1=2;count1<=N-1;count1++)
		{
			if(arrayS[count1+1][count]-arrayS[count1][count]<=0.66666667)
			{
				arrayf[count1+1][count]=8*(arrayS[count1+1][count]-arrayS[count1][count])-4;
			}

			if(arrayS[count1+1][count]-arrayS[count1][count]>0.66666667)
			{
				arrayf[count1+1][count]=2*(arrayS[count1+1][count]-arrayS[count1][count])-2;
			}

			arrayT[count1][count]=arrayR[count1][count]+dt*(arrayf[count1+1][count]-arrayf[count1][count])+sqrt(dt)*0.8944*arrayX[count1][count];
			arrayP[count1][count+1]=arrayT[count1][count]*exp(-0.4*0.5*dt);
			arrayQ[count1][count+1]=arrayQ[count1][count]+0.5*dt*arrayT[count1][count];
		  
		}

	}

	
	for (count=1;count<=M-1;count++)
	{
		arrayD[1][count]=arrayQ[1][count];
		for(count1=2;count1<=N;count1++)
		{
			arrayD[count1][count]=arrayQ[count1][count]-arrayQ[count1-1][count];
		}
	}

	
	for(i=0; i<=3; i++)
	{
		for(j=0;j<=3; j++)
		{
			cout<<arrayD[i][j];
		}
		cout<<endl;
	}
	return 0;  
		
}
Last edited on
It is impossible that it can not run if it has no a compilation or linker error.
Last edited on
Your code has a segFault, caused by a stack overflow, because you're trying to use more of the stack memory than the operating system gives you (it all goes wrong when your code tries to use arrayX, I expect). This is usually caused by making huge arrays on the stack. Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
     const int N=49;
	const int M=100000; 
	const double dt=0.01;
	double arrayP[N][M];
	double arrayQ[N][M];
	double arrayR[N][M];
	double arrayS[N][M];
	double arrayD[N][M];
	double arrayT[N][M];
	double arrayX[N-1][M];
	double arrayf[N][M];
	double arrayY[1][N];
	double arrayYI[1][N];
	double arrayTI[N];


In C++, we use use a container such as vector to do this sort of thing, instead of huge arrays on the stack.

Also, this being C++, tend towards

1
2
3
#include <cmath>
#include <cstdio>
#include <cstdlib> 

and indeed,
#include <iostream>
instead of
1
2
3
#include "math.h"
#include "stdio.h"
#include "stdlib.h" 
Last edited on
So arrays are written on the stack. Then for a matrix, should I use vector to contain the data? How can I write huge arrays and access them?
If you create them using new, they are made on the heap.
Topic archived. No new replies allowed.