public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int minSum = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = input.nextInt();
if (array[i][j] < minSum) {
minSum = array[i][j];
}
}
System.out.println(minSum);
}
}
}
Why is my code not meeting the last condition (displaying the minimum sum of the elements in any row or colum)
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
4 February, 19:35
You say the code is supposed to display the minimum sum of any row or column but I don't see that this code is summing any row or column. It looks like it is attempting to get the minimum entered number in the table:
For example:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
The minimum entered number is 1, however:
The top row sums to 6
The middle row sums to 15
The bottom row sums to 24
The left column sums to 12
The middle column sums to 15
The right column sums to 21
That means the top row is the minimum sum and the correct answer is 6
0
Mirna C.
4 February, 20:20
Thank you. I misunderstood the task instructions. Back to square one, we go. LOL!
+1
Guadalupe Gagnon
4 February, 22:08
I have been with codegym since 2018 and I have heard from MANY students that the tasks need better examples to go along with the conditions. They surveyed me 5 or 6 years ago and I told them the exact thing. It would help SO much with simply understanding what is being asked.
0
Mirna C.
5 February, 14:46
Thank you and I agree with you, Lupe, but for this specific task, I believe it to be my fault because it clearly states to display the MINIMUM SUM of the elements in any row or column and NOT the minimum element. So far, working with arrays has been the biggest challenge for me.
Can you clarify the differences between the indices of an array vs the element values and also the references of arrays? Sorry I am not sure how toask for this bur So far, I get that the elements are the content of each cell or what each cell holds, and the index is the address of that cell??? I am confused when it states that a multidimensional arrays holds a reference to references to references.... see attached...thank you in advance. ![]()

+1
Guadalupe Gagnon
5 February, 15:58useful
Lets see if I can help clear things up:
- First a variable is jsut a way to store a piece of data; a String variable would store a String, a double would store a double etc. When we first start learning programming they describe variables as a "bin"
- building off of the "bin" concept and array would just be multiple bins that store the same type of data. So an array declared like this:
would equal 3 connected bins that can store Strings
- Multidimensional arrays, despite sounding science fiction, are actually really simple when you remember how arrays are just connected bins that store a specific type of data. So in the above example the String array had 3 bins that could store each store a String. A multidimensional array is simply just an array that stores other arrays, or each of it's individual "bin" stores additional bins. So an array declared like this:
is just simply an Array with 3 bins that each can store a String[]
- Adding more depth to a multidimensional array isn't hard to process either:
Just simply means an Array of 3 bins that each hold a String[][]. Each String[][] is simply just an array whose bins each contain a String[]
*** continued into next post - I'll explain "reference" *** +1
Guadalupe Gagnon
5 February, 16:26useful
In Java data is stored somewhere in memory. It is important to remember that any Object (not primitives) that isn't immutable (like Strings) gets passed around by reference. I like to explain "reference" like this:
Imagine a kite. To fly a kite you need to attach a string to it which you would hold. In this example the attached string is the way you interact with the kite. In programming the way that you interact with an object is with a reference to that object, typically a variable.
Now back to the kite. If you tied a second string to that kite and someone else held onto it then both people could interact with the same kite. If one person pulled on the string to make the kite go right, the other person would also realize the kite going right. In programming if two variables reference the same object and a change is made through one of the variables then it would also show in the second variable.
An example in programming:
Check the output of this code. In the second example both arrayA and arrayB both hold refences to the same object in memory. Making a change to either will be realized by both. There is no limits to the amount of references an object can have.
It is important to know that immutable obects can only have one reference though. When you try to create another reference a new object is created and stored in the new reference. Therefore immutable objects behave like the first example where the change to 'a' didn't change 'b'. +1
Guadalupe Gagnon
5 February, 16:33useful
***Sorry about the long posts, this will be the final.. and i'll keep it short****
Now the language that you are specifically asking about: "stores a reference to a reference....". Variables don't ever store objects, they only store references to memory locations of that object. So it would be correct when talking about an array of objects to state that the array is holding references. And multidimensional arrays hold references of references.
It is important to understand the concepts of references in programming, however this is fairly basic and in the real world people will know what you are talking about if you say "I have an array or arrays" without having to explain that "My array contains references to other arrays".
The harder thing is remembering that immutable objects behave like primitives when it comes to references.
+1
Mirna C.
5 February, 17:25
Thank you so much for this information. I truly appreciate your time and effort. I will process this information (code) line by line. I especially love the kite example you used regarding references. You explained complex topics so well. Thank you again.
+1