C
C#8mo ago
AhmadHija

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
Kringe8mo 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
AhmadHijaOP8mo 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
SpReeD8mo 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
Kringe8mo 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
AhmadHijaOP8mo ago
bro its like school type of somethhin what is var
Pobiega
Pobiega8mo ago
Type inference Don't worry about it, tbh
Kringe
Kringe8mo ago
mb idk why halve is var and halve is int
AhmadHija
AhmadHijaOP8mo 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
SpReeD8mo 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
Kringe8mo ago
this is what you mean by diagonal right
No description
AhmadHija
AhmadHijaOP8mo ago
bro its like a 30 Q homework i solved everything execpt this thing yea when the row and col are equal
Pobiega
Pobiega8mo ago
So what exactly are you stuck on? Accessing the array? Figuring out the main diagonal?
AhmadHija
AhmadHijaOP8mo 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
Pobiega8mo 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
AhmadHijaOP8mo ago
yes row = col
Pobiega
Pobiega8mo ago
If you visualize this array, you quite quickly see a pattern for what is below the diagonal and what is above it
AhmadHija
AhmadHijaOP8mo ago
yea
Kringe
Kringe8mo ago
or am I spoiling
Pobiega
Pobiega8mo ago
A little 🙂
Kringe
Kringe8mo ago
he didnt have time to copy 😆
AhmadHija
AhmadHijaOP8mo ago
below the main diagonal the row > col and above the row <col
Pobiega
Pobiega8mo ago
Okay
AhmadHija
AhmadHijaOP8mo ago
@Kringe
No description
AhmadHija
AhmadHijaOP8mo ago
xD
Kringe
Kringe8mo ago
Damn
AhmadHija
AhmadHijaOP8mo ago
i dont want it like that i want to understand bruv
Pobiega
Pobiega8mo ago
So now you know how to differentiate it What's stopping you from understanding?
Kringe
Kringe8mo ago
thats the spirit
AhmadHija
AhmadHijaOP8mo ago
idk what to do tbh
Pobiega
Pobiega8mo 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
Kringe8mo ago
Ill do it
AhmadHija
AhmadHijaOP8mo ago
you mean somethin like that
AhmadHija
AhmadHijaOP8mo ago
No description
Pobiega
Pobiega8mo ago
Sure.
Kringe
Kringe8mo ago
do you also have to do something with the second diagonal or only the red one
AhmadHija
AhmadHijaOP8mo ago
i know what i need 2 do but not how to do it only the red one
Pobiega
Pobiega8mo ago
Combine some loops with an if statement? And then you are more or less done...
AhmadHija
AhmadHijaOP8mo ago
the problem is with the loops
Kringe
Kringe8mo ago
Do you know how to use breakpoints?
AhmadHija
AhmadHijaOP8mo ago
like yea but why
Pobiega
Pobiega8mo ago
Why are the loops a problem? Look at the picture you posted
Kringe
Kringe8mo 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
AhmadHijaOP8mo ago
i dont know how to form them
Pobiega
Pobiega8mo ago
Well you have two options
AhmadHija
AhmadHijaOP8mo ago
im already lost my guy
Pobiega
Pobiega8mo 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
SpReeD8mo 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
AhmadHijaOP8mo ago
the second one is the one im supposd to do i will do it just going to eat rn
Kringe
Kringe8mo ago
Is that because we talking about squares in this context?
SpReeD
SpReeD8mo ago
Because we have coordinates in a 2d matrix, like Vector2, representing a single point of data.
Pobiega
Pobiega8mo ago
X/y are the standard coordinate names
Kringe
Kringe8mo 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
AhmadHijaOP8mo 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
Kringe8mo ago
Yes nice!
AhmadHija
AhmadHijaOP8mo ago
wait ima show you the solution its wrong bro how am i supposd to do it
Kringe
Kringe8mo ago
Could you paste the code
AhmadHija
AhmadHijaOP8mo 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
Kringe8mo ago
you are returning after checking the first value
AhmadHija
AhmadHijaOP8mo 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
Kringe8mo ago
wait that was wrong so to clarify you only are checking the vals under the daigonal right
AhmadHija
AhmadHijaOP8mo ago
under and above
Kringe
Kringe8mo ago
and the above condition is
AhmadHija
AhmadHijaOP8mo ago
all the values in the main diagonal and above need to be any number but 0
Kringe
Kringe8mo ago
ow mb but i think you turned it around
AhmadHija
AhmadHijaOP8mo ago
can u check it for me
Kringe
Kringe8mo 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
AhmadHijaOP8mo ago
ill try it
Kringe
Kringe8mo 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
AhmadHijaOP8mo ago
nah its still wrong
Kringe
Kringe8mo ago
past the code and the array for me plz
AhmadHija
AhmadHijaOP8mo ago
No description
AhmadHija
AhmadHijaOP8mo ago
array code is that
Kringe
Kringe8mo 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
AhmadHijaOP8mo 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
Kringe8mo ago
depends on the question I guess but everything working now?
AhmadHija
AhmadHijaOP8mo ago
yea
Kringe
Kringe8mo ago
nice finally
AhmadHija
AhmadHijaOP8mo 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
Want results from more Discord servers?
Add your server