Hi I Need Help In Two Dimensional Arrays

How do I check the values ​​below and above the main diagonal in a square array?
78 Replies
Kringe
Kringe3mo ago
int[,] arr =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
{ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }
};

Console.WriteLine(arr[0, 4]); //prints out 4
Console.WriteLine(arr[2, 1]); //prints out 21
Console.WriteLine(arr[3, 3]); //prints out 33
int[,] arr =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
{ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }
};

Console.WriteLine(arr[0, 4]); //prints out 4
Console.WriteLine(arr[2, 1]); //prints out 21
Console.WriteLine(arr[3, 3]); //prints out 33
Does this snippet explain it for you?
ahmadhija🌵
ahmadhija🌵3mo ago
I Didnt Mean It Like That if i want for example to check if all the values below the main diagonal are 0 how im i supposd to do this Can i get some help
SpReeD
SpReeD3mo ago
Talking about main-diagnal I think you mean a matrix. First off, you need to determine it and next create a method for checking below and above. You need for loops for this; looping through a 2D Array is done by nesting a for loop into another, first loop will iterate through the row and the inner loop through column, like so
int[,] arr =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
{ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }
};

for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
Debug.Print(arr[x, y].ToString());
}
}
int[,] arr =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
{ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 }
};

for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
Debug.Print(arr[x, y].ToString());
}
}
Kringe
Kringe3mo ago
var size = 10;
var squareArray = new int[size, size];
var count = 1;

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
squareArray[i, j] = count;
count++;
}
}

for (int i = 0; i < size; i++)
{
Console.WriteLine($"Diagonal: {squareArray[i, i]}");
Console.WriteLine($"Above the diagonal: {(i - 1 < 0 ? "out of bounds" : squareArray[i - 1, i])}");
Console.WriteLine($"Under the diagonal: {(i + 1 >= size ? "out of bounds" : squareArray[i + 1, i])}");
Console.WriteLine();
}
var size = 10;
var squareArray = new int[size, size];
var count = 1;

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
squareArray[i, j] = count;
count++;
}
}

for (int i = 0; i < size; i++)
{
Console.WriteLine($"Diagonal: {squareArray[i, i]}");
Console.WriteLine($"Above the diagonal: {(i - 1 < 0 ? "out of bounds" : squareArray[i - 1, i])}");
Console.WriteLine($"Under the diagonal: {(i + 1 >= size ? "out of bounds" : squareArray[i + 1, i])}");
Console.WriteLine();
}
Is this it?
ahmadhija🌵
ahmadhija🌵3mo ago
bro its like school type of somethhin what is var
Pobiega
Pobiega3mo ago
Type inference Don't worry about it, tbh
Kringe
Kringe3mo ago
mb idk why halve is var and halve is int
ahmadhija🌵
ahmadhija🌵3mo ago
the Question is you need to code a method that returns a true of false if: all the values below the main diagonal are 0 and all the values in the main diagonal and above it need 2 be and number other than 0
SpReeD
SpReeD3mo ago
Now, that you know how to iterate through a matrix, the rest is up to you, to do the calculations and iterate through the main diagonal. I won't provide you the solution to your homework ¯\_(ツ)_/¯
Kringe
Kringe3mo ago
this is what you mean by diagonal right
No description
ahmadhija🌵
ahmadhija🌵3mo ago
bro its like a 30 Q homework i solved everything execpt this thing yea when the row and col are equal
Pobiega
Pobiega3mo ago
So what exactly are you stuck on? Accessing the array? Figuring out the main diagonal?
ahmadhija🌵
ahmadhija🌵3mo ago
no my problem is how to get the values below and above the main diagonal how the structure of the for is going to be
Pobiega
Pobiega3mo ago
Well, if the matrix has a cube shape and a side length of... 5 We know the diagonal is 0,0 to 5,5 right?
ahmadhija🌵
ahmadhija🌵3mo ago
yes row = col
Pobiega
Pobiega3mo ago
If you visualize this array, you quite quickly see a pattern for what is below the diagonal and what is above it
ahmadhija🌵
ahmadhija🌵3mo ago
yea
Kringe
Kringe3mo ago
or am I spoiling
Pobiega
Pobiega3mo ago
A little 🙂
Kringe
Kringe3mo ago
he didnt have time to copy 😆
ahmadhija🌵
ahmadhija🌵3mo ago
below the main diagonal the row > col and above the row <col
Pobiega
Pobiega3mo ago
Okay
ahmadhija🌵
ahmadhija🌵3mo ago
@Kringe
No description
ahmadhija🌵
ahmadhija🌵3mo ago
xD
Kringe
Kringe3mo ago
Damn
ahmadhija🌵
ahmadhija🌵3mo ago
i dont want it like that i want to understand bruv
Pobiega
Pobiega3mo ago
So now you know how to differentiate it What's stopping you from understanding?
Kringe
Kringe3mo ago
thats the spirit
ahmadhija🌵
ahmadhija🌵3mo ago
idk what to do tbh
Pobiega
Pobiega3mo ago
I'm unfortunately on a phone atm, otherwise id write a program that prints the matrix and uses colors to show above/on/below. You know exactly what to do thou
Kringe
Kringe3mo ago
Ill do it
ahmadhija🌵
ahmadhija🌵3mo ago
you mean somethin like that
ahmadhija🌵
ahmadhija🌵3mo ago
No description
Pobiega
Pobiega3mo ago
Sure.
Kringe
Kringe3mo ago
do you also have to do something with the second diagonal or only the red one
ahmadhija🌵
ahmadhija🌵3mo ago
i know what i need 2 do but not how to do it only the red one
Pobiega
Pobiega3mo ago
Combine some loops with an if statement? And then you are more or less done...
ahmadhija🌵
ahmadhija🌵3mo ago
the problem is with the loops
Kringe
Kringe3mo ago
Do you know how to use breakpoints?
ahmadhija🌵
ahmadhija🌵3mo ago
like yea but why
Pobiega
Pobiega3mo ago
Why are the loops a problem? Look at the picture you posted
Kringe
Kringe3mo ago
csharp
var size = 5;
var squareArray = new int[size, size];
var count = 1;

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
squareArray[i, j] = count;
count++;
}
}

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Console.Write(squareArray[i, j] + "\t");
}
Console.WriteLine();
}

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j > i)
{
Console.WriteLine("Above");
Console.WriteLine(squareArray[i, j]);
}
else if (j < i)
{
Console.WriteLine("under");
Console.WriteLine(squareArray[i, j]);
}
else
{
Console.WriteLine("Diagonal");
Console.WriteLine(squareArray[i, j]);
}
}
}
csharp
var size = 5;
var squareArray = new int[size, size];
var count = 1;

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
squareArray[i, j] = count;
count++;
}
}

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
Console.Write(squareArray[i, j] + "\t");
}
Console.WriteLine();
}

