Jump to content


Code Snippets


3 replies to this topic

#1 Dauth

    <Custom title available>

  • Gold Member
  • 11193 posts

Posted 18 June 2009 - 23:41

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.

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.

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.


#2 jnengland77

    Amateur

  • Gold Member
  • 138 posts

Posted 18 June 2009 - 23:53

Dauth thought I should post this bit of code inspired by this xkcd comic.

long sheepCount = 1;
int notAsleep = 1;
while &#40; notAsleep &#41;
{
printf&#40; &#34;%ld&#34;, sheepCount++ &#41;;
notAlseep = checkAsleep&#40; sheepCount &#41;;
sleep&#40; 1 &#41;;
}


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.


#3 Libains

    Light up life.

  • Gold Member
  • 4950 posts

Posted 18 June 2009 - 23:57

EPIC WIN. Very nicely done jnengland :P
For there can be no death without life.

#4 Erik

    Feels green.

  • Project Leader
  • 978 posts
  • Projects: ZH: Frontlines

Posted 19 June 2009 - 08:04

Global Sheep
repeat
  Sheep=Sheep+1
  print &#34;Sheep count &#34;+Sheep
until CheckAsleep&#40;Sheep&#41;=1


same in basic :P

Edited by Erik, 19 June 2009 - 08:06.




5 user(s) are reading this topic

0 members, 5 guests, 0 anonymous users