Archive for the ‘ C# ’ Category

Find out the sum of integers

If an array is having integers/Char/special Char… Ex: "PST456DA85M2A!!23++46", find out the sum of integers.

Note: If we find consecutive digits in array we need to treat it as number, let say 456, we need to treat it as [ four hundred and fifty six]. Write a program to get the output by summing 456+85+2+23+46.

Also this needs to be done in less number of iterations.

Solution

            string inputString = "PST456DA85M2A!!23++4600";
            char[] inputChars = inputString.ToCharArray();
            double sum = 0;
            double tempnum = 0;
            double powerCounter = 0;
            double num;
            for (int i = inputChars.Length - 1; i >= 0; i--)
            {
                if (inputChars[i] >= 48 && inputChars[i] <= 57)
                {
                    num = inputChars[i] - 48;
                    tempnum = num * Math.Pow(10, powerCounter) + tempnum;
                    powerCounter = powerCounter + 1;
                }

                else
                {
                    sum = sum + tempnum;
                    powerCounter = 0;
                    tempnum = 0;
                }

            }
            Console.WriteLine();
            Console.WriteLine(inputString);
            Console.WriteLine(sum);
            Console.ReadLine();

Write the program to Print Star

Write the program to implement
*****
***
*
***
*****

 

public static void Star1(int n)
    {

      int k = n;
      int temp = n;
      Console.WriteLine();
      while (n > 0)
      {
        for (int i = 0; i < k; i++)
        {
          Console.Write("*");
        }
        
        Console.WriteLine();
        n = n - 1;
        if (n > temp / 2)
        {
          k = k - 2;
        }
        else
        {
          k = k + 2;
        }

      }
    }
 
This program will work for odd values of n

Counting Sort

Definition from Wikipedia

In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the maximum key value, so it is only suitable for use directly in situations where the keys are not significantly larger than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.

Here is the C# implementation of Counting Sort

 class Program
  {
    static void Main(string[] args)
    {
      //create an input array with maximum value of an element is 1024
      int[] input = new int[10000];
      Random random = new Random();
      for (int i = 0; i < 10000; i++)
      {
        input[i] = random.Next(0, 1024);

      }
      Sorting.CountingSort(input);
    }

   
  }

 public class Sorting
  {
    public static void CountingSort(int[] arrayA)
    {
      Console.WriteLine("Original Array");
      for (int i = 0; i < arrayA.Length; i++)
      {

        Console.Write(arrayA[i] + " , ");
      }
      Console.WriteLine("");
      int k = 1024;
      int[] arrayB = new int[arrayA.Length];
      int[] arrayC = new int[k];
      for (int i = 0; i < arrayC.Length; i++)
      {

        arrayC[i] = 0;
      }
      for (int j = 0; j < arrayA.Length; j++)
      {
        arrayC[arrayA[j]] = arrayC[arrayA[j]] + 1;
      }

      //Place the number of elements less than each value at i into array C.    
      for (int i = 1; i < k; i++)
        arrayC[i] = arrayC[i] + arrayC[i - 1];

      //Place each element of arrayA into its correct sorted position in the
      //output array B.
      for (int j = arrayA.Length - 1; j >= 0; j--)
      {
        arrayB[arrayC[arrayA[j]] - 1] = arrayA[j];
        arrayC[arrayA[j]] = arrayC[arrayA[j]] - 1;
      }

      //Overwrite the original arrayA with the output arrayB.
      Console.WriteLine("Sorted Array");
      for (int i = 0; i < arrayA.Length; i++)
      {
        arrayA[i] = arrayB[i];
        Console.Write(arrayA[i] + " , ");
      }

      Console.WriteLine("");

    }
  }
 
in this program I am showing output on console, it can be easily saved on file also.

Optional Parameters in C#

With the release of .NET 4.0 C# programmers are now able to created methods with optional parameters like VB programmers doing. Optional arguments are widely used in VBA for long time. Although they make life a little bit easier for programmers (you don’t have to repeat default values in your method calls).

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace optionalParameters
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             OptionalParameterTest t = new OptionalParameterTest();
  13.             Console.WriteLine("Optional Parameter :  {0}", t.OptionalParameter("One"));
  14.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1"));
  15.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","three1"));
  16.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","Three1","Four1"));
  17.             Console.ReadLine();
  18.  
  19.         }
  20.  
  21.     }
  22.     public class OptionalParameterTest
  23.     {
  24.         public string OptionalParameter(string  one, string two = "Two",string three="Three",string four="Four")
  25.         {
  26.  
  27.            
  28.             return one + "  " + two + "  " + three + "  " + four;
  29.         }
  30.     }
  31.  
  32. }

Optional Parameters

Understanding Method Overloading in C#

Like other Programming languages C# also supports method overloading. Method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function. [According to wikipedia]

Method Overloading Example

Suppose in our application we want to add to two integers, double, float and long. The first way is to create unique methods for each addition operation or call a single method name with distinct set of arguments.

The Visual Studio IDE will provide you assistance while calling overloaded method.

method overloading

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Method_Overloading
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("**********************************Method Overloading Demo**********************************");
  13.             Console.WriteLine("Add two int :" + Add(1, 1));
  14.             Console.WriteLine("Add two Double :" + Add(1.0, 1.0));
  15.             Console.WriteLine("Add two float :" + Add(11.12, 1.123));
  16.             Console.WriteLine("Add two long :" + Add(234561, 11235678));
  17.             Console.ReadLine();
  18.  
  19.         }
  20.         private static  Int64 Add(int num1, int num2)
  21.         {
  22.             return num1 + num2;
  23.         }
  24.         private static double Add(double num1, double num2)
  25.         {
  26.             return num1 + num2;
  27.         }
  28.         private static float Add(float num1, float num2)
  29.         {
  30.             return num1 + num2;
  31.         }
  32.         private static long Add(long num1, long num2)
  33.         {
  34.             return num1 + num2;
  35.         }
  36.     }
  37. }

Method overloading does not depends upon the return type of method, if you are having two methods with unique name with equal number of arguments and of same type with different return type than these methods are not overloaded method instead they are same methods and compiler will give error at compile time.

method overloading error