for loop never ends in c -


i'm trying write c program prints grid each year. have loop iterates prints grid each year how many years choose in argument. loop prints grid endless number of years exceeding boundary put on reason. here's code:

    int main(int argc, char *argv[]) { if (argc != 3) /* argc should 2 correct execution */ {     /* print argv[0] assuming program name */     printf("usage: %s filename", argv[0]); } else {     int year = atoi(argv[1]);     double grida[11][11];     double gridb[11][11];     int in;     int n;     printf("%d\n",year);     file *file = fopen(argv[2], "r");     (int = 0; < 12; i++) {         fscanf(file, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",                 &grida[i][0], &grida[i][1], &grida[i][2], &grida[i][3],                 &grida[i][4], &grida[i][5], &grida[i][6], &grida[i][7],                 &grida[i][8], &grida[i][9], &grida[i][10], &grida[i][11]);     }     fclose(file);     for(n = 0; n < year; n++) {         printf("year %d: \n", n);         if (n == 0) {             (int = 0; < 12; i++) {                 (int j = 0; j < 12; j++) {                     if (j == 11) {                         printf("%.1lf\n", grida[i][j]);                     } else {                         printf("%.1lf ", grida[i][j]);                     }                 }             }         } else if (n % 2) {             in = nextdependency(grida, gridb);             (int = 0; < 12; i++) {                 (int j = 0; j < 12; j++) {                     if (j == 11) {                         printf("%.1lf\n", gridb[i][j]);                     } else {                         printf("%.1lf ", gridb[i][j]);                     }                 }             }         } else {             in = nextdependency(gridb, grida);             (int = 0; < 12; i++) {                 (int j = 0; j < 12; j++) {                     if (j == 11) {                         printf("%.1lf\n", grida[i][j]);                     } else {                         printf("%.1lf ", grida[i][j]);                     }                 }             }         }     } } exit(0); } 

the loop never ends one:

for(n = 0; n < year; n++) { printf("year %d: \n", n); ... 

through attempting debug found loop finite when before code:

file *file = fopen(argv[2], "r");     (int = 0; < 12; i++) {         fscanf(file, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",                 &grida[i][0], &grida[i][1], &grida[i][2], &grida[i][3],                 &grida[i][4], &grida[i][5], &grida[i][6], &grida[i][7],                 &grida[i][8], &grida[i][9], &grida[i][10], &grida[i][11]);     } 

but when put after code loops infinitely, bug cannot figure out why happening? have idea how fix this?

you've defined grid dimensions 11 11, you're reading 12 elements it. last element overwrites loop variable.

in general, if define array of size n, can access elements 0 through n-1.

in case, solution define grids space 12 elements.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -