###Can be simplified###
Can be simplified
Currently, you create a heads
array and then use two functions find_color()
and head_check()
to find the color of each element using the array. But actually, you can determine the color of each element in a much simpler fashion, without ever needing to create a temporary array in the first place.
Essentially, you are trying to trace back each element to its diagonal starting point ("head"), where the starting point may either be on the top or left edge of the matrix. You can determine which edge a given element's head is on by comparing its row number against its column number. Any element where col >= row
will have its head be on the top edge, otherwise its head will be on the left edge.
Given the above, you can reduce all of your color finding code to this one function:
// Rank is the element number, in the range 0..(size*size-1)
// Size is the length of the matrix on one side.
int find_color(int rank, int size)
{
int row = rank / size;
int col = rank % size;
if (col >= row) {
// Diagonal head must be in top row.
return col - row;
} else {
// Diagonal head must be on left side.
return (row - col) + size - 1;
}
}
###Can be simplified###
Currently, you create a heads
array and then use two functions find_color()
and head_check()
to find the color of each element using the array. But actually, you can determine the color of each element in a much simpler fashion, without ever needing to create a temporary array in the first place.
Essentially, you are trying to trace back each element to its diagonal starting point ("head"), where the starting point may either be on the top or left edge of the matrix. You can determine which edge a given element's head is on by comparing its row number against its column number. Any element where col >= row
will have its head be on the top edge, otherwise its head will be on the left edge.
Given the above, you can reduce all of your color finding code to this one function:
// Rank is the element number, in the range 0..(size*size-1)
// Size is the length of the matrix on one side.
int find_color(int rank, int size)
{
int row = rank / size;
int col = rank % size;
if (col >= row) {
// Diagonal head must be in top row.
return col - row;
} else {
// Diagonal head must be on left side.
return (row - col) + size - 1;
}
}
Can be simplified
Currently, you create a heads
array and then use two functions find_color()
and head_check()
to find the color of each element using the array. But actually, you can determine the color of each element in a much simpler fashion, without ever needing to create a temporary array in the first place.
Essentially, you are trying to trace back each element to its diagonal starting point ("head"), where the starting point may either be on the top or left edge of the matrix. You can determine which edge a given element's head is on by comparing its row number against its column number. Any element where col >= row
will have its head be on the top edge, otherwise its head will be on the left edge.
Given the above, you can reduce all of your color finding code to this one function:
// Rank is the element number, in the range 0..(size*size-1)
// Size is the length of the matrix on one side.
int find_color(int rank, int size)
{
int row = rank / size;
int col = rank % size;
if (col >= row) {
// Diagonal head must be in top row.
return col - row;
} else {
// Diagonal head must be on left side.
return (row - col) + size - 1;
}
}
###Can be simplified###
Currently, you create a heads
array and then use two functions find_color()
and head_check()
to find the color of each element using the array. But actually, you can determine the color of each element in a much simpler fashion, without ever needing to create a temporary array in the first place.
Essentially, you are trying to trace back each element to its diagonal starting point ("head"), where the starting point may either be on the top or left edge of the matrix. You can determine which edge a given element's head is on by comparing its row number against its column number. Any element where col >= row
will have its head be on the top edge, otherwise its head will be on the left edge.
Given the above, you can reduce all of your color finding code to this one function:
// Rank is the element number, in the range 0..(size*size-1)
// Size is the length of the matrix on one side.
int find_color(int rank, int size)
{
int row = rank / size;
int col = rank % size;
if (col >= row) {
// Diagonal head must be in top row.
return col - row;
} else {
// Diagonal head must be on left side.
return (row - col) + size - 1;
}
}