Manage and share your stuff. Sign up for FREE!
www.meestuf.com
-----------------------------------------------------------------------------------------
C# 1 – Hello World
C# 2 – Variables
C# 3 - Variable Scope
C# 4 - What If?
C# 5 - What If? Or, Else ?
C# 6 - What If? Or, Else If....?
C# 7 - Switch statement
C# 8 - For loop statement
C# 9 - while-loop statement
C# 10 - do-while-loop statement
C# 11 - Arrays
C# 12 - Multidimensional Arrays - Multiplication Table
C# 13 - Collections - Todo List
C# 14 - Find the largest value in an array
C# 15 - Selection sort
C# 16 - Insertion Sort
C# 17 - Bubble Sort
-----------------------------------------------------------------------------------------
Manage and share your stuff. Sign up for FREE!
www.meestuf.com
Showing posts with label Console. Show all posts
Showing posts with label Console. Show all posts
Tuesday, January 8, 2013
Wednesday, July 4, 2012
C# 17 - Bubble Sort
Implementing a sort using insertion 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 = { 2, 3, 1, 9, 4, 7, 5, 8, 6, 10, 20, 12, 18, 11, 15, 16 }; Console.Write("Before sort: "); PrintArray(array); Console.WriteLine(); BubbleSort(array); Console.Write("After sort: "); PrintArray(array); Console.ReadLine();}
private static void BubbleSort(int[] array){ int temp; for (int i = array.Length; i >= 0 ; i--) { for (int x = 0; x < array.Length - 1; x++) { if (array[x] > array[x + 1]) { temp = array[x]; array[x] = array[x + 1]; array[x + 1] = temp; } } }}
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. Enjoy!
References:
http://www.sorting-algorithms.com/bubble-sort
http://en.wikipedia.org/wiki/Bubble_sort
http://www.algolist.net/Algorithms/Sorting/Bubble_sort
Thursday, June 28, 2012
C# 16 - Insertion Sort
Implementing a sort using insertion 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 = { 2, 3, 1, 9, 4, 7 };
Console.Write("Before sort: ");
PrintArray(array);
Console.WriteLine();
InsertionSort(array);
Console.Write("After sort: ");
PrintArray(array);
Console.ReadLine();
}
private static void InsertionSort(int[] array)
{
/*
for i = 2:n,
for (k = i; k > 1 and a[k] < a[k-1]; k--)
swap a[k,k-1]
→ invariant: a[1..i] is sorted
end
*/
for (int i = 0; i < array.Length; i++)
{
for (int x = i; x < 0; x--)
{
if (array[x] < array[x - 1])
{
int temp = array[x - 1];
array[x - 1] = array[x];
array[x] = temp;
}
}
}
}
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.
References:
http://en.wikipedia.org/wiki/Insertion_sort
http://www.sorting-algorithms.com/insertion-sort
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 = { 2, 3, 1, 9, 4, 7 };
Console.Write("Before sort: ");
PrintArray(array);
Console.WriteLine();
InsertionSort(array);
Console.Write("After sort: ");
PrintArray(array);
Console.ReadLine();
}
private static void InsertionSort(int[] array)
{
/*
for i = 2:n,
for (k = i; k > 1 and a[k] < a[k-1]; k--)
swap a[k,k-1]
→ invariant: a[1..i] is sorted
end
*/
for (int i = 0; i < array.Length; i++)
{
for (int x = i; x < 0; x--)
{
if (array[x] < array[x - 1])
{
int temp = array[x - 1];
array[x - 1] = array[x];
array[x] = temp;
}
}
}
}
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.
References:
http://en.wikipedia.org/wiki/Insertion_sort
http://www.sorting-algorithms.com/insertion-sort
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.
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
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
Wednesday, June 27, 2012
C# 13 - Find the smallest value in an array
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 };
PrintArray(array);
Console.WriteLine("Smallest Value: " + GetSmallestValue(array));
Console.ReadLine();
}
private static int GetSmallestValue(int[] array)
{
int smallest = array[0];
for (int i = 0; i < array.Length; i++)
{
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
private static void PrintArray(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.WriteLine();
}
3. Press F5 to run and see the results.
4. Thank you.
Labels:
.Net,
Arrays,
C#,
C# statements,
C# Tutorial,
Console,
Free Tutorials,
Tutorial
Thursday, November 24, 2011
C# 13 - Collections - Todo List
1. Create a new console project.
2. Edit the Program.cs to look like as shown below.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TodoList { class Program { private static List<string> todolist = new List<string>(); static void Main(string[] args) { string menuOption; bool exitApplication = false; do { Console.WriteLine("==========================="); Console.WriteLine("Welcome to your Todo List."); Console.WriteLine("==========================="); Console.WriteLine("[1] Show Todo List"); Console.WriteLine("[2] Add a new Todo"); Console.WriteLine("[3] Delete a Todo"); Console.WriteLine("[4] Exit"); Console.Write("What do you want to do? "); menuOption = Console.ReadLine(); switch(menuOption) { case "1": Console.WriteLine("---------------------------"); ShowTodoList(); Console.WriteLine("---------------------------"); break; case "2": Console.WriteLine("---------------------------"); AddNewTodo(); Console.WriteLine("---------------------------"); break; case "3": Console.WriteLine("---------------------------"); DeleteATodo(); Console.WriteLine("---------------------------"); break; case "4": Console.WriteLine("---------------------------"); Console.WriteLine("Exit"); Console.WriteLine("---------------------------"); exitApplication = true; break; default: Console.WriteLine("Invalid option"); break; } } while (!exitApplication); }
private static void DeleteATodo() { string todoNumberInput; int todoNumber; ShowTodoList(); Console.Write("What Todo do you want to delete? "); todoNumberInput = Console.ReadLine(); if (Int32.TryParse(todoNumberInput, out todoNumber)) { if (todoNumber > todolist.Count) Console.WriteLine("Todo does not exist."); else todolist.RemoveAt(todoNumber); } else { Console.WriteLine("Input must be a number"); } }
private static void AddNewTodo() { string newTodo; Console.Write("What will you be doing? "); newTodo = Console.ReadLine(); todolist.Add(newTodo); } private static void ShowTodoList() { int todoNumber = 1; if (todolist.Count == 0) { Console.WriteLine("Your Todo list is empty"); } else { foreach (string todo in todolist) { Console.WriteLine("[" + todoNumber + "] " + todo); todoNumber++; } } } } }
3. Press F5 to run and see the results.
4. Thank you.
Learn more about Collections.
Friday, October 28, 2011
C# 12 - Multidimensional Arrays - Multiplication Table
Manage and share your stuff. Sign up now for FREE!
meestuf.com
-----------------------------------------------------------------------------------------------------
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
meestuf.com
-----------------------------------------------------------------------------------------------------
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[,] multiplicationTable = new int[11,11];
for (int i = 1; i < 11; i++)
for (int x = 1; x < 11; x++)
multiplicationTable[i, x] = i * x;
Console.Write("x".PadRight(3) + "|");
for (int i = 1; i < 11; i++)
{
Console.Write(i.ToString().PadRight(3) + "|");
}
Console.Write("\n");
Console.WriteLine("--------------------------------------------");
for (int i = 1; i < 11; i++)
{
Console.Write(i.ToString().PadRight(3) + "|");
for (int x = 1; x < 11; x++)
{
Console.Write(multiplicationTable[i, x].ToString().PadRight(3) + "|");
}
Console.Write("\n");
Console.WriteLine("--------------------------------------------");
}
Console.ReadLine();
}
3. Press F5 to run and see the results.
4. Thank you.
Rectangular array is another type of array that allows
you to define a table like array that has a row and column definition.
Syntax:
type[,] = new type[row,column];
type – can be any data type
row – is the number of rows for your table
column – is the number of columns for your table
e.g.
int[,]
mytable = new int[10, 5];
-----------------------------------------------------------------------------------------------------
Manage and share your stuff. Sign up now for FREE!
meestuf.com
-----------------------------------------------------------------------------------------------------
Manage and share your stuff. Sign up now for FREE!
meestuf.com
Thursday, October 13, 2011
C# 11 - Arrays
An array of sorts.
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
3. Press F5 to run and see the results.
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) { //declaring and initializing an array string[] theAvengers = new string[5]; theAvengers[0] = "Iron Man"; theAvengers[1] = "Thor"; theAvengers[2] = "Captain America"; theAvengers[3] = "Hawkeye"; theAvengers[4] = "Hulk"; Console.WriteLine("The Avengers!"); Console.WriteLine("-------------------------"); //loop through an array to access or modify its contents for (int i = 0; i < 5; i++) { Console.WriteLine(theAvengers[i]); } //another way of declaring and initializing an array //also another way is
//string[] uncannyXMen = new string[] { "Wolverine", "Prof. X", "Cyclops", "Storm", "Beast" };string[] uncannyXMen = { "Wolverine", "Prof. X", "Cyclops", "Storm", "Beast" }; Console.WriteLine("\nThe X-Men!"); Console.WriteLine("-------------------------");
//loop through an array to access in contents for (int i = 0; i < uncannyXMen.Length; i++) { Console.WriteLine(uncannyXMen[i]); }
Console.WriteLine("Press any key to continue..."); Console.ReadLine(); }
4. Thank you.
Declaring an array allows you to store multiple items of a specific type. It syntax is:
type[] arrayName;
To learn more about arrays check these resources.
Friday, October 7, 2011
C# 10 - do-while-loop statement
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
static void Main(string[] args)
{int counter = 0;
while (counter < 10)
{Console.WriteLine("Hello World from a while loop.");
counter++; }Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}3. Press F5 to run and see the results.5. Thank you.
The do- while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. It executes code at least once. Its syntax is:
do
{
//code to execute
}
while(<condition>);
Labels:
.Net,
C#,
C# statements,
C# Tutorial,
Console,
Tutorial
Thursday, October 6, 2011
C# 9 - while-loop statement
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
3. Press F5 to run and see the results.
4. Thank you.
The while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:
while(<condition>)
{
//code to execute
}
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
class Program { static void Main(string[] args) { int counter = 0; while (counter < 10) { Console.WriteLine("Hello World from a while loop."); counter++; } Console.WriteLine("Press any key to continue..."); Console.ReadLine(); } }
3. Press F5 to run and see the results.
4. Thank you.
The while-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:
while(<condition>)
{
//code to execute
}
Thursday, September 29, 2011
C# 8 - For loop statement
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
3. Press F5 to run and see the results.

