Unofficial Programming Thread IV

Post » Tue Jun 18, 2013 8:12 am

http://www.gnu.org/licenses/gpl-faq.html

That's what I use to help me figure things out. May not help, but it's a good reference on it all the same.
User avatar
stephanie eastwood
 
Posts: 3526
Joined: Thu Jun 08, 2006 1:25 pm

Post » Tue Jun 18, 2013 1:36 pm

I am a new programmer and am interested in learning C++. I am currently taking a class in Python and it's a bit too easy, so I'm looking for a challenge. I have a book on data structures in C++ but it assumes that you can already write in the language. Do any of you have recommendations or books or tutorials on basic C++? Thanks!
User avatar
Lalla Vu
 
Posts: 3411
Joined: Wed Jul 19, 2006 9:40 am

Post » Tue Jun 18, 2013 11:54 am

This should help get you started http://www.cplusplus.com/doc/tutorial/
User avatar
BlackaneseB
 
Posts: 3431
Joined: Sat Sep 23, 2006 1:21 am

Post » Tue Jun 18, 2013 12:02 am

You want it to be more difficult to use? Do all of your programming wearing mittens. That should do it. /wink.png' class='bbc_emoticon' alt=';)' />
User avatar
Rozlyn Robinson
 
Posts: 3528
Joined: Wed Jun 21, 2006 1:25 am

Post » Tue Jun 18, 2013 11:42 am

Nah. Use http://xkcd.com/378/ /tongue.png' class='bbc_emoticon' alt=':P' />
User avatar
Hairul Hafis
 
Posts: 3516
Joined: Mon Oct 29, 2007 12:22 am

Post » Tue Jun 18, 2013 8:39 am

Not done any coding in nearly 6 months since I moved department.
User avatar
natalie mccormick
 
Posts: 3415
Joined: Fri Aug 18, 2006 8:36 am

Post » Tue Jun 18, 2013 2:10 am

Have not done any coding in awhile but now have a couple of small projects. I could do it in either Python or C# with the same amount of effort. At the moment I would target only a Windows machine and can use cx_freeze ont he Python script to make an easy to run executable, or compile for .NET 3.0 with C# to avoid any unneeded installations on the target machine. If this is the case which should I choose? Overhead isn't nessesarily a factor since overall the program will only run a short period of time but I am unsure which way to go with this. Suggestions?
User avatar
Tracey Duncan
 
Posts: 3299
Joined: Wed Apr 18, 2007 9:32 am

Post » Tue Jun 18, 2013 3:55 pm

IronPython? /tongue.png' class='bbc_emoticon' alt=':tongue:' /> In situations where implementation complexity and maintenance complexity are pretty much a wash the next thing I usually look at is deployment complexity. If all things except deployment complexity are truly equal then that would be the decider for me. Another thing to keep in mind is whether or not you might want to re-use any of that code, and if so, are you more likely to re-use it in a .NET project or a python project?
User avatar
Cccurly
 
Posts: 3381
Joined: Mon Apr 09, 2007 8:18 pm

Post » Tue Jun 18, 2013 5:39 am

I should have expected that from someone. No not IronPython, but of the two should I bother to favor one over the other? In all reality it is just going to edit a couple of text files and be done.
User avatar
Cheville Thompson
 
Posts: 3404
Joined: Sun Mar 25, 2007 2:33 pm

Post » Tue Jun 18, 2013 9:53 am

Like I said, if one is easier to deploy (and has fewer "not-already-satisfied" dependencies) than the other in your environment I'd go with that one.
User avatar
Angela Woods
 
Posts: 3336
Joined: Fri Feb 09, 2007 2:15 pm

Post » Tue Jun 18, 2013 9:56 am

I think I want to go with Python since it appears to be quicker and easier to write this but I have one issue I cannot resolve. I am tryign to avoid a command window popping up when I send a system command to Windows.
User avatar
chinadoll
 
Posts: 3401
Joined: Tue Aug 22, 2006 5:09 am

Post » Tue Jun 18, 2013 12:07 pm

Are you talking about the "cmd.exe" command-shell window that pops up when you execute a command-line statement or some other kind of window (like UAC)?
User avatar
Cagla Cali
 
Posts: 3431
Joined: Tue Apr 10, 2007 8:36 am

Post » Tue Jun 18, 2013 4:27 pm

I was talking about cmd.exe but I seem to have found a work around. Python act strange sometimes which I don't understand but I guess I should get used to it since C++ is beyondmy abilities and I wanted to do some cross platform work.
User avatar
-__^
 
Posts: 3420
Joined: Mon Nov 20, 2006 4:48 pm

Post » Tue Jun 18, 2013 12:33 am

