Program doesn't work for larger values

Hi everybody,

I am trying to write my first program. Unfortunately, it won't work for large input values. Realistic values for my application would be n = 250, p = 10 and t = 15.

I'd like to know, if my program is not properly working or if it might also be a hardware problem. I am working with Windows XP Professional x64, in case that is important.

I tried to explain what I did in the program. If somebody might care to run it, it might be easier to run it completely (including uncommented parts).

Briefly, the idea is as follows
for different w from 1 to t
- I create a nxn array
- set all elements to zero
- set one random element = 1 (coordinates are two random numbers)
- set all sorrounding elements in an area p = 1 (square with center p and edge length 2p+1)

- then I check if the last array created, that is t, has mutual 1 entries with array t-1, t-2 and so on. I do this by multiplying the array entries: a mutual 1 is found when the product is 1).
- Now I multiply dann an exponential term, whose value depends on the number of the array that I compare to array t.
-in the end I sum up ALL entries (all entries from all arrays)


This might be a little difficult to understand, but maybe someone wouldn't mind to take a look at my code. I hope I didn't explain it to badly, but I am really tired as I am working on that problem for quite some time - I even posted my code in german first. :-)


Thanks, Laura


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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <gsl/gsl_rng.h>
#include <windows.h>
#include <time.h>

unsigned const int n;
const int p;
unsigned const int t;
unsigned const int A;
//unsigned const int t0;
//unsigned const int y;



int main(void)
{

/********************************************************************************************************/
input:
n : size of array (n x n)
p : size of area with a one, square with center p and edge length 2p+1
t : number of arrays */
/********************************************************************************************************/

printf("n = : ");
scanf("%d", &n);
printf("p = : ");
scanf("%d", &p);
printf("t = : ");
scanf("%d", &t);
 
int w;    
int m;
int q;
int Surface[t][n][n];

/********************************************************************************************************/
/* random Numbers */
/********************************************************************************************************/

//srand(573);       
srand(time(0));     

for (w=0; w<t; w++){ 

int hitX = rand() % n + 0;
int hitY = rand() % n + 0;


/***************************************************************************************/
/*********** Sets all array elements zero **********/
/***************************************************************************************/

for (m=0; m<n; m++)
{
for (q=0; q<n; q++)
{
Surface[w][m][q]=0;
}
}


/***************************************************************************************/
/* Sets one array element Surface[hitX][hitY] = 1 */
/***************************************************************************************/
//
Surface[w][hitX][hitY]=1;


/***************************************************************************************/
/* Print Array (test) */
/***************************************************************************************/

//for (q=0; q<n; q++)
//{  
//   for (m=0; m<n; m++)
//   {
//   printf("%-3d\t", Surface[w][m][q]);
//   }
//printf("\n");
//} 
//printf("\n");

/***************************************************************************************/
/* Fill area with 1s */
/***************************************************************************************/

int left = max(hitX - p, 0);
int right = min(hitX + p, n-1);
int top = max(hitY - p, 0);
int bottom = min(hitY + p, n-1);
int y;
int x;

for (y = top; y <= bottom; y++)
    {
    for (x = left; x <= right; x++)
    {
    Surface [w][x][y] = 1;
    }
}


/***************************************************************************************/
/*  Print array again (test) */
/***************************************************************************************/

//for (q=0; q<n; q++)
//{
//    for (m=0; m<n; m++)
//    {
//    printf("%-3d\t", Surface [w][m][q]);
//    }
//    printf("\n");
//    }  
//printf("\n");

} 

/*******************************************************************************
/********************************************************************************************************/
  
//printf("\n");
//printf("Einzelne Ueberlappe");
//printf("\n");

/**************************************************************************************
/***************************************************************************************/

int i;
double Surface2 [t][n][n];
double Summe = 0;
int t0 =2;

for(i=0;i<t-1;i++){

for (q=0; q<n; q++)
{
    for (m=0; m<n; m++)
    {
    
    Surface2 [t-1][m][q]= (Surface [t-1][m][q] * Surface [t-(2+i)][m][q] * exp(-((i+1.0)/t0)));
    }
}
//printf("\n");



/***************************************************************************************/
/*  Print Arrays (test)*/
/***************************************************************************************/

for (q=0; q<n; q++)
{
    for (m=0; m<n; m++)
    {
    printf("%-.3f\t", Surface2 [t-1][m][q]);
    }
    printf("\n");
    }  
printf("\n");


  for (q=0; q<n; q++)
      {
          for (m=0; m<n; m++)
          {
          Summe = Summe + Surface2 [t-(1)][m][q];
          }
      } 
 
/***************************************************************************************/
/*  Sum over all array elements of single array*/
/***************************************************************************************/ 
 
//printf("\n");
//printf("%-.3f\t", Summe);
//printf("\n");

/***************************************************************************************/
/*  Sum over all array elements of all arrays*/
/***************************************************************************************/
}

printf("\n");
printf("%-.3f\t", Summe);
printf("\n");

getch();
  
}  
Last edited on
what compiler do you use?

the lines 9 to 12 are not valid C/C++. The constants are supposed to be initialized.
line 29/31/33 are not valid either. You shouldn't be able to modify constants.

you put the two 3-dimensional arrays on stack, but it is limited and if your arrays are too large your program will crash
Hi,

I am using the dev-c++ compiler.

Can I just put "int XXX" instead of "const int XXX" or will I have to look further into variable / constant declaration?

But I guess my most important problem is what you said about the array size. I have already expected somethng like that. Can you give me a hint on how to solve the problem withoutusing the huge 3d array? I think I need the nxn array. But if there is a way to enumerate the arrays without an additional array dimension that would work, I guess.

What is the size limit for an array?? Does it depend on my machine?

Thanks, Laura
so better use an actual compiler like GCC (with IDE Code::Blocks) or Visual C++

yes remove the const

create the array on the heap using new[]. the heap is unlimited (nearly):

1
2
3
int *Surface = new[t * n * n]; // Note: this is an 1-dimensional array
...
delete[] Surface;


you will get into trouble with a 3-dimensional array. Instead use an 1-dimensional array and calculate the index, like so:

z*width*height + y*width + x

read this:
http://www.cplusplus.com/articles/G8hv0pDG/
Hi Laura,

Is there a reason why you are writing C rather than C++ ?

It's just that there are lots of advantages with C++, but maybe you assignment requires C?

If using C, then you will have to use malloc & free rather than C++'s new & delete Remember to check that memory allocation worked - malloc returns NULL if it fails. One should also check the return value of other functions like scanf and fprintf as well.

Another tip is to get the code working with a small array size first (but still on the heap), then work on increasing the size after that.

Have a look a the reference material at the top left of this page for details & examples.

Hope all goes well 8+)
Topic archived. No new replies allowed.