how to get absolute value of a 2D array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to get absolute value of a 2D array

I'm trying to get the largest value after getting the sums of the columns in a two-dimensional array? this is my method so far help me. public static int AbsvalueFromAcolumn (int[][] array) { int yarray[] = new int[array.length]; for (int x [] : array ) { int count = 0; for (int column : x) { count += Math.abs(array[column][x]); } yarray[x] = count; } int absValue = yarray[0]; for (int b : yarray ) { if (yarray[b] > absValue ) { absValue = yarray[b];} } return absValue; Tried enhance loops and normal loops. expecting the largest absolute value from the individual sums of the columns or rows. a corrected example would be great. thanks in advance

22nd Apr 2019, 11:08 PM
Lees
2 Answers
+ 4
I think you mixed up how for loop works with that of for-each loop (enhanced for loop). Try this and tell me how it goes. P.S. For future posts, please attach a saved code link instead. public class Program { public static int AbsValueFromAColumn (int[][] array) { int yarray[] = new int[array.length]; int index = 0; // for yarray for (int row [] : array) { int sum = 0; for (int column : row) { sum += Math.abs(column);//Math.abs(array[column][row]); } yarray[index++] = sum;//yarray[row] = count; } int absValue = yarray[0]; for (int b : yarray ) { if(b > absValue)//if(yarray[b] > absValue) { absValue = b;//absValue = yarray[b]; } } return absValue; } public static void main(String[] args) { int[][] sample = { {1,-2,3,4,5}, {6,-7,8,9,10}, {10,100,20,40,-5}, {8,-45,74,77,-6}, {19,-20,15,30,42} }; System.out.println(AbsValueFromAColumn(sample)); } }
23rd Apr 2019, 7:20 AM
Ipang
+ 1
Can you save it as a code in Sololearn and share it here? That way, someone can help you...
23rd Apr 2019, 6:49 AM
Kartik
Kartik - avatar