In case your workaround falls through: one way to do that is to use Wsh to execute the shell command for you silently. If you're ok with executing your command from a VBS file you can do:
WshShell.Run("[ your command line ]", 0)
If you need to pass parameters you can pass them to the VBS and then build the command line string from within the script.
User avatar
Rudi Carter
 
Posts: 3365
Joined: Fri Sep 01, 2006 11:09 pm

Post » Tue Jun 18, 2013 3:22 pm

A while ago, I was doing some homework for my Java class. I was supposed to assign a random value between 0-9 (inclusive) to two separate integers, and the resulting integer values of the two variables could not be equal. Embedded in the question was the following hint: "Generate a value for the first variable, then continually generate values for the second variable until it is not equal to the first variable."

Well I have to say, that single piece of bad advice made me want to throw the entire book in the trash. Is there a name for such a bad code design? For the curious, I ended up writing my code like this:
int numFirst, numSecond;Random generator = new Random();numFirst = generator.nextInt(10);numSecond = generator.nextInt(9);if( numSecond >= numFirst ) numSecond++;
... so there's always an equal chance for numSecond to generate every value from 0-9 except the one picked for numFirst. But it bothers me what awful principles are being taught in these classes. >.<
User avatar
Dalia
 
Posts: 3488
Joined: Mon Oct 23, 2006 12:29 pm

Post » Tue Jun 18, 2013 1:58 am

I love bit hacks. They may not be pretty and they don't always make sense, but they're damn interesting.

Spoiler

public static int countBits(int i){    i -= ( (i >>> 1) & 0x55555555 );    i  = (i & 0x33333333) + ( (i >>> 2) & 0x33333333 );    return (((i + (i >>> 4)) & 0x0F0F0F0F) * 0x01010101) >>> 24;}
User avatar
..xX Vin Xx..
 
Posts: 3531
Joined: Sun Jun 18, 2006 6:33 pm

Post » Tue Jun 18, 2013 11:55 am

Bit hacks are quite fun. I used them frequently in the drawing loops of a Wolfenstein clone I wrote once.

On a similar note, did you know that the result of ANDing any power of 2 with another number is the same as the modulus of the first number by the second? Ex:

256 & 36 = 256 % 36
32 & 13 = 32 % 13
128 & 45 = 128 % 45

EDIT: Or maybe I have that backwards... still cool, though.
User avatar
Zualett
 
Posts: 3567
Joined: Mon Aug 20, 2007 6:36 pm

Post » Tue Jun 18, 2013 4:59 am

I think you mean this?
a & (b-1);
Where a is some integer and b is a power of 2.
User avatar
Hearts
 
Posts: 3306
Joined: Sat Oct 20, 2007 1:26 am

Post » Tue Jun 18, 2013 2:17 pm


Ew. That's giving me some disturbing flashbacks of when I used to try to figure out what was going on in some of the http://www.ioccc.org/ entries!
User avatar
Bad News Rogers
 
Posts: 3356
Joined: Fri Sep 08, 2006 8:37 am

Post » Tue Jun 18, 2013 5:44 am

http://www.ioccc.org/2012/blakely/blakely.c
User avatar
Julie Serebrekoff
 
Posts: 3359
Joined: Sun Dec 24, 2006 4:41 am

Post » Tue Jun 18, 2013 9:07 am

I have to use python for my new job and as someone coming from C++ it seems very messy compared to what I normally right.

I've been doing some research and tutorials on it, but could someone give me a run-down on the advantages of python for someone who is familiar with C++/Java.....so far all I see is the ability to run from source and the way it packages up for extending code.
User avatar
Miss K
 
Posts: 3458
Joined: Sat Jan 20, 2007 2:33 pm

Post » Tue Jun 18, 2013 2:12 am

That is probably the biggest advantage, no compile time. It appears to me, and I have only done very basic C++ many years ago, that Python is easier to do cross platform work with but I am not entirely sure on this. Python is also pretty simple for nyone to pick up and work with which is another advantage and the script sizes stay small if that is important to you.
User avatar
Darlene Delk
 
Posts: 3413
Joined: Mon Aug 27, 2007 3:48 am

Post » Tue Jun 18, 2013 8:09 am


I had to google around to see what you were talking about. Sounds like a lot of work for little reward.


It sounds like they wanted you to do a while-loop and repeatedly check to see if the second number is the same as the first. Of course when dealing with random numbers there's a very slight chance that this loop could go on for eternity, locking up your program. I say you made the right call.

