[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
This might sound stupid but I can't get it to work. After
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 12
Thread images: 2
File: 1448714211352.gif (4 MB, 360x200) Image search: [Google]
1448714211352.gif
4 MB, 360x200
This might sound stupid but I can't get it to work. After reading how many rows and how many columns a matrix has, I'm trying to display the minimum element on each row, then the minimum element on each column.

My code looks like this, but sometimes it throws an IndexOutOfBoundsException, sometimes it just doesnt display the minimum for the columns, only for the rows.

The code for the rows, which works, is this

public static void minRand(int[][] n) {

int[] result = new int[n.length];
int min = 0;


for (int i = 0; i < n.length; i++) {
for (int j = 0; j < n[0].length; j++) {
if (n[i][j] < n[i][min]) {
min = j;
}
result[i] = n[i][min];
}
System.out.println("Minimum on line " + i + " is " + result[i]);
}

}


And the code for the columns, which does not work is

public static void minCol(int[][] n) {

int[] result = new int[n[0].length];
int min = 0;

for (int i = 0; i < n.length; ++i) {
min = Integer.MAX_VALUE;
for (int j = 1; j < n[i].length; ++j)
if (n[j][i] < min)
min = n[j][i];
System.out.println("Minimum on column no " + i + " is " + min);
}

}


If i give it 3 lines and 4 columns, the console displays this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Principal.minCol(Principal.java:103)
at Principal.afisareMeniu(Principal.java:196)
at Principal.main(Principal.java:13)

Minimum on line 0 este 0
Minimum on line 1 este 0
Minimum on line 2 este 0


It's 0 because i didnt initialize the matrix, but the minimum on each line works.

Also. If i give it a square matrix, both the minimum for rows and columns work. What am i doing wrong?
>>
>>51595451
>for (int i = 0; i < n.length; ++i)
>for (int j = 1; j < n[i].length; ++j)
>n[j][i]

You can't use j as index for n.
>>
>>51595451
stackoverflow.com/questions/5958186/multidimensional-arrays-lengths-in-java
>>
>>51595508

Should i use a different For?
>>
>>51595508

then why does it work for Square matrices?
>>
>>51595600
You have mixed up the ordering. When the numbers are the same, the ordering doesn't matter.
>>
>>51595623

ok, so then help me, how should i modify it?
>>
bamp
>>
File: 1444423668894.jpg (67 KB, 297x321) Image search: [Google]
1444423668894.jpg
67 KB, 297x321
help me out
>>
>>51596138
http://stackoverflow.com/questions/6630990/java-a-two-dimensional-array-is-stored-in-column-major-or-row-major-order

and

>>51595532
>>
Why does this throw an IndexOutOfBounds Exception in MxN matrices, but works just fine for NxN matrices?

public static void minCol(int[][] n) {

for (int care = 0; care < n.length; care++) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < n[0].length; i++) {
if (n[i][care] < min) {
min = n[i][care];
}
}
System.out.println("Minimum on row " + care + " is " + min);
}
}
>>
>>51597634
because you're pretty much doing
for w in width
for h in height
access at width:h, height:w

You should be doing [care][i], not [i][care].
Thread replies: 12
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.