debugging - 2D array referencing whole index, not individual cell in C -
i can't seem figure out problem is. i'm writing 2d random walk simulation , thought use 2d array simulate grid it's on. however, in code, when try reference particular cell in array increment it's value, bin[3][4], increments value of whole index i.e. bin[1][4],bin[2][4],bin[3][4],etc. ideas on how fix or i'm doing wrong? thanks.
#include <stdio.h> #include <math.h> #define total 10. /*total iterations*/ #define walk 5 /*total # of steps in walk*/ #define lambda 1.0 /*step size*/ #define binsize 0.1 /*current chosen values bug checking simplicity*/ main() { float range = walk*lambda*2.; /*2 times max range, positive*/ int n,prob,i,k,j,placex,placey,bins; double valuex,valuey, a; /*value starts @ half range values +*/ bins=(range/binsize); int bin[bins][bins]; for(i=0;i<=bins;i++) /*zero out bin*/ { for(j=0;j<=bins;j++) { bin[i][j]=0; } } for(k=0;k<total;k++) { valuex=range/2.; valuey=range/2.; for(n=1;n<=walk;n++) { prob= rand(4) % 100+1; if(prob<=25) { valuex=valuex+pow(lambda,n); } else if(prob>25 && prob<=50) { valuex=valuex-pow(lambda,n); } else if(prob>50 && prob<=75) { valuey=valuey+pow(lambda,n); } else if(prob>75) { valuey=valuey-pow(lambda,n); } } placex=floor(valuex/binsize+0.5); placey=floor(valuey/binsize+0.5); bin[placex][placey]=++bin[placex][placey]; printf("%d %d %d\n",placex,placey,bin[placex][placey]); /* bug checking. prints bin numbers value should go , value of element.*/ } for(i=0;i<=bins;i++) { for(j=0;j<=bins;j++) { a=bin[i][j]/total; //printf("%lf %lf %lf\n",i*binsize-range/2.,j*binsize-range/2.,a); /*format input igor*/ } } }
figured out. going out of array bounds. loops go bins arrays indexed 0 bins -1. took out = <= in loops worked. end code looked this. in general going out of array bounds has unpredictable behavior.
#include <stdio.h> #include <math.h> #define total 10. /*total iterations*/ #define walk 5 /*total # of steps in walk*/ #define lambda 1.0 /*step size*/ #define binsize 0.1 /*current chosen values bug checking simplicity*/ main() { float range = walk * lambda * 2.; /*2 times max range, positive*/ int n, prob, i, k, j, placex, placey, bins; double valuex, valuey, a; /*value starts @ half range values +*/ bins = (range / binsize); int bin[bins][bins]; /*zero out bin*/ for(i = 0; < bins; i++) { for(j = 0; j < bins; j++) bin[i][j] = 0; } for(k = 0; k < total; k++) { valuex = range / 2. - 1; valuey = range / 2. - 1; for(n = 1; n <= walk; n++) { prob = rand(4) % 100 + 1; if(prob<=25) valuex += pow(lambda, n); else if(prob <= 50) valuex -= pow(lambda, n); else if(prob <= 75) valuey += pow(lambda, n); else valuey -= pow(lambda, n); } placex = floor(valuex / binsize + 0.5); placey = floor(valuey / binsize + 0.5); bin[placex][placey] += 1; printf("%d %d %d\n", placex, placey, bin[placex][placey]); /* bug checking. prints bin numbers value should go , value of element.*/ } for(i = 0; < bins; i++) { printf("\n"); for(j = 0 ;j < bins; j++) { if (bin[i][j] == 0) printf(" "); else printf("%d ", bin[i][j]); = bin[i][j] / total; } } }
the loops added @ bottom output pattern.
Comments
Post a Comment