Code

Code Snippet - The Stopwatch Class as a Tool

There are some really cool tools available to help you test and improve your programs in the.Net Framework.

One of the most simple is the Stopwatch class, situated in the System.Diagnostics namespace. As the name suggests it allows you to create a stopwatch object, and use it to time processes, operations or any other part of your program.

Code Snippet - Smallest Number Divisible by 1-20

Code Snippet - Smallest Number Divisible by 1-20

This algorithm will calculate the lowest number that is evenly divisible by all of the numbers from 1-20. This is one of the Project Euler problems (ProjectEuler.net), which i have recently taken to playing with. I really enjoy thinking in a completely different way to my day job, and this is a great place to do that. Check it out below.

The code uses a do-while loop to increase the number to be tested from 1 to as high as is needed to allow the number to be divisable by 20 and all numbers below.

Code Snippet - Printing a Binary Triangle

This little code snippet comes directly from a 'code challenge' I completed on Programr.com and I found it really interesting so decided to put it up here.

The challenge was to create a binary triangle of a size determined by the user. So the program collected user input for the number of rows and then printed a binary triangle to the console output in the format given in the challenge spec below. The method shown here does not include gathering the user input.

Code Snippet - Count Instances of a Value in an Array

This method checks to see if the given value is within the array that it is passed. For every occurrence of the value in the array, the counter increases. The method will return an int with the count of the given value in an int[].

In each loop the next item in the array is compared against the value that is being searched for. if the value matches the search term, then the counter increases by one. The count is then returned at the end of the method.

Code Snippet - Minimum and Maximum Values of an Array

This method will take in an array of type double and return the minimum and maximum values. I wrote it to deal with temperature readings and to find the min/max for a week.

It will work with any number of array elements as it uses a foreach loop to iterate through the values and assign the smallest and largest values to a new array to be returned.

Code Snippet - Convert String to Title Case

This code snippet converts any string that it is passed into 'Title Case', ie capitalising the first letter of every word. Very useful for titles (unsurprisingly given the name!) It will work with strings that are all lower case, all upper case, or any other mix of case.

Note the using statement to bring in the System.Globalisation namespace, the program will not compile without this.

Code Snippet - Convert Days, Hours and Minutes into Seconds

The code below will convert the given number of days, hours, minutes and seconds into seconds.

The value used is an int, which is an alias of System.Int32.What this means is that the maximum number of seconds that can be stored is 2,147,483,647, which is the equivalent of ~68 years' worth of seconds. If a larger number is needed then the method must be changed to use a larger value type, eg long.