java - passing 2 dimensional array and return 1 dimensional array -


public class magicsquare {     public static void main(string[] args) {         int[][] mysquare = {{2,7,6},                             {9,5,1},                             {4,3,8}};          system.out.print(columnsareallequaltomagicsum(mysquare));     }                                            //***********************//     public static int getsumofcolumn(int[]  /*[] array, int index*/) {         int sum = 0;                //********//         int[] t = getcolumn(array  /*, index*/);          (int = 0; < t.length; i++) {             sum = sum + t[i];             system.out.print(t[i] + " ");         }         system.out.println();         return sum;     }      public static int[] getcolumn(int[][] array, int index) {         int[] result = new int[array[1].length];         (int = 0; < array[1].length; i++) {             result[i] = array[i][index];         }         return result;     }      public static boolean columnsareallequaltomagicsum(int[][] array) {         int arraylength = array[1].length;         int previousvalue = 0;         (int = 0; < arraylength; i++) {             int value = getsumofcolumn(array, i);              if (i != 0) {                 if (previousvalue != value) {                     return false;                 }             }             previousvalue = value;         }         return true;     } 

this code works fine if uncomment comments. i'm trying here pass in 2d array getcolumn method , returning in 1d array gets passed getsumofcolumn. columnsareallequaltomagicsum check see if columns equal 15 , return true, else false. , how that?

well, see lot of errors in code:

  1. delete comments. not code inside of them, /* */.

  2. if you, won't create variable set length of first column in multidimensional array. put in loop:

    for (int = 0; < array[1].length; i++){}

  3. if pass 2 variables method getsumofcolumn , have 1 parameter in declaration of method, won't work. have declare index. also, if pass there variable has multidimensional array can't declare simple array.

  4. if try set previousvalue in if statement, have include previousvalue = value in if-else statement because, if not, set previousvalue value.

  5. you never compare if number it's 15.

in opinion, code have this:

public class magicsquare {      public static void main(string[] args) {         int[][] mysquare = {{2,7,6},                             {9,5,1},                             {4,3,8}};          system.out.print(columnsareallequaltomagicsum(mysquare));     }        public static int getsumofcolumn(int[][] array, int index) {          int sum = 0;                         int[] t = getcolumn(array, index);          (int = 0; < t.length; i++) {             sum = sum + t[i];             system.out.print(t[i] + " ");         }         system.out.println();         return sum;     }      public static int[] getcolumn(int[][] array, int index) {         int[] result = new int[array[1].length];          (int = 0; < array[0].length; i++) {              result[i] = array[i][index];         }         return result;     }     public static boolean columnsareallequaltomagicsum(int[][] array) {          int previousvalue = 15;         boolean correct = true;          (int = 0; < array[1].length && correct == true; i++) {             int value = getsumofcolumn(array, i);              if (i != 0) {                 if (previousvalue != value) {                     correct = false;                 }             }         }          if(correct == true){            return true;         }         else{            return false;         }     } 

if didn't missunderstood question, think want sum columns each row in multidimensional-array , compare them if number of result equals 15.

i think easier in double loop, 1 inside another, through rows , columns , compare them there think code put above have works.

with double loop mean:

for(int = 0; < array[1].length; i++) {   for(int j = 0; j < array[0].length; j++)   {     //your code make sum   } //your code compare if sum equals 15 } 

i expect helpful you!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -