0

I'm working on a numerical simulation program, for simplicity I recreated the code into expanding circles on a domain bounded on each side. I want to track each circle's radius. If I have this code:

int const N = 10;
int D[N+2][N+2]; //domain bounded on each side
int const nCircle = 4;
int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};

void eval(); //function to expand circles

int main() {
    for (int n=0;n<5;n++) {
        eval();
        for (int y=1;y<=N;y++) {
            for (int x=1;x<=N;x++) {
                printf("%d ", D[x][y]);
            }
            printf("\n");
        }
        printf("\n");
    }
}

for visualization and simplicity purpose, add these into global definition

double radius[nCircle] = {2, 2, 2, 2}; //actually unknown, i want to track this

void eval() {
    double a;
    for (int z=0;z<nCircle;z++) {
        for (int y=1;y<=N;y++) {
            for (int x=1;x<=N;x++) {
                a = pow(x-center[z][0], 2) + pow(y-center[z][1], 2);
                if (a <= pow(radius[z], 2))
                    D[x][y] = 1;
            }
        }
        radius[z] += ((double) rand() / (RAND_MAX));
    }
}

How do I do that?

edit: note that circles might overlap each other, array D only store the union of circle area without information of the intersections.

1 Answers1

1

Cannot declare a global array with a variable size (VLA). Use a compile time constant.

// int const N = 10;
#define N 10
int D[N+2][N+2];
// int const nCircle = 4;
#define nCircle  4
int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};

double radius[nCircle] = {2, 2, 2, 2};

Alternatively use C99, and declare D[] and center[] inside main(). Code could use another method to use data in eval() like eval(N, D, nCircle, center, radius)

int main() {
  int const N = 10;
  int D[N+2][N+2] = {0};  // Initialize D
  int const nCircle = 4;
  int center[nCircle][2] = {{1, 1}, {N, N}, {N, 1}, {1, N}};
  double radius[nCircle] = {2, 2, 2, 2};
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • thanks for reminding me the standards! the code above isn't my actual program, it's just an analogy of what seems to happen. btw what's the difference of using int const and #define in above code? – Romadhona Fajar Nov 21 '15 at 20:25
  • @Romadhona Fajar There are many posts on `const` vs `define` such as http://stackoverflow.com/questions/1674032/static-const-vs-define-in-c, http://stackoverflow.com/questions/732321/problem-with-const-vs-define-in-visual-studio – chux - Reinstate Monica Nov 21 '15 at 22:33