2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
static void Main(string[] args) { for (int i = 0; i < 10; i++) { Console.WriteLine("Hello World!"); } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }

4. Thank you.
The for-loop statement is used perform a piece of code multiple times as long as the condition set is evaluated to true. Its syntax is:
for(<initialization>;<condition>;<incrementation/decrementation>)
{
//code to execute
}
Labels:
.Net,
C#,
C# statements,
C# Tutorial,
Console,
Tutorial
Wednesday, September 21, 2011
C# 7 - Switch statement
1. Create a new console project.
2. Edit the Program.cs file as shown below.
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(); switch (choice) { case "1": Console.WriteLine("Chocolate, chocolate, chocolate!"); break; case "2": Console.WriteLine("Candy, candy, candy!"); break; case "3": Console.WriteLine("Lollipop, lollipop, lollipop!"); break; case "4": Console.WriteLine("Sugar, sugar, sugar!"); break; case "5": Console.WriteLine("Honey, honey, honey!"); break; case "6": Console.WriteLine("Cake, cake, cake!"); break; default: Console.WriteLine("Hmmm, I don't know what you're favorite is."); break; } Console.WriteLine("Press any key to continue....."); Console.ReadLine(); }3. Press F5 to run and see the results.
4. Thank you.
The switch statement is used to add condition to a piece of code. Its syntax is:
switch(<expression>)
case <expression result>:
//code to execute
break;
case <expression result>:
//code to execute
Break;
.
.
.
default:
//code to execute if there are no matching case
break;
expression – could be anything that would result in a value that would get evaluated in each of the cases.
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.
if(<condition>)
//code
else if(<condition>)
// any other code
else
//this code will execute if there are no matching conditions
Labels:
.Net,
C#,
C# data types,
C# statements,
Console,
Tutorial
Wednesday, September 14, 2011
C# 5 - What If? Or, Else ?
Manage and share your stuff for FREE!
www.meestuf.com
-----------------------------------------------------------------------------------------------------
1. Create a new console project.
2. Edit the Program.cs file as shown below.
3. Press F5 to run and see the results.
The if-else statement is used to add condition to a piece of code. Its syntax is:
if()
//code
else
www.meestuf.com
-----------------------------------------------------------------------------------------------------
1. Create a new console project.
2. Edit the Program.cs file as shown below.
static void Main(string[] args)
{
string favoritePet = "Dog";
Console.WriteLine("My favorite pet is a " + favoritePet);
if (favoritePet == "Cat")
{
Console.WriteLine("Then it says: Meow!");
}
else
{
Console.WriteLine("Then it says: Woof!");
}
Console.WriteLine("Press any key to continue....");
Console.ReadLine();
}
4. Thank you.
The if-else statement is used to add condition to a piece of code. Its syntax is:
if(
//code
else
// any other code
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.
-----------------------------------------------------------------------------------------------------
Manage and share your stuff for FREE!
www.meestuf.com
-----------------------------------------------------------------------------------------------------
Manage and share your stuff for FREE!
www.meestuf.com
Subscribe to:
Posts (Atom)












