java - Math done in a 10x10 two dimensional array does not compute correctly -
i set 2 dimensional array, 10 10, , each slot (x*y) respective position. i'm trying add numbers in columns 3, 5, , 7 ctotal , add numbers in rows 2, 4, , 6 rtotal. coding seems sound can't seem make work. ideas?
public static void arraymath() { int ctotal = 0; int rtotal = 0; //int tdiffvalue = (rtotal - ctotal); int twodimarr[][] = new int[10][10]; int row = 10; int col = 10; int x = 0; int y = 0; for(x = 0; x < row; x++) { for(y = 0; y < col; y++) { twodimarr[x][y] = x*y; } } for(x = 0; x < row; x++) { for(y = 0; y < col; y++) { if( (x+y) < col ) { //system.out.print( " " ); } //system.out.print(" " + (twodimarr[x][y])); } //system.out.println(); } for(x = 0; x < twodimarr.length; x++) //problems start down here. { for( y= 0; y<twodimarr.length; y++) { if(y == 2 || y == 4 || y == 6) { rtotal = ((rtotal + twodimarr[x][y])); } } } system.out.println("rtotal " + rtotal + "."); for(x = 0; x < twodimarr.length; x++) { for(y = 0; y < twodimarr.length; y++) { if(x == 3 || x == 5 || x == 7) { ctotal = ((ctotal + twodimarr[x][y])); } } } system.out.print("ctotal " + ctotal + "."); }
x==3 isn't third column, it's 4th(0,1,2,3). meaning it's column of 3*0, 3*1, etc. , 675/540 numbers correct in case.
Comments
Post a Comment