Pascal´s triangle c# - c#

i´d like to build a pascal´s triangle as a square matrix in c# like this.
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
But the following code didn´t perform, could you please help me?
Console.Write("Size of Matrix: ");
int size = Convert.ToInt32(Console.ReadLine());
int[,] pascal = new int[size, size];
for (int i = 0; i < pascal.GetLength(0);i++)
{
for (int j = 0; j < pascal.GetLength(1); j++)
{
if (j > i )
{
pascal[i, j] = 0;
}
if (i == j || j == 0)
{
pascal[i, j] = 1;
}
if (i !=j)
{
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
Console.Write($"{pascal[i,j ],5 }");
}
Console.WriteLine();
}
thx

You are forgot else if and rewrite data if (i != j)
Console.Write("Size of Matrix: ");
int size = Convert.ToInt32(Console.ReadLine());
int[,] pascal = new int[size, size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (i == j || j == 0)
{
pascal[i, j] = 1;
}
else if (j > i)
{
pascal[i, j] = 0;
}
else if (i != j)
{
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}
Console.Write($"{pascal[i, j],5 }");
}
Console.WriteLine();
}

Your problem is your ifs, need to be else if otherwise you will be trying to reference negative indexes in the array with pascal[i - 1, j - 1] etc.
for (int i = 0; i < pascal.GetLength(0); i++)
{
for (int j = 0; j < pascal.GetLength(1); j++)
{
if (j > i)
pascal[i, j] = 0;
else if (i == j || j == 0)
pascal[i, j] = 1;
else if (i != j)
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
Console.Write($"{pascal[i, j],5}");
}
Console.WriteLine();
}
Another way you could achieve this is with good old fashioned math
for (var i = 0; i < rows; i++)
for (var j = 0; j <= i; j++)
if (j != 0 && i != 0)
pascal[i, j] = val = val * (i - j + 1) / j;
else
pascal[i, j] = 1;
To Output
for (var i = 0; i < rows; i++)
{
for (var j = 0; j < rows; j++)
Console.Write($"{pascal[i, j]} ");
Console.WriteLine();
}

A new array always contain zeros. You could seed the 1's outside the loops, then only loop through the rest.
I would also separate the construction of the array, from the printing method. If you only wanted to print it, you'd only need one array.
int[,] pascal = new int[size, size];
pascal[0, 0] = 1;
for (int i = 1; i < size; i++)
{
pascal[i, 0] = pascal[i, i] = 1;
for (int j = 1; j <= i - 1; j++)
pascal[i, j] = pascal[i - 1, j - 1] + pascal[i - 1, j];
}

To help to grasp this dynamic programming concept the following is my recursive solution.
for(var i = 0; i < 5; ++i)
{
for(var j = 0; j < i + 1; ++j)
{
Console.Write($"{p(i, j)} ");
}
Console.WriteLine();
}
static int p(int i, int j)
=> (j == 0 || i == j)
? 1
: p(i - 1, j) + p(i - 1, j - 1);

Related

How to store the results of an array matrix into a smaller array c#

I need to add a value which would be either p1(payoff one) or p2 (payoff two) to the surrounding four neighbours of a value in a matrix that'll then be printed into a new array matrix. If it's 1 then p1 will need to be added to it's neighbours or if its 0 then p2 will be added to its neighbours. I've tried to do this approach with a nested for loop but my 'if' statement in my for loop is giving me errors and Im not sure where to go next with it.
class MainClass
{
static void Main(string[] args)
{
int m, n, i, j, p1, p2;
// rows and columns of the matrix+
m = 3;
n = 3;
//Payoff matrix
p1 = 10; //cheat payoff matrix
p2 = 5; //co-op payoff matrix
int[,] arr = new int[3, 3];
Console.Write("To enter 1 it means to co-operate" );
Console.Write("To enter 0 it means to cheat");
Console.Write("Enter elements of the Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("Printing Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
// how to change the values of the matrix
int[] payoffMatrix = new int[4];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if(arr[i,j] == 1)
{
arr[i, j] = arr[i - 1, j] , arr[i + 1, j] , arr[i, j - 1] , arr[i, j + 1];
}
}
Console.WriteLine();
}
The result of the neighbouring values need to be printed into the payoff matrix aswell
If I understood correctly you need to make a copy of your array first. Because otherwise you would read e.g. "1" from a array position (i + 1) from the current iteration. That's probably not what you want.
Then you just set the desired values in your for-loop. You need some bound checking, because e.g. arrNew[i - 1] will only be accessible if i > 0
this gives you something like:
int[,] arrNew = arr.Clone() as int[,]; //creates a copy of arr
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (arr[i, j] == 1)
{
if (i > 0) //bounds checking
{
arrNew[i - 1, j] = 1;
}
if (i < m - 1) //bounds checking
{
arrNew[i + 1, j] = 1;
}
if (j > 0) //bounds checking
{
arrNew[i, j - 1] = 1;
}
if (j < n - 1) //bounds checking
{
arrNew[i, j + 1] = 1;
}
}
}
}
For a matrix:
0 0 0
0 1 0
0 0 0
the result would be
0 1 0
1 1 1
0 1 0

How can i improve this code to find rectangles and not only squares

