Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate the minimum value for each column in 2D array  RSS feed

 
nvayin agha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a 2D array , iam trying to calculate the minimum value for each column and put the result in the result array.

the code bellow is calculating the minimum value for each column , but if i change the array to int[][] array = {{6,3,9},{0,8,2} } or int[][] array = {{6,3,9}, {0,8,2}, {3,7,5}, {4,7,0}};
i am receiving error

 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please cut-and-paste the actual error message into a post so that we can see it.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically... you were trying to do object operations on primitives. Integer is the Class you need to use if you want to do object manipulation on integer values.

Please pay attention to what I am about to say:

Thank you for using code tags, please clean up your style to be consistent in the way you treat white space, the API documentation is your friend, ALWAYS post the error codes that you get along with your code.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a static method that you want to call, use the class name and not an actual class object.
Instead of

it should be
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your indices are inverted.

you are iterating over all rows i, but n[0][i] is the ith COLUMN of row 0.
 
nvayin agha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is same , if i change the array size i am receiving error
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Basically... you were trying to do object operations on primitives. Integer is the Class you need to use if you want to do object manipulation on integer values.

Les, why did you change Nvayin's int's to Integers? It worked the way he had it.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Your indices are inverted.

you are iterating over all rows i, but n[0][i] is the ith COLUMN of row 0.


Ditto for


 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically the first error that popped up in my IDE was "cannot convert int[][] to Object[][]", so the int to Integer conversion started.
Carey Brown wrote:
Les, why did you change Nvayin's int's to Integers? It worked the way he had it.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Basically the first error that popped up in my IDE was "cannot convert int[][] to Object[][]", so the int to Integer conversion started.
Carey Brown wrote:
Les, why did you change Nvayin's int's to Integers? It worked the way he had it.

I popped the original code into Eclipse, and except for a warning on line 15, it worked, albeit, only for a square array because he was using the wrong n.length's.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Les, why did you change Nvayin's int's to Integers? It worked the way he had it.

That was my first thought too, but actually there is quite a good reason for using Integers - although it probably doesn't apply in this case.

If the array is "jagged" (ie, not rectangular), then a "column" might be missing some elements; and the only foolproof way to signal that is with a null, as any other value you choose could be misinterpreted depending on the operation you do on it.

So, at the very least, a column - and personally, I'd be inclined to write a utility method that returns a "column" for any kind of 2-D array as an array of values in row order - should be made up of Integers.

Winston
 
nvayin agha
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

iam trying to call the min method but iam recieving error :

double [][] type can not convert to object ??


double arr1[][]= {{0.1,0.2},{0.5,0.07}};

String R2;
R2 = Arrays.deepToString(mincol(arr1));
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post all the code and the exact error message?

Another thing: you should get used to writing
  double[][] arr1;
instead of
  double arr1[][];
 
Jimm Jones
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new to Java and coderanch.
Freshly joined because I think learning along with others will help me improve my skills faster.

Nvayin, the method signature
  deepToString(Object[])
requires an
  Object[]
as the argument. arr1 is an
  Object[][] (double[][])
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid that is incorrect. An array is an object but a double[] is not an array of Objects. So a double[][] counts as an array of objects (=Object[]) where the runtime type of each element is double[].
I think you will find the deepToString() method can test whether the elements are arrays or not and handle them differently from non‑array elements.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By column, do you mean row? You usually print arrays horizontally, so each element appears as a row. Have you tried negative numbers or a 0‑length array as one of your elements? I think you should do both.
Have you tried a Stream? You can get a Stream<int[]> with a method of the Arrays class. The peek method allows you to do something with each element as it passes through the Stream. Here I am using a λ; that allows you to pass each element a to an instruction, here to print it using Arrays#toString.
You probably have to use the forEach method to do something with the individual rows; I would turn them into Streams again, and print their minimum. Wrap the whole thing in a print call and Arrays#stream() (or you can use this instead), and you get an IntStream, and print the result of calling its min() method.Note different indentation: the dots align vertically. Once you find out what min returns, you can do lots of other things with that returned value.

[Edit} I challenge you to work out how to obtain the same functionality without the use of the peek call.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!