dimanche 9 avril 2017

Program crashing after randomization of a float ( C )

The program seems to crash on line 54 when calculating circles[3][6]. What could be causing this? I entered a bunch of print statements to find out some information and everything seems to be going great until exactly [3][6], which doesn't seem to be anything special for it to crash on.

int main() {

double a, b, circleXMax, circleYMax, circleRMax;
double rand_float(double a, double b);
int i, j;
int circleInfo = 3;
int circleNum = 50;
int PI = 3.14;
double circleArea;
double circleAreaMax = 0;
double circles[circleInfo][circleNum];

printf("Opened \n");

for(i = 1; i <= circleNum; i++) {
    printf("Start of i = %d \n", i);
    for(j = 1; j <= circleInfo; j++) {
        printf("Start of j = %d \n", j);
        if(j == 1 || j == 2) {
            printf("Start of randomization of j (%d) \n", j);
            circles[j][i] = rand_float(100.00, 900.00);
            printf("circles[%d][%d] = %f \n", j, i, circles[j][i]);
        }
        else {
            printf("Start of randomization of j (%d) \n", j);
            circles[j][i] = rand_float(0.00, 100.00);
            printf("circles[%d][%d] = %f \n", j, i, circles[j][i]);
        }
    }
    printf("Start of calculation of circleArea \n");
    circleArea = PI * circles[3][i] * circles[3][i];
    printf("Completed calculation of circleArea = %f \n", circleArea);
    if(circleArea >= circleAreaMax) {
        printf("circle was larger then maximum current \n");
        circleAreaMax = circleArea;
        circleXMax = circles[1][i];
        circleYMax = circles[2][i];
        circleRMax = circles[3][i];
    } else {
        printf("circle was NOT larger then maximum current \n");
    }
}

printf("Circle with largest area (%f) has\n", circleAreaMax);
printf("center (%f, %f) and radius %f", circleXMax, circleYMax, circleRMax);

   }

   double rand_float(double a, double b) {
      printf("doing a randomization via rand_float \n");
      return (((double)rand()/RAND_MAX)*(b-a))+a;
   }




Aucun commentaire:

Enregistrer un commentaire