Archive for 'C#'
Matrices
Published on February 5th, 2008.
I have constructed a class library to perform different operations with matrices. I was inspired by my university course of Linear Algebra. This library is still beta, so don’t wonder if you encounter a problem.
In mathematics, a matrix is a rectangular table of numbers. Instead of using integers only, I have used my fraction class to represent numbers in the matrix. You can sum matrices, multiply them, determine their determinant and their rang, inverse & transpose them. (more…)
Filled under C#. No Comments.
Passing Data to Threads and Retrieving Data from Threads
Published on February 4th, 2008.
When you do calls to components, which may need some time to respond, it is very essentially to put this calls in a separate thread. Otherwise you risk your application to bug for a while.
When you work with many threads sometimes you will need to pass data between them. Although .NET Framework provides an easy way to achieve this goal, I will show you another solution of this problem, which is more secure. (more…)
Filled under C#. No Comments.
Spiral Walk
Published on February 3rd, 2008.
We have a square matrix (NxN) of integers. Having given a starting corner point and a direction, we want to walk all the elements from this matrix on a spiral way. I found this problem accidentally and it vexed me. That’s why I decided to create a simple algorithm.
We are going to create a class called SpiralWalk. It provides a method called WalkIt which does all the job. This methods has three arguments: starting row & col index (zero-based) and a direction. Remember, that the point must be a corner point. Every NxN array has four corner points: (0, 0), (0, N-1), (N-1, 0), (N-1, N-1). (more…)
Filled under C#. No Comments.
Numbers Comparison
Published on February 2nd, 2008.
You have two real numbers and you want to get the greater one. How would you do this? I am going to show you a few variants to solve this problem.
1. Direct comparison
greaterNumber = (a > b) ? a : b;
This method just checks: if a is greater than b then returns a else – returns b.
2. Using arithmetic operators and boolean expressions
greaterNumber = (a > b) * a + (a <= b) * b;
Actually, this is not working in C#, but in C++. In C++, a value of type bool can be converted to a value of type int; in other words, false is equivalent to zero and true is equivalent to nonzero values. In C#, there is no conversion between the bool type and other types.
3. Using Abs method
greaterNumber = (Math.Abs(a-b) + a + b)/2;
I leave this for you to check its truth.
Filled under C#. No Comments.
Arranging Numbers at Random
Published on February 2nd, 2008.
Let’s say we want to arrange the numbers from 1 to 100 at random. This is a little confusing problem but there is a solution (not only one) for it. The basic way is the following: you have an array where you put your numbers, you generate a random number while this number is not contained in your array, then you add it to the array. Well, this method is very sluggish! When you add numbers to the array you decrease the size of the multitude of possible numbers. And when this size is too little, the while cycle loops very long until you get a random number, which is not already added to the array. Here is this example. I have used a stopwatch to count the necessary time for this method to finish. (more…)
Filled under C#. No Comments.