Computer Science Canada

Tidbits and Tricks for programming

Author:  AsianSensation [ Fri Nov 21, 2003 5:29 pm ]
Post subject:  Tidbits and Tricks for programming

Here is just a collection of tricks and shortcuts you can use when programming, I knew it helped me alot. Anyways, anyone else with anything to add, please share too.

Declaring Picture Files

I constantly see alot of people declaring picture files like this:
code:
var picID: array 1..20 of int

picID (1) := Pic.FileNew ("Apple.bmp")
picID (2) := Pic.FileNew ("Orange.bmp")
picID (3) := Pic.FileNew ("Strawberry.bmp")
picID (4) := Pic.FileNew ("Starfruit.bmp")
.
.
.
picID (5) := Pic.FileNew ("Avocado.bmp")

this is really a waste of time, I mean who cares if your name your picture strawberry? It's the program that they care about, so next time you need to import tons of pictures do it like this:

first, name all your pictures "pic1.bmp", "pic2.bmp", "pic3.bmp", "pic4.bmp" ... "pic35.bmp"
then declare an array of variables:
code:
var picID :array 1..35 of int

and run a for loop like this:
code:
for i : 1 .. 35
   picID := Pic.FileNew ("pic" + intstr(i) + ".bmp")
end for


this cuts all your picture declaring into 4 lines total. Just make sure you have a textfile or some sort to tell you what each picture is.

Author:  rizzix [ Fri Nov 21, 2003 6:14 pm ]
Post subject: 

Emulating two dimensional dynamic arrays in Turing
(there are no two dimensional dynamic arrays in turing)

assumptions:
the array begins with 0 for row and column index
the two dimensional array we are to emulate is not of an irregular shape (this is possible in languages like Java) but of a rectangular (matrix) shape.

solution:
let y be the index for the row count
let x be the index for the column count

then the location of [y][x] in a one dimensional array:
y * NumberOfColumns + x

what does this mean?

var arr : flexible array 0 .. 0 of int
new arr, 99

- notice the total # of elements (or area) in the 2 dimensional array is 100 = the # elements in the one dimensional array

now to get the [2][5] element from this array

x := 5
y := 2

value := arr(y * 10 + x)

Author:  Andy [ Fri Nov 21, 2003 7:06 pm ]
Post subject: 

u lucky turing kids with lower boundry of arrays...

Author:  Dan [ Sat Nov 22, 2003 1:27 am ]
Post subject: 

dodge_tomahawk wrote:
u lucky turing kids with lower boundry of arrays...


why whould you need that? i think you are refuring to how C and othe languges only have [num] for array. but if you think about it you realy dont need a starting and end point for them, since realy they are just the same but with difrent numbers for the index.

Author:  Mazer [ Sat Nov 22, 2003 8:47 am ]
Post subject: 

i thought dodge meant to say upper boundary of arrays. like, in turing you could say
code:

put upper (myArray)

to see how high the array went. but in c++ you need a variable to keep track of the upper boundary, or just remember it yourself. (unless there's some other way that i was not made aware of)

Author:  rizzix [ Sat Nov 22, 2003 2:50 pm ]
Post subject: 

actually there is another way Smile

sizeof(arr) / sizeof(arr[0])

that should give u the # of elements

Author:  Dan [ Sun Nov 23, 2003 2:41 pm ]
Post subject: 

oOo, i thougth he was talking about the rang thing turing has.

java has the thing to tell how big an array is too.

Author:  Andy [ Sun Nov 23, 2003 3:41 pm ]
Post subject: 

in turing, you can just pass an array through a function and use upper to how big it is, in c, you have to shove the size in as well, kinda annoying

Author:  rizzix [ Sun Nov 23, 2003 7:22 pm ]
Post subject: 

well u'll love java then Smile

suppose u have an array of type x called element[10] <-- yea 10 elements

in java all u need to do is element.length yep thats right an array is an object in java, just ask it its size!

Author:  Tony [ Sun Nov 23, 2003 7:32 pm ]
Post subject: 

everything is an object in java Very Happy java kicks ass 8)

and methods such as .toString make debugging really easy Very Happy

Author:  AsianSensation [ Sun Nov 30, 2003 9:27 pm ]
Post subject: 

OK, this has been bugging me for too much.

Here is the best programming trick you will ever need when programming in turing.

Press either F8, F9, or F10

Author:  Andy [ Sun Nov 30, 2003 9:44 pm ]
Post subject: 

F8 is awsome it tells you what to type

Author:  Cervantes [ Fri Jan 23, 2004 2:53 pm ]
Post subject: 

ooh cool never new that one Razz

Author:  AsianSensation [ Thu Mar 18, 2004 8:36 pm ]
Post subject: 

Another programming trick. Don't use processes unless you are really really good and can handle it. So far, only Mazer has successfully used processes for his Evasive Maneuvers game (even then he had to create a draw-check procedure to handle it).

I am seeing way too many kids with action games that are full of processes. They are inaccurate, messes up your View.Update, messes up your collision detection, especially those done with whatdotcolor, and God forbid if you based any part of your program on Time.Elapsed. Processes will ruin it all.

