ideas | discover | explore

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

Thursday, June 28, 2012

C# 15 - Selection sort

Implementing a sort using selection sort algorithm.

1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.

static void Main(string[] args)
{
    int[] array = { 3, 2, 1, 4, 8 };
    Console.Write("Before sort: ");
    PrintArray(array);
    Console.WriteLine();
    SelectionSort(array);
    Console.Write("After sort : ");
    PrintArray(array);
    Console.WriteLine();

    Console.ReadLine();

}

private static void SelectionSort(int[] sortArray)
{
    int smallest = 0;
    int smallestIndex = 0;

     /* advance the position through the entire array */
     /*   (could do i < n-1 because single element is also min element) */
     for (int i = 0; i < sortArray.Length; i++)
     {
          smallest = sortArray[i];//the smallest value is at index 0
          smallestIndex = i;//store the index of the smallest value

           //find the index of the smallest value
                for (int x = i + 1; x < sortArray.Length; x++)
                {
                    if (sortArray[x] < sortArray[i])
                        smallestIndex = x;
                }

                //swap/exchange the smallest value found with last index
                sortArray[i] = sortArray[smallestIndex];
                sortArray[smallestIndex] = smallest;
            }
        }


private static void PrintArray(int[] array)
{
     for (int i = 0; i < array.Length; i++)
     {
          Console.Write(array[i] + " ");
     }
}


3. Press F5 to run and see the results.


4. Thank you.

Reference(s):
http://en.wikipedia.org/wiki/Selection_sort
http://www.sorting-algorithms.com/selection-sort

Sunday, September 18, 2011

C# 6 - – What If? Or, Else If....?


1. Create a new console project.
2. Edit the Program.cs file as shown below.



        static void Main(string[] args)
        {
            Console.WriteLine("What is your most favorite sweet stuff?");
            Console.WriteLine("[1]Chocolate");
            Console.WriteLine("[2]Candy");
            Console.WriteLine("[3]Lollipop");
            Console.WriteLine("[4]Sugar");
            Console.WriteLine("[5]Honey");
            Console.WriteLine("[6]Cake");
            Console.Write("Enter your choice: ");
            string choice = Console.ReadLine();
 
            if (choice=="1")
            {
                Console.WriteLine("Chocolate, chocolate, chocolate!");
            }
            else if (choice == "2")
            {
                Console.WriteLine("Candy, candy, candy!");
            }
            else if (choice == "3")
            {
                Console.WriteLine("Lollipop, lollipop, lollipop!");
            }
            else if (choice == "4")
            {
                Console.WriteLine("Sugar, sugar, sugar!");
            }
            else if (choice == "5")
            {
                Console.WriteLine("Honey, honey, honey!");
            }
            else if (choice == "6")
            {
                Console.WriteLine("Cake, cake, cake!");
            }
            else
            {
                Console.WriteLine("Hmmm, I don't know what you're favorite is.");
            }
 
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

3.       Press F5 to run and see the results.

The if-else-if statement is used to add multiple condition to a piece of code. Sort of like a chained if-else statement Its syntax is: 



if(<condition>)
     //code
else if(<condition>)
     // any other code
else
     //this code will execute if there are no matching conditions


Condition – could be anything that would result in a true or false. If only runs the code inside its braces when the condition is true. If the condition fails or evaluates to false the code under the else will get executed.


Monday, August 29, 2011

C# 2 – Variables

C# 2 - Variables

1.  Create a new C# console project.
2.  Inside main() type the following code.

string myName = "Reymon Reyes"//Put your name within the two double quotes.
int myAge = 25;
float myHeight = 162.62F;
decimal myWeight = 132.28M;

Console.WriteLine("My profile");
Console.WriteLine("Name: {0}", myName);
Console.WriteLine("Age: {0}", myAge);
Console.WriteLine("Height: {0}", myHeight);
Console.WriteLine("Weight: {0}", myWeight);

Console.WriteLine("Press any key to continue.....");
Console.ReadLine();



3. Your Program.cs will now look like this.


4. Press F5 to run your code.





  • Syntax for declaring a variable.
    • <data type> <variable name>;
    • Eg: int myAge;
  • Data type can be any of the primitive types or a class.
  • Variable names cannot begin with a number or any special characters although an underscore is allowed.
  • Variable names are case sensitive which means myAge is different from MyAge.
  • Variable names cannot be any of the reserved keywords used within C#.
  • The declaration decimal myWeight = 132.28M; contains a suffix literal M to specify the value 132.28 as decimal type.