I have this code and I work finding squares of "0" in 2d arrays.
How can I make find rectangles and not only squares?
I'm having some difficulty doing this.
// Set first column of S[,]
for (i = 0; i < R; i++)
{
S[i, 0] = M[i, 0];
}
// Set first row of S[][]
for (j = 0; j < C; j++)
{
S[0, j] = M[0, j];
}
// Construct other entries of S[,]
for (i = 1; i < R; i++)
{
for (j = 1; j < C; j++)
{
if (M[i, j] == 0)
S[i, j] = Math.Min(S[i, j - 1], Math.Min(S[i - 1, j], S[i - 1, j - 1])) + 1;
}
}
// Find the maximum entry, and indexes of maximum entry in S[,]
max_of_s = S[0, 0];
max_i = 0;
max_j = 0;
for (i = 0; i < R; i++)
{
for (j = 0; j < C; j++)
{
if (max_of_s < S[i, j])
{
max_of_s = S[i, j];
max_i = i;
max_j = j;
}
}
}
Console.WriteLine("Maximum size sub-matrix is: ");
for (i = max_i; i > max_i - max_of_s; i--)
{
for (j = max_j; j > max_j - max_of_s; j--)
{
M[i, j] = 2;
Console.Write(M[i, j] + " ");
}
Console.WriteLine();
}
Find any size rectangles and not only squares.

Assigning value in 2D array not working - swapping indices

Basically I have a 2D array of which looks like this:
0 x x -1
x 0 x x
x x 0 x
-1 x x 0
If using two for loops (nested) with indices 'i' and 'j' and if they equal each other, e.g. i and j = 2, then the value placed in array[i, j] will be '0'. If 'i' = array.Length - 1 and 'j' = 0 (or vice versa) then value in array[i, j] will be -1.
What I'm trying to do is place random numbers between 1 and 20 in the places denoted by 'x'. I know how to do this, but, I want the numbers in the 'x' on the other side of the '0's to be equal. For example,
value # i = 2, j = 3 is equal to value # i = 3, j = 2.
Now I've already done this in a previous program by doing:
array[j, i] = array[i, j]
But for some reason in this new program this no longer works, at all.
What's wrong?
Here's the actual code:
size = r.Next(4, 8);
V = new int[size, size];
for (int i = 0; i < size; i++)
{
s = "";
for (int j = 0; j < size; j++)
{
if (i == j)
{
V[i, j] = 0;
}
else if ((i == size - 1 && j == 0) || (i == 0 && j == size - 1))
{
V[i, j] = -1;
}
else
{
V[i, j] = r.Next(1, 20);
}
V[j, i] = V[i, j];
s += "|" + V[i, j] + "|";
}
Console.WriteLine(s);
}
Console.Read();
the problem with the algorithm is that its re-overwriting (overwriting twice) the lower half of the matrix.
try this loop instead:
for (var i = 0; i < size; i++)
{
s = "|";
for (int j = 0; j < size; j++)
{
if (i == j)
{
V[i, j] = 0;
}
else if (i == size - 1 && j == 0 || i == 0 && j == size - 1)
{
V[i, j] = -1;
}
else if (i < j)
{
V[i, j] = r.Next(1, 20);
V[j, i] = V[i, j];
}
s += $"{V[i, j],2}|";
}
Console.WriteLine(s);
}

Finding diagonal in 2D array and replacing with 0 above it

So the start is:
Random r = new Random();
int[,] mas = new int[4, 5];
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
{
mas[i, j] = r.Next(1, 10);
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Looks something like
4 3 5 6 2
3 5 6 7 4
2 3 4 5 5
2 3 4 5 6
What i i need is to get 0 above the diagonal.
4 0 0 0 0
3 5 0 0 0
2 3 4 0 0
2 3 4 5 0
This is what i've got so far, no what i need , but atleast got some diagonal and some 0's.
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = i; j < mas.GetLength(1); j++)
{
mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
If you want to do everything in one go, you could do this:
Random r = new Random();
int[,] mas = new int[4, 5];
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = 0; j < mas.GetLength(1); j++)
{
mas[i, j] = j > i ? 0 : r.Next(1, 10);
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
Console.WriteLine();
Otherwise, your second section needs to be:
for (int i = 0; i < mas.GetLength(0); i++)
{
for (int j = i; j < mas.GetLength(1); j++)
{
if(j > i) mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Console.WriteLine();
}
try this one (inside the inner loop):
if(j > i ) mas[i, j] = 0; // column number > row number, above diagonal
or the (better) alternative (inside the outer loop):
for (int j = i+1; j < mas.GetLength(1); j++)
{
mas[i, j] = 0;
Console.Write("{0}\t", mas[i, j]);
}
Using Linq
Random r = new Random();
//int[,] mas = new int[4, 5];
int[][] mas = new int[4][];
for (int i = 0; i < mas.Length; i++)
{
mas[i] = new int[5];
for (int j = 0; j < mas[i].Length; j++)
{
mas[i][j] = r.Next(1, 10);
Console.Write("{0}\t", mas[i][j]);
}
Console.WriteLine();
}
for (int i = 0; i < mas.Length; i++)
{
mas[i] = mas[i].Select((c, ind) =>
{
if (ind > i)
c = 0;
return c;
}).ToArray();
}

How to improve nested loop in C#?

I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.

Categories