14,937,037 members
1.00/5 (1 vote)
See more:
Hello, there,

I spent above seven hours trying to figure out this problem, but our test system gave me only 10/100 points. Can somebody please help me to find my mistake?

With the given input I got a successful output, although my solution is incorrect.

Requirement:

Ivo's galaxy is represented as a two-dimensional array.

You will receive two integers, separated by a space, which represent the two dimensional array - the first being the rows and the second being the columns.

Every cell in the matrix is a star that has a value. Ivo starts at the given row and col. He can move only on the diagonal from the lowest left to the upper right, and adds to his score all the stars (values) from the cells he passes through.

Unfortunately, there is always an Evil power that tries to prevent his success.

Evil power starts at the given row and col and instantly destroys all stars on the opposite diagonal - from lowest right to the upper left. Ivo adds the values only of the stars that are not destroyed by the evil power.

Then, you must fill the two dimensional array with increasing integers starting from 0, and continuing on every row, like this:

• first row: 0, 1, 2… m

• second row: n+1, n+2, n+3… n + n.

The input ends when you receive the command "Let the Force be with you". When that happens, you must print the value of all stars that Ivo has collected successfully.

Input

• On the first line, you will receive the number N, M -> the dimensions of the matrix. You must then fill the matrix according to these dimensions.

• On the next several lines you will begin receiving 2 integers separated by a single space, which represent Ivo's row and col. On the next line you will receive the Evil Power's coordinates.

• There will always be at least 2 lines of input to represent at least 1 path of Ivo and the Evil force.

• When you receive the command, "Let the Force be with you" the input ends.

Output

• The output is simple. Print the sum of the values from all stars that Ivo has collected.

What I have tried:

C#
```using System;
using System.Linq;

namespace JediGalaxy
{
public class Program
{
static void Main(string[] args)
{
int[] arrayInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
int matrixRows = arrayInfo[0];
int matrixCols = arrayInfo[1];
int[,] matrix = CreateMatrix(matrixRows, matrixCols);
long sumOfStars = 0;
while (line != "Let the Force be with you")
{
long[] ivoCoordinates = line.Split().Select(long.Parse).ToArray();
long ivoRow = ivoCoordinates[0];
long ivoCol = ivoCoordinates[1];
long ivoStartRow = 0;
long ivoStartCol = 0;
ValidateIvoStart(matrixRows, ivoRow, ivoCol, out ivoStartRow, out ivoStartCol);

long evilRow = evilsCoordinates[0];
long evilCol = evilsCoordinates[1];
long evilStartRow = 0;
long evilStartCol = 0;
ValidateEvilStart(matrixRows, matrixCols, evilRow, evilCol, out evilStartRow, out evilStartCol);

if(evilStartRow <= matrixRows - 1 && evilStartCol >= 0 && evilStartCol <= matrixCols - 1)
{
matrix[evilStartRow, evilStartCol] = 0; // if evils starts in the matrix, he destroyes the current star
}

while (evilStartRow > 0 && evilStartCol > 0)
{
EvilDestroysStars(matrix, ref evilStartRow, ref evilStartCol);
}

if (ivoStartRow <= matrixRows - 1 && ivoStartCol >= 0 && ivoStartCol <= matrixCols - 1)
{
sumOfStars += matrix[ivoStartRow, ivoStartCol]; // if Ivo starts is in the matrix, he collects the current star
}

while (ivoStartRow > 0 && ivoStartCol < matrixCols - 1)
{
IvoCollectsStars(matrix, ref sumOfStars, ref ivoStartRow, ref ivoStartCol);
}

}

Console.WriteLine(sumOfStars);
}

private static void IvoCollectsStars(int[,] matrix, ref long sum, ref long ivoStartRow, ref long ivoStartCol)
{
ivoStartRow--;
ivoStartCol++;
sum += matrix[ivoStartRow, ivoStartCol];
}

private static void EvilDestroysStars(int[,] matrix, ref long evilStartRow, ref long evilStartCol)
{
evilStartRow--;
evilStartCol--;
matrix[evilStartRow, evilStartCol] = 0;
}

private static void ValidateEvilStart(int matrixRows, int matrixCols, long evilRow, long evilCol, out long evilStartRow, out long evilStartCol)
{
if (evilRow > matrixRows - 1)
{
evilStartRow = matrixRows;
}

else
{
evilStartRow = evilRow;
}

if (evilCol > matrixCols)
{
evilStartCol = matrixCols;
}

else
{
evilStartCol = evilCol;
}
}

private static void ValidateIvoStart(int matrixRows, long ivoRow, long ivoCol, out long ivoStartRow, out long ivoStartCol)
{
if (ivoRow > matrixRows - 1)
{
ivoStartRow = matrixRows;
}

else
{
ivoStartRow = ivoRow;
}

if (ivoCol < 0)
{
ivoStartCol = -1;
}

else
{
ivoStartCol = ivoCol;
}
}

private static int[,] CreateMatrix(int matrixRows, int matrixCols)
{
int[,] matrix = new int[matrixRows, matrixCols];
int currentNum = 0;
for (int row = 0; row < matrixRows; row++)
{
for (int col = 0; col < matrixCols; col++)
{
matrix[row, col] = currentNum;
currentNum++;
}
}

return matrix;
}
}
}```
Posted
Updated 15-Mar-21 1:36am
v3

## Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
BillWoodruff 14-Mar-21 14:59pm

+5

## Solution 2

I fear this snipset can change the diagonal depending on input.
C#
```private static void ValidateEvilStart(int matrixRows, int matrixCols, long evilRow, long evilCol, out long evilStartRow, out long evilStartCol)
{
if (evilRow > matrixRows - 1)
{
evilStartRow = matrixRows;
}

else
{
evilStartRow = evilRow;
}

if (evilCol > matrixCols)
{
evilStartCol = matrixCols;
}

else
{
evilStartCol = evilCol;
}
}
```

Advice: make samples inputs to see if 2 different inputs gives same output.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Elena Popova 14-Mar-21 4:33am

Patrice T, thanks a lot for your feedback. :)
This is the problem I spend above seven hours debugging and the code is working correctly, but our test system rejects my solution. Why I don't know.
I have already done hundreds exercises at the university I know how to work with debugger. :)
I was debugging the evil path and it seems is working correctly.
All the best!
BillWoodruff 14-Mar-21 14:56pm

+5
Patrice T 14-Mar-21 14:59pm

Thank you