Dissecting the First C# Program You Created - 03
Enjoy!
“A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers.”
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] + " "); }}
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] + " "); }}
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.
{
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.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(); }
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(); } }