03 Tem

C# Towers Of Hanoi Problem Implementation

This example contains a recursive solution for the Towers of Hanoi problem.

For more information about Hanoi Tower:
http://en.wikipedia.org/wiki/Tower_of_Hanoi

Usage:

            MoveDisc(3, 1, 3, 2);

            //Output:
            //Index of 20 value in list is 4
            //Move disk 1 from tower 1 to tower 3
            //Move disk 2 from tower 1 to tower 2
            //Move disk 1 from tower 3 to tower 2
            //Move disk 3 from tower 1 to tower 3
            //Move disk 1 from tower 2 to tower 1
            //Move disk 2 from tower 2 to tower 3
            //Move disk 1 from tower 1 to tower 3
        public void MoveDisc(int n, int from, int to, int other)
        {
            if (n > 0)
            {
                MoveDisc(n - 1, from, other, to);
                Console.WriteLine("Move disk {0} from tower {1} to tower {2}", n, from, to);
                MoveDisc(n - 1, other, to, from);
            }
        }
03 Tem

C# Sequential Search Example

Sequential search(Linear search) is the simplest search algorithm. It is a special case of brute-force search. It is a method for finding a particular value in a list. To achieve this, it checks every one of its elements one.

Usage:

            //Sorted array
            int[] arr = new int[10]
            {
                1, 2, 4, 11, 20, 28, 48, 84, 96, 106
            };

            int index = SequentialSearch(arr, 20);
            Console.WriteLine("Index of 20 value in array is " + index.ToString());    
            
            //Output:
            //Index of 20 value in array is 4

Sequential Search Implementation:

        private int SequentialSearch(int[] arr, int searchNumber)
        {
            arr[arr.Length - 1] = searchNumber;
            
            int i;
            for (i = 0; arr[i] != searchNumber; i++);
            return (i < arr.Length -1) ? i : -1;
        }

See Also:
Binary Search

03 Tem

C# Binary Search Example

Binary search or half-interval search algorithm finds the position of a specified input value (the search “key”) within an array sorted by key value.
For more information about Binary Search:
http://en.wikipedia.org/wiki/Binary_search_algorithm

This examples shows how to search an item in an array using binary search.

Usage:

            //Sorted array
            int[] arr = new int[10]
            {
                1, 2, 4, 11, 20, 28, 48, 84, 96, 106
            };

            int index1 = arr.ToList().BinarySearch(20);
            int index2 = BinarySearchIterative(arr, 20);
            int index3 = BinarySearchRecursive(arr, 20, 0, arr.Length - 1);
            Console.WriteLine("Index of 20 value in list is " + index1.ToString() + " (using .NET Frameowork)");
            Console.WriteLine("Index of 20 value in list is " + index2.ToString() + " (using BinarySearchIterative method)");
            Console.WriteLine("Index of 20 value in list is " + index3.ToString() + " (using BinarySearchRecursive method)");

            //Output:
            //Index of 20 value in list is 4 (using .NET Frameowork)
            //Index of 20 value in list is 4 (using BinarySearchIterative method)
            //Index of 20 value in list is 4 (using BinarySearchRecursive method)

Iterative and recursive methods:

 
        private int BinarySearchIterative(int[] arr, int searchNumber)
        {
            int left = 0;
            int right = arr.Length - 1;

            int middle;
            while (left <= right)
            {
                middle = (left + right) / 2;
                switch (Compare(arr[middle], searchNumber))
                {
                    case -1: left = middle + 1; break;
                    case 0: return middle;
                    case 1: right = middle - 1; break;
                }
            }
            return -1;
        }

        private int BinarySearchRecursive(int[] arr, int searchnum, int left, int right)
        {
            int middle;
            while (left <= right)
            {
                middle = (left + right) / 2;
                switch (Compare(arr[middle], searchnum))
                {
                    case -1: return BinarySearchRecursive(arr, searchnum, middle + 1, right);
                    case 0: return middle;
                    case 1: return BinarySearchRecursive(arr, searchnum, left, middle - 1);
                }
            }
            return -1;
        }

        private int Compare(int x, int y)
        {
            return x < y ? -1 : (y < x ? 1 : 0);
        }

See Also:
Sequential Search(Linear Search)

27 Haz

C# String Formatting For A Number Using Scientific Notation

This example shows how to format a number using scientific notation. To do this, you can use string.Format method.

Samples:

            double number1 = 12345.67890123;
            Console.WriteLine("Default Format: " + number1);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number1));
            Console.WriteLine();

            int number2 = 123456789;
            Console.WriteLine("Default Format: " + number2);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+0}", number2));
            Console.WriteLine();

            double number3 = 12345.67890123;
            Console.WriteLine("Default Format: " + number3);
            Console.WriteLine("Scientific Notation: " + string.Format("{0:#.##E+00}", number3));
            Console.WriteLine();

Output is like below:

Default Format: 12345.67890123
Scientific Notation: 1.23E+4

Default Format: 123456789
Scientific Notation: 1.23E+8

Default Format: 12345.67890123
Scientific Notation: 1.23E+04
27 Haz

C# Chart Control Example

This example shows how to display your data in your Windows Forms program as a bar graph or spline chart. To achieve this, you use Chart class in System.Windows.Forms.DataVisualization.Charting. Chart control can be found in Toolbox(.NET Framework 4.0 or newer). Older versions will not have the Chart control available.
To add a chart control to your application,

  • In design view, open the Toolbox.
  • From the Data category, drag a Chart control to the design area.

If you cannot see the Chart control in the Toolbox, right click in the Toolbox, select Choose Items, and then select the following namespaces in the .NET Framekwork Components tab:

  • System.Web.UI.DataVisualization.Charting
  • System.Windows.Forms.DataVisualization.Charting

C# Winform Choose Toolbox Items

Usage:

        void FrmChartExample_Load(object sender, EventArgs e)
        {
            BarExample(); //Show bar chart
            //SplineChartExample();
        }


This method shows how to add a bar chart.

        public void BarExample()
        {
            this.chartControl.Series.Clear();

            // Data arrays
            string[] seriesArray = { "Cat", "Dog", "Bird", "Monkey" };
            int[] pointsArray = { 2, 1, 7, 5 };

            // Set palette
            this.chartControl.Palette = ChartColorPalette.EarthTones;

            // Set title
            this.chartControl.Titles.Add("Animals");

            // Add series.
            for (int i = 0; i < seriesArray.Length; i++)
            {
                Series series = this.chartControl.Series.Add(seriesArray[i]);
                series.Points.Add(pointsArray[i]);
            }
        }

C# Bar Chart Example

This method shows how to add a spline chart.

        private void SplineChartExample()
        {
            this.chartControl.Series.Clear();

            this.chartControl.Titles.Add("Total Income");

            Series series = this.chartControl.Series.Add("Total Income");
            series.ChartType = SeriesChartType.Spline;
            series.Points.AddXY("September", 100);
            series.Points.AddXY("Obtober", 300);
            series.Points.AddXY("November", 800);
            series.Points.AddXY("December", 200);
            series.Points.AddXY("January", 600);
            series.Points.AddXY("February", 400);
        }

C# Spline Chart Example