for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (j > i)
{
Console.WriteLine("Above");
Console.WriteLine(squareArray[i, j]);
}
else if (j < i)
{
Console.WriteLine("under");
Console.WriteLine(squareArray[i, j]);
}
else
{
Console.WriteLine("Diagonal");
Console.WriteLine(squareArray[i, j]);
}
}
}
Set your breakpoint at the inner for loop and just loop through it I think you will understand how it works
ahmadhija🌵
ahmadhija🌵3mo ago
i dont know how to form them
Pobiega
Pobiega3mo ago
Well you have two options
ahmadhija🌵
ahmadhija🌵3mo ago
im already lost my guy
Pobiega
Pobiega3mo ago
Either loop through everything and use an if to decide if you want to continue or not Or write your loops cleverly to only target above or below the diagonal The second is better, but might be harder
SpReeD
SpReeD3mo ago
Try using x and y as variable names, like I did in my example, not i (increment) or j, makes things clearer. Also, if using VS, set a breakpoint with F9, run the code and step through it with F10. Do it, you'll understand that way.
ahmadhija🌵
ahmadhija🌵3mo ago
the second one is the one im supposd to do i will do it just going to eat rn
Kringe
Kringe3mo ago
Is that because we talking about squares in this context?
SpReeD
SpReeD3mo ago
Because we have coordinates in a 2d matrix, like Vector2, representing a single point of data.
Pobiega
Pobiega3mo ago
X/y are the standard coordinate names
Kringe
Kringe3mo ago
y thats what I meant will do thanks
int size = 5;
int[,] squareArray = new int[size, size];
int count = 1;

//populate array
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
squareArray[x, y] = count;
count++;
}
}

//loop through array
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (x > y)
{
Console.WriteLine("Above");
Console.WriteLine(squareArray[y, x]);
}
else if (x < y)
{
Console.WriteLine("Under");
Console.WriteLine(squareArray[y, x]);
}
else
{
Console.WriteLine("Diagonal");
Console.WriteLine(squareArray[y, x]);
}
}
}
int size = 5;
int[,] squareArray = new int[size, size];
int count = 1;

//populate array
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
squareArray[x, y] = count;
count++;
}
}

//loop through array
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
if (x > y)
{
Console.WriteLine("Above");
Console.WriteLine(squareArray[y, x]);
}
else if (x < y)
{
Console.WriteLine("Under");
Console.WriteLine(squareArray[y, x]);
}
else
{
Console.WriteLine("Diagonal");
Console.WriteLine(squareArray[y, x]);
}
}
}
changed var to int and added coordinates for clarity
ahmadhija🌵
ahmadhija🌵3mo ago
i will see it when i comeback ty or i will try to solve it when i comeback and if i need it ill look at it i understand it now soo if i want to check if all the values under are 0 i just loop the array and if(row > col) thats it ?
Kringe
Kringe3mo ago
Yes nice!
ahmadhija🌵
ahmadhija🌵3mo ago
wait ima show you the solution its wrong bro how am i supposd to do it
Kringe
Kringe3mo ago
Could you paste the code
ahmadhija🌵
ahmadhija🌵3mo ago
static bool Q4P141(int[,] mat) { for (int i = 0; i < mat.GetLength(0); i++) for (int j = 0; j < mat.GetLength(1); j++) { if (i > j && mat[i, j] != 0) return false; if (i < j && mat[i, j] == 0) return false; } return true;
} the purpose of the method is to check of all the values below are 0 and above and in the main diagonal are different from 0 aka any other number
Kringe
Kringe3mo ago
you are returning after checking the first value
ahmadhija🌵
ahmadhija🌵3mo ago
all the values need to be 0 soo if one value is not 0 you can return false you can say if(i>j) if(ma[i,j]!=0) return false
Kringe
Kringe3mo ago
wait that was wrong so to clarify you only are checking the vals under the daigonal right
ahmadhija🌵
ahmadhija🌵3mo ago
under and above
Kringe
Kringe3mo ago
and the above condition is
ahmadhija🌵
ahmadhija🌵3mo ago
all the values in the main diagonal and above need to be any number but 0
Kringe
Kringe3mo ago
ow mb but i think you turned it around
ahmadhija🌵
ahmadhija🌵3mo ago
can u check it for me
Kringe
Kringe3mo ago
okay il make it a bit clearer
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
Console.WriteLine($"cord {mat[y, x]}");

