java - how do you find the max row in a 2d array -


create 2-d array m :

  1. ask user input row , column sizes keyboard (use scanner). n 9, if user input column size bigger n+4 ask user reinput column size
  2. fill arrays elements double numbers in range of (3.0 , 13.0) using of random object
  3. pass above array m , call following 2 methods

    • in findmaxrow(double[][]array), find , print largest sum of columns in 2d array
    • in returnavg(m), print out avg of array m

comment:

so, made the code find max colm can't figure out how find max row. need able find max row figuring out how since code finds cllm , not max row.

here code:import java.util.scanner;

  public static void main(string[] args) {     scanner input = new scanner(system.in);     random rand = new random();      system.out.print("number of rows? ");     int rows = input.nextint();     system.out.print("number of columns? ");     int columns = input.nextint();     while (columns > 7) { // check column > n+5 n = 2 in case         system.out.print("the column amount high. try number: ");         columns = input.nextint();     }      double[][] m = new double[rows][columns];      (int = 0; < rows; i++) {         (int j = 0; j < columns; j++) {             m[i][j] = rand.nextdouble()*7+4;         }     }     findmaxcol(m);     system.out.println("the average value of array "+returnavg(m)); } public static void findmaxcol(double[][] a) {     double[] maxcol = new double[a[0].length];     double max = 0;     int maxcolnum=0;     (int = 0; < a[0].length; i++) { // sum of columns         (int j = 0; j < a.length; j++) {             maxcol[i]+=a[j][i];         }     }     //system.out.println(arrays.tostring(maxcol));     (int = 0; < maxcol.length; i++) {         if (max < maxcol[i]) {             max = maxcol[i];             maxcolnum = i; // column number associate array column starting 0         }     }     system.out.println("the highest column sum column #"+maxcolnum+" sum of "+max); } public static double returnavg(double[][] a) {     double sum = 0; // initialization     (int = 0; < a.length; i++) {         (int j = 0; j < a[i].length; j++) {             sum+=a[i][j];         }     }     // system.out.println(sum); test line     return (sum/(a.length*a[0].length)); // avg } 

}

not sure understand desired result, should give row greatest sum, sum was. note doesn't take account multiple rows equal sums. report first row in array greatest sum.

public static void findmaxrow(double[][] a){      double maxsum = 0;     int maxrow = 0;      (int row = 0; row < a.length; row++){          double rowsum = 0;          (int column = 0; column < a[row].length; column++){             rowsum += a[row][column];         }          if (rowsum > maxsum){             maxsum = rowsum;             maxrow = row;         }     }      // maxsum should greatest sum of row     // maxrow row contained greatest sum } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -