Code Snippets
Dauth 18 Jun 2009
We have a number of skilled programmers here, just post your little codes which don't do much for others to start learning how to code from examples.
One of my own, simple matlab code to go from seconds to hh:mm:ss, this program works with a vector of values and maybe a matrix. I've not bothered with making a read in fuction since matlab codes work from plain text files but one could be added very easily.
This code can be expanded for any time unit.
Edit: Bug spotted by JNengland77, stupid mornings hurting my coding skills
Post your own and it might help someone.
Edited by Dauth, 18 June 2009 - 23:49.
One of my own, simple matlab code to go from seconds to hh:mm:ss, this program works with a vector of values and maybe a matrix. I've not bothered with making a read in fuction since matlab codes work from plain text files but one could be added very easily.
Quote
t = 3661;
%Choose your t values, use [value1, value2, value3, ..., valueN] for a vector of values
a = rem(t,3600);
%Gets remainder when we remove hours
hh = (t-a)/3600;
%Gives hh in hours
t = t - hh*3600;
%Removes hours from t
a = rem(t,60);
%Gets remainder when we remove minutes
mm = (t-a)/60;
%Gives mm in mins
ss = t - mm*60
%Finds the seconds
disp(hh)
disp(mm)
disp(ss)
%Outputs the hh:mm:ss as a series of three values over each other.
%Choose your t values, use [value1, value2, value3, ..., valueN] for a vector of values
a = rem(t,3600);
%Gets remainder when we remove hours
hh = (t-a)/3600;
%Gives hh in hours
t = t - hh*3600;
%Removes hours from t
a = rem(t,60);
%Gets remainder when we remove minutes
mm = (t-a)/60;
%Gives mm in mins
ss = t - mm*60
%Finds the seconds
disp(hh)
disp(mm)
disp(ss)
%Outputs the hh:mm:ss as a series of three values over each other.
This code can be expanded for any time unit.
Edit: Bug spotted by JNengland77, stupid mornings hurting my coding skills
Post your own and it might help someone.
Edited by Dauth, 18 June 2009 - 23:49.
jnengland77 18 Jun 2009
Dauth thought I should post this bit of code inspired by this xkcd comic.
C/C++ code. checkAsleep( ) should be something strange, but haven't thought what it would be yet.
Edited by jnengland77, 19 June 2009 - 00:21.
long sheepCount = 1; int notAsleep = 1; while ( notAsleep ) { printf( "%ld", sheepCount++ ); notAlseep = checkAsleep( sheepCount ); sleep( 1 ); }
C/C++ code. checkAsleep( ) should be something strange, but haven't thought what it would be yet.
Edited by jnengland77, 19 June 2009 - 00:21.
Erik 19 Jun 2009
Global Sheep repeat Sheep=Sheep+1 print "Sheep count "+Sheep until CheckAsleep(Sheep)=1
same in basic
Edited by Erik, 19 June 2009 - 08:06.