Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, February 26, 2010

.NET 4.0 : Complex Numbers

 

    Hello today I’m going to start to describe and apply the new classes which were added into .NET 4.0 Framework.

 

When I was 16, it always was a problem there. Sex was in the air, a lot of different opportunities had the habit to rise without any prevention at all, and I was loving challenges. BUT, it also was the period when the most useless and useful information was taken in and out from the classes. Physics, mathematics, chemistry and others were at the same time boring and challenging.

One of the basic subjects in math was : Quadratic equations, and also introduction of complex numbers, which was pretty strange… imaginary and real parts formed the complex number. But these numbers has a lot applications in physics, astronomy and geometry.

Question how did we solved these kind of equations in the past?
We did a structure or a class and tried different things with it, like multiplication and division, and others as well. But sometimes we didn’t really figured out very well the logic.

Now, guess what, in .NET 4.0 we already got a Complex structure, which in fact represents the complex number.

So here is a very simple application of it:

using System;
using System.Numerics;

namespace Intro.ComplexNumbers
{
    class Program
    {

        static void Main(string[] args)
        {
            // do it always, we'll stop the loop by pressing Ctrl + C.
            while (true)
            {
                string errorMsg = string.Empty;

                Console.WriteLine("To solve the quadratic equations using the Quadratic Formula,");

                int a = ReadCoeficientValueFromConsole("a", "Please enter ax^2: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;    
                }

                int b = ReadCoeficientValueFromConsole("b", "Please enter bx: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;
                }

                int c = ReadCoeficientValueFromConsole("c", "Please enter c: ", ref errorMsg);

                if (!string.IsNullOrEmpty(errorMsg))
                {
                    ShowErrorMessage(errorMsg);
                    break;
                }

                // we're assuming that the discriminant is negative
                // for more information visit http://en.wikipedia.org/wiki/Quadratic_Formula#Quadratic_formula
                Complex root1 = new Complex(-b / 2 * a, Math.Sqrt(4 * a * c - b * b) / 2 * a);
                Complex root2 = new Complex(-b / 2 * a, (-1) * Math.Sqrt(4 * a * c - b * b) / 2 * a);

                Console.WriteLine("Roots are: ");
                Console.WriteLine("Root1 (positive): {0}", root1);
                Console.WriteLine("Root2 (negative): {0}", root2);
            }
        }

        /// 
        /// Shows error message in console.
        /// 
        /// Error message.
        private static void ShowErrorMessage(string errorMsg)
        {
            Console.WriteLine(errorMsg);
            Console.ReadKey();
        }

        /// 
        /// Reads a string from console and tries to convert it into an Int32, and assign it 
        /// to a coefficient.
        /// 
        /// Coefficient name.
        /// String which will be shown to ask the coefficient introduction.
        /// The coefficient Int32 value.
        private static int ReadCoeficientValueFromConsole(string coefficientName, string requieringString, ref string errorMsg)
        {
            int coeficient = 0;

            Console.WriteLine(requieringString);

            string aValue = Console.ReadLine();
            if (!string.IsNullOrEmpty(aValue))
            {
                try
                {
                    coeficient = Convert.ToInt32(aValue);
                }
                catch (FormatException)
                {
                    errorMsg = "Your '" + coefficientName + "' coeficient is not a numeric value.";
                }
                catch (Exception)
                {
                    errorMsg = "There was an exception raised because of coeficient '" + coefficientName + "'";
                }
            }

            return coeficient;
        }
    }
}

Here are some screenshots:

Figure 1. Coefficient error

Figure 2. Good coefficients solving 3x2 + 9x +7 = 0

Download source from here

NOTE: I’ve done this project using Visual Studio 2010 RC.

Wednesday, September 9, 2009

New Udevi C# courses: Udev'itRight

Hi,
I decided to start C# begginers courses online.
I'll cover 3 main branches of applications:
1. Desktop app (console, WinForms, WPF)
2. Web apps (ASP.NET MVC, Silverlight)
3. Mobile Apps (Windows Mobile)
and the course itself will consist in writting 3 different applications:
1st app - Green Application - will be a simple app, which will be made by 2 of us, me and my student. Here I'm gonna try to put the basics of real project knowledges like team work, versionning source code software, code comments, bugtracking and also try to put my student to work. I mean really... put his brain in action, get some logical stuff to do.
2nd app - Yellow Application will be more complicated so my student could progress and dive into Programming wolrd himself. But any student may need some help in DEV process, so i'm gonna just give him some advices, may be directions. The yellow level can really permit it. The application itself will have more functional stuff than green app had. Plus it will have a technique to learn as a bonus (it is just a plan for now so let it be a little secret)
3rd app - Red application - the highest level of complexity (for begginers), no chance to ask me for an advice, it will be the work of the student itself. Nice bussiness apps with clean functionality, without bugs, with commented code and a series of tests runned over this code. That's what I call the highest level of quality. I know that will be some persons who just won't make it. But that's ok... at least you've tried it and you will know your errors: what was done wrong, where you should pay more attention. Because this is really what we are doing, helping people to get experience.
So i'm gonna try to combine more persons (5-6) in a team. And work individually and in team with these persons. Aproximate period of courses duration is 4-5 months. 2 times a week for 2 hours each time.
You need to have skype and Mikogo installed.
I'll covering a wide range of subjects.
So if you want to join you should put a comment to this post and give me some details so I could contact you. Or just email me at monomachus85@yahoo.com.
P.S. I am thinking now about improvements to Frank Car System, so if you have some thoughts about that you can comment that post as well.