Try a normal loop once in a while. Guess what, the computer draws really really fast, I bet you can't tell that one thing is actually happening before another.

Author:  Flashkicks [ Fri May 07, 2004 7:47 am ]
Post subject: 

Its funny that you say that-bcuz I thought of processes to be really handy so far... but i do indeed see where you are coming from.. The only thing with only sticking to procedures is that you cant really have multiple things happening at once. Hence; i always used a process. I could keep my original procedure running and have a process at the same time.. Unless I am just gonig about things All Wrong Shocked ..lol.

Process werk GREAT for keeping music in a game as well Wink

Author:  Dan [ Fri May 07, 2004 12:00 pm ]
Post subject: 

U should only uses proces when they are needed, it dose not make scen to uses them rather then a procedure. And the avg new user dose not use them right b/c they do not understand the conspects of multy treading.

Author:  Cervantes [ Fri May 28, 2004 4:23 pm ]
Post subject: 

when you're typing out your code:
if you've typed this:
code:

for n : 1 .. 100

hit ctrl + enter, it will skip a line and type end for.
it even works if you've just typed
code:

for

it also works for ifs and loops and functions and whatnot.

EDIT: only works with Turing 4.0.5

Author:  Andy [ Thu Jun 03, 2004 8:32 pm ]
Post subject: 

wow.. thats cool... very useful for me cuz when i program in turing, i like to finish my loops/if statements/procs just so the indent will look prettier.. thx

Author:  wtd [ Mon Sep 27, 2004 2:39 pm ]
Post subject: 

dodge_tomahawk wrote:
in turing, you can just pass an array through a function and use upper to how big it is, in c, you have to shove the size in as well, kinda annoying


C++ templates and runtime type inferencing make this much easier.

code:
#include <iostream>

template <typename T, size_t N>
void println_all(T (&arr)[N])
{
        for (int i = 0; i < N; i++)
        {
                std::cout << arr[i] << std::endl;
        }
}

int main()
{
        int a[] = {3,4,5};
        println_all(a);
}

Author:  rizzix [ Mon Sep 27, 2004 9:15 pm ]
Post subject: 

correct me if i'm wrong but i'm not sure that code would work for something like this:

code:

int a[] = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
println_all(a);


basically a dynamically allocted array?

Author:  wtd [ Mon Sep 27, 2004 9:31 pm ]
Post subject: 

rizzix wrote:
correct me if i'm wrong but i'm not sure that code would work for something like this:

code:

int a[] = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
println_all(a);


basically a dynamically allocted array?


Apparently not, according to my quick check.

Author:  rizzix [ Mon Sep 27, 2004 9:33 pm ]
Post subject: 

ic intersting.. i got to check this out myself.. nice tip btw!! 8)

Author:  wtd [ Mon Sep 27, 2004 9:40 pm ]
Post subject: 

I think what you're looking for is:

code:
int * a = new int[3];

Author:  rizzix [ Mon Sep 27, 2004 9:40 pm ]
Post subject: 

hmm then i get:
code:
test.cpp: In function `int main()':
test.cpp:20: error: no matching function for call to `println_all(int*&)'

Author:  wtd [ Mon Sep 27, 2004 9:44 pm ]
Post subject: 

rizzix wrote:
hmm then i get:
code:
test.cpp: In function `int main()':
test.cpp:20: error: no matching function for call to `println_all(int*&)'


Yes, because as far as the compiler is concered, a is just a pointer, and what's getting passed in is a reference to a pointer, not an array.

I believe it's a case of the compiler just not holding onto enough information to make this work. A and C++ compilers are notoriously lacking in their storage of information. The fact that many other languages do preserve this information is what opens up a lot of opportunities for optimization.

Author:  rizzix [ Mon Sep 27, 2004 9:45 pm ]
Post subject: 

yep thats true.. so i guess it only works for staticly allocated arrays.

EDIT: i'm not sure if this is the expected behaviour of ANSI C++ nywyz.. i had a feeling it would not work for dynamically allocated arrays.. but i dont see why not.. but then again.. instead of a literal (in our case 3) it could have been a variable.. then it appears to make more sense to why not.. of course i'm talking about dynamic'ly allocated arrays only.

Author:  wtd [ Mon Sep 27, 2004 9:49 pm ]
Post subject: 

rizzix wrote:
yep thats true.. so i guess it only works for staticly allocated arrays.


There might be a way to make it work. I just haven't found it yet. Smile

Author:  rizzix [ Mon Sep 27, 2004 9:52 pm ]
Post subject: 

now i have second thoughts, i mean.. ok i need something clarified: does RTTI check allocated sizes etc.. or just Types only.

Author:  wtd [ Mon Sep 27, 2004 9:59 pm ]
Post subject: 

rizzix wrote:
now i have second thoughts, i mean.. ok i need something clarified: does RTTI check allocated sizes etc.. or just Types only.


I'm thinking it just checks types.


: