Reputation: 11
Given 2 dimensional:
int[,] arr2d = new int[3,3]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Objective: Print the 2d array in the given triangle pattern so it appears as below:
Upvotes: 1
Views: 267
Reputation: 172
I thought this is print along its main diagonal.
.
In the example case, we list the indices of arr2d
:
(0, 2)
(0, 1), (1, 2)
(0, 0), (1, 1), (2, 2)
(1, 0), (2, 1)
(2, 0)
See the regular pattern?
x
of first element on each line remains 0
, and the y
decreases. y
of first element on each line remains 0
, and the x
increases.(x++, y++)
, until either x
or y
≥3.For more general way, a n
dimensional matrix has 2n-1
line of trangle.
n
lines, the x
of first element remains to 0
, the y
decreases. n
lines, the y
of first element remains to 0
, the x
increases. (x++, y++)
until either x
or y
≥3.Here's the code:
static void Main(string[] args) {
int[,] arr2d = new int[3,3]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int dimension = 3;
int x = 0, y = dimension - 1
while(y >= 0) {
WriteDiagalNumbers(arr2d, x, y, dimension);
y--;
// or shorten as
// WriteDiagalNumbers(arr2d, x, y--, dimension);
}
x = 1;
y = 0;
while(x < dimension) {
WriteDiagalNumbers(arr2d, x, y, dimension);
x++;
// or shorten as
// WriteDiagalNumbers(arr2d, x++, y, dimension);
}
}
static void WriteDiagalNumbers(int[,] arr, int x, int y, int dimension) {
List<int> nums = new List<int>();
while(x < dimension && y < dimension) {
nums.Add(arr[x, y]);
x++;
y++;
// or shorten as
// nums.Add(arr[x++, y++]);
}
Console.WriteLine(string.Join(", ", nums));
}
Gives the output:
3
2, 6
1, 5, 9
4, 8
7
Upvotes: 2
Reputation: 39132
I like Sheey's answer, though it bothers me that the array dimensions are hard-coded in that solution. So here's a generic approach using GetUpperBound() to determine the number of rows/columns.
Calling GetUpperBound(0)
would tell you how many ROWS, while GetUpperBound(1)
would give the number of COLUMNS.
We can get the coordinates of all the diagonal starting positions by starting in the upper right corner of the array and moving left. Once we hit the left side we travel down the array. From each of those starting positions we get the diagonal values by incrementing both of the starting x/y positions while they are within the bounds of the array. This is one of those rare occasions where a for
loop with multiple variables can be used.
The code also pads the output based on the largest value so that the triangle can handle any size integers.
Note that when accessing a 2D array, the x
and y
parameters are reversed:
arr[y, x]
You list the y
value as the first parameter, and the x
value as the second parameter.
So with this array:
int[,] arr2d = new int[,]
{
{85, 86, 87, 88},
{89, 90, 91, 92},
{93, 94, 95, 96},
{97, 98, 99, 100}
}
The 88 value, using zero based notation, would be normally thought of as at coordinates (2, 0), but would be accessed with arr2d[0, 2]
.
Similarly, the 97 value would be normally thought of as at coordinates (0, 2), but would be accessed with arr2d[2, 0]
.
I felt this approach was different enough to warrant an additional answer to the question:
static void Main(string[] args)
{
int[,] arr2d = new int[,]
{
{85, 86, 87, 88},
{89, 90, 91, 92},
{93, 94, 95, 96},
{97, 98, 99, 100}
};
printTriangle(arr2d);
Console.Write("Press Enter to quit...");
Console.ReadLine();
}
static void printTriangle(int[,] arr)
{
// Get all the starting positions for "diagonals":
// Start in the top right of the array,
// then move all the way left,
// followed by all the way down.
int y = 0;
int x = arr.GetUpperBound(1);
bool travelLeft = true;
bool triangleComplete = false;
List<string> diagonalValues = new List<string>();
int pad = arr.Cast<int>().Max().ToString().Length;
while (!triangleComplete)
{
diagonalValues.Clear();
for(int yTemp = y, xTemp = x;
xTemp <= arr.GetUpperBound(1) && yTemp <= arr.GetUpperBound(0);
xTemp++, yTemp++)
{
diagonalValues.Add(arr[yTemp, xTemp].ToString().PadLeft(pad));
}
Console.WriteLine(String.Join(", ", diagonalValues));
if (travelLeft)
{
x--; // move left
travelLeft = (x > 0);
}
else
{
y++; // move down
triangleComplete = (y > arr.GetUpperBound(1));
}
}
}
Output:
88
87, 92
86, 91, 96
85, 90, 95, 100
89, 94, 99
93, 98
97
Press Enter to quit...
Upvotes: 1