Example 01
Getting started with our first example, we will be taking a look at the syntax of creating a vector in C#. This example will show you a traditional way of creating some vectors in the C# program. So, we have been starting our code using the System standard library, which has been a must in almost every C# code for general statements usage.
After this, we have included the “IO” class from the System library via the keyword “using” that will be supporting the C# input and output operations using some of its built-in functions. This program code of C# contains a single namespace “Vector” generated by a user, and it holds the same name single class “Vector” in it. Also, this code contains another class named “Test” outside of the namespace “Vector.” This class contains a single main() function of this code that will drive our code and execute it. When we run the code, the main() function starts executing its inner code, and control would be given to its WriteLine() function statement called by the Console Class to display “Vector of 5 cells” at our shell screen.
After that, we called the Create function of the other class “Test”, to create a vector of 5 cells with the repetitive value “2.4” at each of its cells. The Create function of the test class will create a vector and save the returned result within the double type array named “v,” i.e., vector of double type. The same class “Test” is calling its function “Show” to display the newly generated vector by passing it double type vector “v.” The second parameter value “3” has been showing the number of decimals we want to display for each value of a vector and the third parameter value “8” has been showing the width of each value for a vector “v.” This function call will display the created vector at our screen without further delay.
It’s my turn to elaborate on this code’s Create and Show function usage. The Create function will take value “n” as some number and a value invariable “val” from the main() to generate a double type array “r” of size “n.” The “for” loop has been used to iterate each index of vector array “r” using the iteration index “I” and the peak value at point “n.” On each iteration, it will save the value of “val” to the specific index of the vector array “R.” After all the 5 values have been stored in the vector array “r.” The vector “r” will be returned to the main() function.
After the “Show” function call, the double type array vector “v” will be passed along with the value of decimal and width, i.e., a number of decimals for each vector value and the cell width for the vector. The “For” loop has been iterated until the end of a vector “v” via the “Length” function. The WriteLine() function of the Console class within it is utilized to convert each value of decimal type to a string using the ToString() function at each index of vector “v.” The PadLeft()function has been applied to the width “w” of the vector to give the cell width of 8 characters. Let’s save the code first as it is completed.
After saving this code successfully, we have tried to use the mcs C# compiler of Ubuntu 20.04. This compilation has generated an “exe” file for the same code, and we have executed the “exe” code file with the “mono” runtime instruction. It gives the expected output, i.e., the display of the “Vector of 5 cells” string and then the display of 5 double values from the Vector.
Example 02
Let’s compare the vector with the matrix to see how one is different from the other. Thus we have updated our code a little bit by adding two new functions within this C# code. So, we have been using the same Vector and test class. The Vector class contains the same Main() method used to execute the code after running the code. The Main() function has been creating a new Vector “vct” using the VCreate function of the test class by passing the number 5 for size and 3.5 as a value to the vector. The Vshow() function has been called with the help of a Test class of this program to display only 5 decimal double values of a vector “vct” each of width “6”.
After this, we generated a 2-dimensional matrix array “mtr” of a double type with 5 rows and 5 columns via the MCreate function class with the Test class. The Test class name has been used to call MShow() function to display the matrix of 5×5, i.e., each value of 2 decimals and width ‘6’. The Test class contains the same implementation of the VCreate and VShow functions as we displayed in the above example for the Create and Show function.
Two extra functions have been used here, i.e., MCreate and MShow. The MCreate takes rows and column values to generate a matrix “res” of double type and adds value to it with the help of the “for” loop. As we haven’t assigned any value to each row or column of a matrix, thus it will input “0.0” as a double value. After that, it will return the whole matrix to the main() function of the Vector class to be used in the next function call. The MShow() function will be called by passing the double type array matrix “m” along with the decimal and width values. It will initialize several rows variable “nr” using the matrix’s Length. The 2-dimensional matrix requires two “for” loops to iterate rows and columns of a matrix “m,” i.e., “I” is used for rows, and “j” is used for columns. Each value of a matrix will be displayed after conversion into String.
We have got the single line Vector displayed first and then the 5×5 matrix on our screens on compilation and execution of this code.
Conclusion
The introductory paragraph explains how a vector is similar to arrays in C# and how it is different from matrices. This has been covered and discussed through two different examples. The first example describes ways to create and display a vector, and the other shows the creation of a matrix compared to the vector creation. The output shows that the vector display is similar to arrays, and the matrix has a 2-dimensional structure.