Question How is that answer possibe?

Bose

New member
Joined
Dec 5, 2020
Messages
2
Programming Experience
Beginner
Hi, I'm a complete beginner in c# and trying to solve some challenges and I came across this and I have no idea how that answer was possible, here is the link - C# Sharp exercises: Calculate the sum of all the intgers of a rectangular matrix except those integers which are located below an intger of value 0 - w3resource I'm wondering how that j in for loop became 0 from 1 and there is a decrement in the code. And i also feel that the challenge is too logical for me can you suggest ways to improve my logic? thank you.
 
Look closely at the condition in the inner loop:
C#:
int x = 0;
for (int i = 0; i < my_matrix[0].Length; i++)
    for (int j = 0; j < my_matrix.Length && my_matrix[j][i] > 0; j++)
        x += my_matrix[j][i];
return x;

The outer loop goes through each column. The inner loop goes through each row. The condition for the inner loops checks to make sure that the index j is less than the number of rows, and that the element at j,i is greater than zero.

As an aside there is a bug in that code: It can't handle this input:
C#:
new int[][] {
    new int[]{1, 1, 1, 1},
    new int[]{1, 1, 1},
    new int[]{1, 1},
    new int[]{1},
}
(granted, some people may say that input is not a rectangular matrix).
 
Look closely at the condition in the inner loop:
C#:
int x = 0;
for (int i = 0; i < my_matrix[0].Length; i++)
    for (int j = 0; j < my_matrix.Length && my_matrix[j][i] > 0; j++)
        x += my_matrix[j][i];
return x;

The outer loop goes through each column. The inner loop goes through each row. The condition for the inner loops checks to make sure that the index j is less than the number of rows, and that the element at j,i is greater than zero.

As an aside there is a bug in that code: It can't handle this input:
C#:
new int[][] {
    new int[]{1, 1, 1, 1},
    new int[]{1, 1, 1},
    new int[]{1, 1},
    new int[]{1},
}
(granted, some people may say that input is not a rectangular matrix).
1607266129209.png

hey thanks for the time man iv added ive printed j and i before adding up the value to x, can you explain how that j turned to 0 from 1 even though there is no decrement. thanks
 
There is no decrement. The code broke out of the inner loop and went on to the next value of i because the condition that the element be greater than zero was not true. It's why I said pay close attention to the condition in the inner loop.
 
My recommendation is that instead of just running the code (by pressing F5), is to step through the code with the debugger (press F10). As you step through each line of code look at the Autos and Variables pane to see the values of the variables. @Sheepings has a great tutorial on debugging.

Also as alternative, it's worth recalling that a for loop is just syntactic sugar for a while loop. So give a for loop that looks like this:
C#:
for(int i = 0; i < 5; i++)
{
    Console.WriteLine(i);
}
the logic actually looks like this:
C#:
int i = 0;
while (i < 5)
{
    Console.WriteLine(i);
    i++;
}

so in the case of the code from that solution, the nested for loops look like this logic:
C#:
int x = 0;
int i = 0;

while (i < my_matrix[0].Length)
{
    int j = 0;
    while (j < my_matrix.Length && my_matrix[j][i] > 0)
    {
        x += my_matrix[j][i];
        j++;
    }

    i++;
}
return x;

And then another way of reading lines 7-11 is:
C#:
while (j < my_matrix.Length)
{
    if (my_matrix[j][i] <= 0)
    {
        break;
    }

    x += my_matrix[j][i];
    j++;
}
 

Latest posts

Back
Top Bottom