I ran into a similar problem when generating random levels for a game I was working on. Sometimes I'd want to place an object on the map but I didn't want it to be near some other object. There's various solutions to such a problem but I went with one where it would try a given amount of times to put the object on a legal part of the map and if it ran out of tries the object just wouldn't be added. It can't work for anything that you absolutely must have but it is a better solution than a while-loop.
User avatar
Taylrea Teodor
 
Posts: 3378
Joined: Sat Nov 18, 2006 12:20 am

Post » Tue Jun 18, 2013 12:00 pm

Yes, that's it. I used that technique a lot in a texture-mapping routine I wrote once. It only worked for texture sizes that were a power of 2, but hey, what'll you do.

By the way, I stumbled across a cool text file on a really old website once, and seeing as it's just been rotting with a bunch of other rubbish on my flash drive, I've decided to share it with the rest of y'all:
Spoiler

'Before/after comparison of programmer thinking patterns."Before" represents the mind of an underdeveloped programmer."After" represents the mind of a mature software engineer.Before:x = y % 32;x = y * 8;x = y / w + z / w;if( a==b &&c==d &&e==f ) {...}if( (x &1) || (x &4) ) {...}if( x&--#62;=0 &&x&--#60;8 &&y&--#62;=0 &&y&--#60;8 ) {...}if( (x==1) || (x==2) ||(x==4) || (x==8) || ... )if( (x==2) || (x==3) || (x==5) ||(x==7) || (x==11) || (x==13) ||(x==17) || (x==19) ) {...}#define abs(x) (((x)&--#62;0)?(x):-(x))int a[3][3][3];int b[3][3][3];...for(i=0;i&--#60;3;i++)for(j=0;j&--#60;3;j++)for(k=0;k&--#60;3;k++)b[i][j][k] = a[i][j][k];for(i=0;i&--#60;3;i++)for(j=0;j&--#60;3;j++)for(k=0;k&--#60;3;k++)a[i][j][k] = 0;for(x=0;x&--#60;100;x++) {printf("%d\n",(int)(sqrt(x)));}unsigned int x, y, a;...a = (x + y) &--#62;&--#62;1;/* overflow fixup */if( a &--#60;x &&a &--#60;y ) a += 0x8000000;c:\&--#62;tc myprog.cuser% cc myprog.cUse the quicksort algorithm.Use the Bresenham line algorithm.Look through school notes for ideas.Ignore suggestions from others.Code, code, code, code ...After:x = y &31;x = y &--#60;&--#60;3;x = (y + z) / w;if( ((a-B)|(c-d)|(e-f))==0 ) {...}if( x & 5 ) {...}if( ((unsigned)(x|y))&--#60;8 ) {...}if( x&(x-1)==0 &&x!=0 )if( (1&--#60;&--#60;x) & ((1&--#60;&--#60;2)|(1&--#60;&--#60;3)|(1&--#60;&--#60;5)|(1&--#60;&--#60;7) \|(1&--#60;&--#60;11)|(1&--#60;&--#60;13)|(1&--#60;&--#60;17)|(1&--#60;&--#60;19)) ) {...}static long abs(long x) {long y;y = x&--#62;&--#62;31; /* Not portable */return (x^y)-y;}typedef struct {int element[3][3][3];} Three3DType;Three3DType a,b;...b = a;memset(a,0,sizeof(a));for(tx=1,sx=0,x=0;x&--#60;100;x++) {if( tx&--#60;=x ) {tx+=2*sx+3;sx++;}printf("%d\n",sx);}unsigned int x, y, a;...a = (x & y) + ((x ^ y)&--#62;&--#62;1);c:\&--#62;wpp386 /6r/otexan myprog.cuser% gcc -O3 myprog.cUse radix, intro or heap(!) sort.Use fixed point DDA line algorithm.Get examples from USENET/WEB/FTPGet suggestions but be skeptical.Think, code, think, code ...

There are some good tips in there.

Also something that only a few programmers seem to realize but is actually quite obvious is that you can toggle boolean values in one line like this:
booleanVar = (booleanVar==false);
Because booleanVar==false will return true when booleanVar is false, booleanVar will be set to true when booleanVar is false and vice versa.

This code won't work in crankier languages like Java, but plain ol' C will handle something like this just fine.
User avatar
Kortniie Dumont
 
Posts: 3428
Joined: Wed Jan 10, 2007 7:50 pm

Post » Tue Jun 18, 2013 12:32 am

public static boolean toggle(boolean a){    a = (a == false);    return a;}
Works fine in Java /tongue.png' class='bbc_emoticon' alt=':P' />

(I realize that could (and should) easily be shortened to one line)
User avatar
Emmie Cate
 
Posts: 3372
Joined: Sun Mar 11, 2007 12:01 am

PreviousNext

Return to Othor Games