if (x > y)
{
Console.WriteLine("Above");
if (mat[x, y] == 0)
return false;
}
else if (x < y)
{
Console.WriteLine("Under");
if (mat[x, y] != 0)
return false;
}
}
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
Console.WriteLine($"cord {mat[y, x]}");

if (x > y)
{
Console.WriteLine("Above");
if (mat[x, y] == 0)
return false;
}
else if (x < y)
{
Console.WriteLine("Under");
if (mat[x, y] != 0)
return false;
}
}
I also learned you should x an y for this
ahmadhija🌵
ahmadhija🌵3mo ago
ill try it
Kringe
Kringe3mo ago
if (x >= y)
{
if (x == y)
Console.WriteLine("Diagonal");
else
Console.WriteLine("Above");

if (mat[x, y] == 0)
return false;
}
if (x >= y)
{
if (x == y)
Console.WriteLine("Diagonal");
else
Console.WriteLine("Above");

if (mat[x, y] == 0)
return false;
}
if you want to include the diagonal
static bool Q4P141(int[,] mat)
{
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
if (x < y && mat[y, x] != 0)
return false;
if (x >= y && mat[y, x] == 0)
return false;
}

return true;
}
static bool Q4P141(int[,] mat)
{
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
if (x < y && mat[y, x] != 0)
return false;
if (x >= y && mat[y, x] == 0)
return false;
}

return true;
}
without the comments @ahmadhija🌵 Im gonna go did you figure it out?
ahmadhija🌵
ahmadhija🌵3mo ago
nah its still wrong
Kringe
Kringe3mo ago
past the code and the array for me plz
ahmadhija🌵
ahmadhija🌵3mo ago
No description
ahmadhija🌵
ahmadhija🌵3mo ago
array code is that
Kringe
Kringe3mo ago
it should return true right
int[,] squareArray =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 1, 1},
{ 0, 0, 1, 1, 1},
{ 0, 0, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

int[,] squareArray2 =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 1, 1},
{ 0, 0, 1, 1, 1},
{ 0, 99, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

int[,] squareArray3 =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 0, 1},
{ 0, 0, 1, 1, 1},
{ 0, 0, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

Console.WriteLine(Q4P141(squareArray)); //true
Console.WriteLine(Q4P141(squareArray2)); //false
Console.WriteLine(Q4P141(squareArray3)); //false

static bool Q4P141(int[,] mat)
{
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
if (x < y && mat[y, x] != 0)
return false;
if (x >= y && mat[y, x] == 0)
return false;
}

return true;
}
int[,] squareArray =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 1, 1},
{ 0, 0, 1, 1, 1},
{ 0, 0, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

int[,] squareArray2 =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 1, 1},
{ 0, 0, 1, 1, 1},
{ 0, 99, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

int[,] squareArray3 =
{
{ 1, 1, 1, 1, 1},
{ 0, 1, 1, 0, 1},
{ 0, 0, 1, 1, 1},
{ 0, 0, 0, 1, 1},
{ 0, 0, 0, 0, 1}
};

Console.WriteLine(Q4P141(squareArray)); //true
Console.WriteLine(Q4P141(squareArray2)); //false
Console.WriteLine(Q4P141(squareArray3)); //false

static bool Q4P141(int[,] mat)
{
for (int y = 0; y < mat.GetLength(0); y++)
for (int x = 0; x < mat.GetLength(1); x++)
{
if (x < y && mat[y, x] != 0)
return false;
if (x >= y && mat[y, x] == 0)
return false;
}

return true;
}
looks good to me
ahmadhija🌵
ahmadhija🌵3mo ago
let me try it sooo im the one who cant check ty soo if any q i can solve it with this pattern Loop if and then what i want to do
Kringe
Kringe3mo ago
depends on the question I guess but everything working now?
ahmadhija🌵
ahmadhija🌵3mo ago
yea
Kringe
Kringe3mo ago
nice finally
ahmadhija🌵
ahmadhija🌵3mo ago
luv u man last thing y is row and x is col soo when y>x you are talking below and x is the opposite