Today's blogpost is about answers to coding problems:
void PrintConcentricSquare(int [,] m, int n) //params: matrix and dimension
{
if (n%2 == 1)
{
int center =n/2;
int numSquares = center + 1;
for (int i =0; i < numSquares; i++)
{
for (int x=center-i;x<=center+i;x++)
for(int y=center-i;y<=center+i;y++)
Console.Write('{0} ',m[x,y]);
Console.Writeline();
}
}
else
{
int center = n/2;
int numSquares = center;
for (int i =0; i < numSquares; i++)
{
for (int x=center-i-1;x<center+i+1;x++)
for(int y=center-i-1;y<center+i+1;y++)
Console.Write('{0} ', m[x,y]);
Console.WriteLine();
}
}
}
The above algorithm can be modified to print just the boundary of the concentric squares. When x is not at the lower or upper limit and y is not at the lower or upper limit, escape printing the cell value.
#codingexercise
Double GetNthRootProductEachTermRaisedPMinusQ (Double [] A,Double p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductEachTermRaisedPMinusQ(p, q);
}
Print concentric squares of a given square matrix.
void PrintConcentricSquare(int [,] m, int n) //params: matrix and dimension
{
if (n%2 == 1)
{
int center =n/2;
int numSquares = center + 1;
for (int i =0; i < numSquares; i++)
{
for (int x=center-i;x<=center+i;x++)
for(int y=center-i;y<=center+i;y++)
Console.Write('{0} ',m[x,y]);
Console.Writeline();
}
}
else
{
int center = n/2;
int numSquares = center;
for (int i =0; i < numSquares; i++)
{
for (int x=center-i-1;x<center+i+1;x++)
for(int y=center-i-1;y<center+i+1;y++)
Console.Write('{0} ', m[x,y]);
Console.WriteLine();
}
}
}
The above algorithm can be modified to print just the boundary of the concentric squares. When x is not at the lower or upper limit and y is not at the lower or upper limit, escape printing the cell value.
#codingexercise
Double GetNthRootProductEachTermRaisedPMinusQ (Double [] A,Double p, Double q)
{
If ( A== null) return 0;
Return A.NthRootProductEachTermRaisedPMinusQ(p, q);
}
No comments:
Post a Comment