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.
You might think it odd that I did not use an if-else block, but not using one is important because It allows the min and max to be the same value which would be required if all of the array elements are the same, with an if-else statement the second if statement would be passed over if the first evaluates to true.
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.
This method obviously uses ints, but it would be jsut as easy to use doubles or any other value type including chars in a char array.
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.
The for loop iterates through each number up to 20, and the if statement checks to see if the remainder is 0 or not. if it is not, then the number being tested cannot be right, so the for loop breaks and the do-while loop iterates again, having incremented the counter.
After a bit of thinking followed by some optimisation and refactoring, the code below is a considerably faster way to compute the result.
The method above takes about 10seconds to complete, depending on your system; the new method, below, takes about 200ms on the systems I have tested it on.
The counter int q jumps up 20 with each iteration as we know that the result has to be divisible by 20. The for loop starts counting backwards, counting from 20-1, but stops at 10, this is because if a number is divisible by the numbers 11-20, it is divisible by the numbers 1-10, so there is no need to test them. Also, counting backwards means that the higher numbers are reached first, and these are the ones that are harder to divide evenly by.
I'll keep looking for further optimisations, I'm sure there are some somewhere...
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.
It might sound lke a waste of time to see how fast a small part of your program is, but making all of the smaller parts work better can have a great impact on the overall speed of the program so don't discount it as a method of improving your code.
The code to instantiate the stopwatch is just the same as most other objects, and to start and stop the stopwatch is also very straight forward:
Once you have used the stopwatch to time the running of your code, you can use the Elapsed property to access the time that has elapsed while the stopwatch has been running. It is displayed in the format of HH:MM:SS.0000000. (the first three 0’s represent milliseconds) The time stored in the stopwatch can also be accessed using the ElapsedMilliseconds property, which displays only the milliseconds that have elapsed.
The stopwatch can be read without being stopped, so you can access the Elapsed property and leave the stopwatch running, and then check the elapsed time again later to see the total time. If you want to use the intermittent timings later you can use a TimeSpan object to store the time and perform operations on it such as comparing two times.
Alternatively you can stop the stopwatch using Stop(), and then start the stopwatch again by calling Start(). This will cause the stopwatch to continue where it left off when it was stopped. You can clear the contents of the stopwatch with the Reset() method, or to clear the stopwatch and start it again with only one method call you can call Restart(). This is preferable to using a new stopwatch as you then don’t need to create another stopwatch object.
Using the stopwatch has been really useful for assessing the speed of my programs, or algorithms. It has often made me refactor and optimise my code to make it run faster, and therefore make the whole application more snappy and responsive.
A prime example of this is in the Smallest Number Divisible by 1 - 20 where my original program took about 10 seconds to find the answer and I thought there must be a faster way. So using the timers to test various theories i came up with an altered algorithm that ran significantly faster than the original.
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.
If user gives input 5 then the binary triangle should be as is shown at the top of the code below:
The first loop counts the number of lines that are being printed, up to the value of the input.
The second loop uses the count from the first loop to dictate the number of writes required for that given line. ie, as the counter for the first loop increases, the number of characters printed on each line increases proportionally.
The if-else block prints 1 if the last int was 0, and 0 if the last int was 1, then sets the lastInt value to the appropriate value for the next loop.
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.
After I had written this method, I realised that it would be cleaner if it accepted an int array of the times, I have also implemented this below:
This change tidies up the number of parameters significantly, and works in the exact same way.
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.
As an example, the input the quick brown fox jumped over the lazy dog will be converted to The Quick Brown Fox Jumped Over The Lazy Dog