0

How to optimise my example code

So i made a programme which outputs Pascal's triangle until a certain row. The question i'm asking is whether this code is more or less optimal (length) or not and if not what can be done better. CODE: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Console.WriteLine("Until which row shall Pascal's triangle be created?"); int row= Convert.ToInt32( Console.ReadLine()); PasTri(row); } static void PasTri(int row){ int[,] tri= new int[row+1,row+1]; for (int i=0;i<=row;i++){ tri[i,0] =1; tri[i,i] =1; } for (int i=2;i<=row;i++){ for (int j=1; j<i;j++){ tri[i,j] =tri[i-1,j-1]+tri[i-1,j]; } } for (int i=0;i<=row;i++){ for (int j=0;j<=i;j++){ Console.Write(tri[i,j]+" "); } Console.WriteLine(); } } } }

21st Jun 2016, 8:05 PM
Klaus
1 Antwort
0
I'm not sure where the problem is. I mean it's not a long code with a lot of statements and stuff so you don't need to optimize this code. I'm not even sure if you can indeed optimize this code better than it is right now
23rd Jun 2016, 12:32 PM
Alex Ban
Alex Ban - avatar