Skip to main content

Arrays for matrices

Since, engineers are mostly operating on algebraic operations the introduction to arrays and lists is needed. There is several types of the arrays in the C#. I will try show the most useful for me. In general the arrays and lists are used to store the data. I will try to show in easy way to how initialize the arrays.

1D array
For engineer can be understand as a vector. To defined the vector you need to declare the type name and size:

Type[ ] name = new Type[size]{ element_1, element_2, element_3, ... , element_size } ;

Let's say we need a 1D array of 4 double type numbers.

 double[ ] array1 = new double[4]{ 1.02 , 2.32 , 0.000021 , 23123.3213} ;

The other way to declare the array is to put the elements in separate lines.

Type[] name = new Type[size] ;
name[0] = element_1 ;
name[1] = element_2 ;
name[2] = element_3 ;
...
name[size-1] = element_size ;

The example:

double[ ] array1 = new double[4];
array1[0] = 1.02;
array1[1] = 2.32;
array1[2] = 0.000021;
array1[3] = 23123.3213;

Example from VS:



Remember that you can not combine different types of elements inside one array.

2D array
Arrays can be n dimensional objects. To represent the matrix the 2 dimensional array can be build according to definition below.

Type[ , ] name = new Typesize_1 , size_2 ]
{
element_1, element_2 } ,
{ element_3, element_4 }
};

or

Type[ , ] name = { { element_1, element_2 } , { element_3, element_4 } } ;

The other method of declaration is:

Type[ , ] name = new Typesize_1 , size_2 ];
name[0,0] = element_1 ;
name[0,1] = element_2 ;
name[1,0] = element_3 ;
name[1,1] = element_4 ;

Example from VS:




Comments

Popular posts from this blog

Finite Element Method Programming - 1D bar element, C# methods, C# loops

On the previous post, we established 1D bar finite element. This time we will go more into the programming in C# and try to make some automation in our code. I will try to explain: - loops: for - methods in C# - arrays 1D and 2D In the beginning, we have to build a task for us, to not feel that we are losing time. So, let's imagine that we want to check the result for the same bar, but with several divisions. For example: Does the result will change if I will create 2 finite elements instead of 1? The beginning of the code will be looking the same: namespace FEM1d_basic_1 {      class Program     {          static void Main(string[] args)         {              // The heading of the code              Console .WriteLine("FEM 1D program for bar");             // The variables              double emodulus = 210000; // 1000[kN/m2]=1GPa              double barlength = 10; // [m] meters              double elementarea = 0.01; // [m2] meter x meter       
How to write C# components in Visual Studio for Grasshopper? If you have the Visual Studio community 2017, you have to also download assembler for grasshopper v6. https://marketplace.visualstudio.com/items?itemName=McNeel.GrasshopperAssemblyforv6 After downloading the file, start it. The VS community 2017 should be chosen automatically. Now you can start VS 2017 one more time, in the new project, you should find new options: If you choose Visual C# > Rhinoceros > Grasshopper Add-on for v6, a new window will appear: The missing files have to be added to references. The RhinoCommon and Rhino.exe you will probably find somewhere on C disc (in my case it is C:\Program Files\Rhinoceros 5 (64-bit) ). The Grasshopper.dll file location you should check on your disc through searching enginge ( in my case it was :  C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper (b45a29b1-4343-4035-989e-044e8580d9cf)\0.9.76.0 ). Click finish and new empty code sh
The programming skills starts to be essential for nowadays engineer.  The  open source software create a great chance for developing automation in engineering, but also demand some programming knowledge from the structural engineers. Here I will try to slowly introduce engineers in to the basic programming knowledge. The main focus will be on the Finite Element Method in the parametric environment, but also the approach to some Eurocodes will be shown. The main programming language will be c# and python. The software in which I will be working will be the Grasshopper (add-on for Rhino) and Autodesk Dynamo. To start our journey with programming I have to explain the main types which you will find in codes: string - the text type, you can store letters, words or sentences in it. For example: string text1 = "architect is not engineer" ; Let's try to understand this declaration of variable in C#. First we are giving the type ( string ) then we are setti