Once you guys have registered on ORAC, send your username, email address and real name (so we know who you are) to dls.programming@gmail.com. This way, it'll make it easier for Ben and I to keep track of you all.
The above email address is set up forward all incoming mail to both Ben's and my own email accounts. Keep this in mind if you want to send pictures of yourself to Ben for his enjoyment and extra tutoring for you; Seriously, I don't want to know.
James.
Friday, February 19, 2010
Thursday, February 11, 2010
Control Stuctures (Too Infinity But Not Beyond. Yet.)

Welcome to coding.
Control structures allow a program to repeat segments of code and make decisions. C/++ has a
set of these built into it so there's no need to #include anything.
Note: '=' is used for assigning values and '==' is a comparison operator that will return true if the variables on either side of it are equal.
Comparison (aka Relational and Equality) Operators
== is equal to
!= does not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Also
++ Increase variable by one
-- Decrease variable by one
'If' and 'Else' Statements
if (x == y){
printf ("teh if statement worked!\n");
} else {
printf ("x != y\n");
}
Code within an 'if' statement's block (between the following set of { } ) will only run if the condition returns true. In this case, the condition is 'x==y'. If this condition proves to be untrue, the code will not run.
If there is an 'else' statement following IMMEDIATELY after the 'if' block, the code within the 'else' block will run. 'else' statements do not require any conditions apart from the failure of a preceding 'if'.
For our above example, if, say, x = 5 and y = 5 then the above code would print "teh if statement worked". Otherwise, it would print "x != y".
There is also an "else if". This is similar to adding multiple if statements, however only one out of the group will ever run.
if(x == y){
printf("x == y!\n");
} else if (x == y+1) {
printf("x == y+1!\n");
} else {
printf("x is neither y nor y+1\n");
}
Note: If there's only one statement within an if or else block, you don't need curly brackets ( { } ).
'While' Loops
while ( flag == 0 ){
do stuff...;
if (something) flag = 1;
}
int i = 0, n = 9001;
while(i < n){
// do stuff
printf("%d\n", i);
i++;
}
Just like our 'if' statement, 'while' has a condition that must be filled in order to execute. As the name suggests, it will loop for as long as the condition remains true. "While" loops are especially useful when you want a section of your code to keep running until a certain event occurs. However, if you are looking to make a loop that will run a set number of times then a 'for' loop is most likely what you're looking for.
'For' Loops
for (int i = 0; i < 10; i++){
printf("%d\n", i);
}
A for loop allows you to declare a temporary variable that can be used as a counter. It will only exist within the loop - i.e. the variable you created goes out of scope once the 'for' loop ends. A 'for' loop consists of the following:
for (declaration of variable; conditions; increase/decrease) { code }
In this case, we're making a variable named i which will start at 0. The loop will run for as long as i is less than 10. i will increase by one every time the loop has run its course.
That particular for loop will print the following "0 1 2 3 4 5 6 7 8 9". This is because i is increased at the conclusion of the for loop to account for the way pointers function.
Note that the above 'for' loop is similar to the second 'while' loop - they would both do the exact same thing, but at the conclusion of the 'for' loop i wouldn't exist, whereas after the final } of the 'while' loop, i would still work fine.
James.
As I've mentioned numerous times before, it will get a lot more interesting as the year progresses. Also, the first programming competition that you will be participating in will take place somewhere between the 12th and 17th of August. My advice? Get training.
http://orac.amt.edu.au/jaz/
Wednesday, February 10, 2010
The C I/O Method You Will Be Using
So, the first thing we need to understand about I/O is exactly that. What is I/O?
Quite simply, I/O is a shortening of Input/Output. You should see this initialism used a fair bit when it comes to computers.
The next thing to realise about C and C++ I/O methods is that they all write to or read from a file. The main thing to realise here is that the keyboard is a special file, STDIN. The screen is another special file, STDOUT. Finally, there is also a special error file, STDERR. By default, this is routed to the screen, but you can change all of this behaviour.
In C, the file that you're reading to or writing from is of type "FILE" - just like an integer is of type "int".
Furthermore, as explained in our below post about pointers, we like to use FILE* instead of FILE. Life is just so much simpler this way.
To declare a FILE*, it's as simple as this:
FILE *in;
FILE *out;
Of course, I've used the names "in" and "out" because these, obviously, will represent the location in memory of our input and output files (again, this memory thing is because of our pointers. Seriously, re-read that other post).
However, there is a problem with this. If you understood/read anything about variables, you'd understand that just randomly declaring a pointer does nothing useful. We want *in to be talking about a file. Same goes for *out.
Thus, we can do this:
in = fopen("inputfile", "r");
out = fopen("outputfile", "w");
Of course, common sense tells us that "r" means read and "w" means write. Common sense is right.
This takes the file "inputfile" (it would usually be a .txt, but it can be anything, really. If the file was called "yourMum.txt", change the word "inputfile" to "yourMum.txt") and opens it in reading mode. Make sure the file exists; if it doesn't the universe explodes.
It then creates the file "outputfile", and makes it writeable. If "outputfile" already exists, it'll delete it first.
Now, we can print!
Unlike printing/reading to/from stdout/stdin, we can't use printf() or scanf(). These are specially-designed functions just for printing to/reading from the screen/keyboard. Instead, we need fprintf() or fscanf() - i.e. the printing/reading functions for files.
They both work with only a small change.
First inside the brackets is the file you're printing to. Then, you just copy-paste what you would've put inside printf().
For instance,
fprintf(out, "%d\n", 9000+1);
fscanf(in, "%d", &n);
Or, for a more complete example:
#include <cstdio>
int main(){
FILE* in = fopen("inputfile", "r");
FILE* out = fopen("outputfile", "w");
fscanf (in, "%c %d\n", &letter, &number); // read in data
fprintf (out, "%c %d\n", letter, number); // write data
return 0;
}
That's it for now. Any questions, either jump on #dlspc via http://webchat.freenode.net/ or ask us on Thursday.
James. Ben. Pretty much all Ben.
PS If you felt like you weren't getting enough attention last week, don't worry. We'll all be in the one pod this week.
Quite simply, I/O is a shortening of Input/Output. You should see this initialism used a fair bit when it comes to computers.
The next thing to realise about C and C++ I/O methods is that they all write to or read from a file. The main thing to realise here is that the keyboard is a special file, STDIN. The screen is another special file, STDOUT. Finally, there is also a special error file, STDERR. By default, this is routed to the screen, but you can change all of this behaviour.
In C, the file that you're reading to or writing from is of type "FILE" - just like an integer is of type "int".
Furthermore, as explained in our below post about pointers, we like to use FILE* instead of FILE. Life is just so much simpler this way.
To declare a FILE*, it's as simple as this:
FILE *in;
FILE *out;
Of course, I've used the names "in" and "out" because these, obviously, will represent the location in memory of our input and output files (again, this memory thing is because of our pointers. Seriously, re-read that other post).
However, there is a problem with this. If you understood/read anything about variables, you'd understand that just randomly declaring a pointer does nothing useful. We want *in to be talking about a file. Same goes for *out.
Thus, we can do this:
in = fopen("inputfile", "r");
out = fopen("outputfile", "w");
Of course, common sense tells us that "r" means read and "w" means write. Common sense is right.
This takes the file "inputfile" (it would usually be a .txt, but it can be anything, really. If the file was called "yourMum.txt", change the word "inputfile" to "yourMum.txt") and opens it in reading mode. Make sure the file exists; if it doesn't the universe explodes.
It then creates the file "outputfile", and makes it writeable. If "outputfile" already exists, it'll delete it first.
Now, we can print!
Unlike printing/reading to/from stdout/stdin, we can't use printf() or scanf(). These are specially-designed functions just for printing to/reading from the screen/keyboard. Instead, we need fprintf() or fscanf() - i.e. the printing/reading functions for files.
They both work with only a small change.
First inside the brackets is the file you're printing to. Then, you just copy-paste what you would've put inside printf().
For instance,
fprintf(out, "%d\n", 9000+1);
fscanf(in, "%d", &n);
Or, for a more complete example:
#include <cstdio>
int main(){
FILE* in = fopen("inputfile", "r");
FILE* out = fopen("outputfile", "w");
fscanf (in, "%c %d\n", &letter, &number); // read in data
fprintf (out, "%c %d\n", letter, number); // write data
return 0;
}
That's it for now. Any questions, either jump on #dlspc via http://webchat.freenode.net/ or ask us on Thursday.
PS If you felt like you weren't getting enough attention last week, don't worry. We'll all be in the one pod this week.
Tuesday, February 9, 2010
Registrations
After you've signed up on ORAC, could you please send your username to my email address? This way I'll be able to receive your submissions and monitor your progress.
butler_j@live.com
James.
Note: If the below post scared you, rest assured that it scares me too :p
butler_j@live.com
James.
Note: If the below post scared you, rest assured that it scares me too :p
Sunday, February 7, 2010
Assigning Variables

It only just occurred to me that you might end up wanting to store data eventually. This should help you get through Addition in Starter Problems 1 on ORAC.
In order to manipulate data, you first have to store it somewhere. We do this by assigning the data to what's known as a variable (which is essentially a block of memory). Before we can utilise these variables we must tell the computer to allocate space. This is called declaring variables.
Declaring A Variable
Everything on a computer is declared in terms of bits and bytes. All these bytes are generally stored inside your RAM, although using things like printf(); you can also write to your Hard Drive space.
There are trade-offs with both ideas - for instance, you only have a very limited amount of RAM; computers range between 128MB to roughly 2GB of RAM, and 4GB is the limit for any 32-but system when it comes to RAM.
On the other hand, most computers have virtually infinite hard drive space (1TB hard drives aren't worth that much anymore.....), but reading from and writing to files takes up far more time than reading or writing to RAM.
As such, when it comes to writing C or C++ programs, you need to create as much of a balance as is possible. Depending on what you're doing, it's fine to use 50MB or 100MB of RAM, but you shouldn't be choking down Gigabytes of the shit. It's just not healthy!
Keeping this in mind, we move to thinking about variables. The cool thing about variables is that they generally only take up a few bytes each, but before you know it this adds up.
As a bit of background knowledge, there are 8 bits to a byte. Each bit in a data type is also the next power of two. To illustrate this, let's take a "char". A "char" is 1 byte; thus 8 bits.
Initially, it looks like this in memory:
00000000
If we want to store the letter "a" in this char (which corresponds to the number 97 in ASCII), our char would look like this:
01100001
Converting this, we walk from right-to-left, and each time we come across a number we increase our power of two. Thus, the right-most number represents whether our number includes 20, the one to the left of this is 21, the one to the left again is 22, etc.
Thus, our number is composed of 1*(20) + 1*(25) + 1*(26) = 1 + 32 + 64 = 97.
(i.e. any number with a 0 in front of it implies 0 * that number, which is just 0).
Usually, unless it's an unsigned data type (explained below), the first bit always refers to the negative sign. Thus,
11100001
signifies -3, and NOT 97 + 27 (for a signed type, it's -1*(2next number in the sequence - the value without the sign bit)).
When it comes to C and C++, we declare our variables simply by typing a valid data type in front of them (the next section will cover these), and our variable name.
For instance, to declare a character, we would type this:
char myCharacter;
This declares a variable that takes up 1 byte in memory, and we have decided to refer to this byte of memory as "myCharacter".
This is the point of variable declaration; for us humans to be able to refer to the location of memory we want. Or something like that, anyway.
Data Types
Disclaimer: These are somewhat architecture-dependent. For most cases across any 32-bit system, these values are correct but for a 64-bit system they are incredibly likely to be utterly wrong. If in doubt, open up a new .cpp file and get it to do this: 'printf("%d\n", sizeof(int));'
a "char": 1 byte.
a "bool": 1 byte, and can only take the value "true" or "false".
a "short" (or "short int", either works): 2 bytes.
an "int": 4 bytes.
a "long" (or "long int"): 4 bytes. There is usually no difference between "int" and "long", unless you're on a 64-bit system.
a "float": 4 bytes. These hold floating point numbers - i.e. numbers that require a decimal point. Unlike an integer, a 4-byte floating point number can hold roughly 3.4 +/- 107.
a "long long" (or "long long int"): 8 bytes.
a "double": 8 bytes. These hold floating point numbers up to 1.7 +/- 10308.
a "long double": 8 bytes. Again, this is 8 bytes on an 32-bit system, but this will probably different on a 64-bit system.
Also, all these can be prefixed with "unsigned".
By default, every type is "signed". This means the very last (or the left-most) bit determines whether or not the number you are storing is negative or positive. If you make a type "unsigned", this last bit now represents part of the number instead of it's sign (hence signed/unsigned).
A Brief example
#include <cstdio>
using namespace std;
int main(){
int y;
char BenIsNotCool;
printf("%d\n", sizeof(BenIsNotCool) + sizeof(y));
BenIsNotCool = 'A'
y = 1337;
printf("%d\n", sizeof(BenIsNotCool) + sizeof(y));
return 0;
}
The output of this example would be this:
5
5
This shows that whether or not a variable actually stores values, it will always take up the size it's allocated (although technically, these variables actually do store values; they're just garbage).
Arrays
So, it's all good and well to declare one or two variables to do one or two things. But what happens if you need ninety variables, or over nine thousand variables, and they're all the same type (or at least, multiple of them are the same type)?
This is where arrays come in handy.
Unlike one variable, which takes up, say 4 bytes, an array takes up a contiguous space in memory. If you declare an array for 10 variables, this takes up 40 bytes. If you declare an array for 100 variables, that's 400 bytes, etc. etc.
To declare an array, we declare it like so:
type myArray[size];
Say we want to declare 100 integers, in an array called JamesSucksDick. We would do this like so:
int JamesSucksDick[100];
This gives us 100 integers to play with, all allocated in a contiguous block.
Of course, this now gives us 100 integers to play with. However, we can't just access them with JamesSucksDick = 10, 11, 13, 11202029, 9001;.
That just wouldn't work.
So how?
Pointers
So, the other day we discussed pointers. Briefly.
Essentially, a pointer does exactly what you'd expect: it points. But unlike a regular variable, which only talks about one specific block of memory, a pointer can point to any block of memory of that same type. Thus, an int pointer can point to any int in memory (careful, this will come up later), but it can NOT point to a character.
So, onwards to pointers.
To declare a pointer, it's as simple as this:
type *pointerName;
This will declare a pointer, called pointerName. If we want to declare a pointer to an integer, it's as easy as:
int *myPointer;
The problem with this is that we cant really do 'myPointer = 4', like we would for a normal variable. You could try this. But it will most likely crash.
See the thing with pointers is that the = sign talks about the address they reference. For instance, 'myPointer = 4' tries to access the 4th block of memory. And that's just going to fail.
So we need something else.
Pointers have two extra things they can use. You also have access to * and &.
When talking about pointers, * is what's called the dereference operator. In other words, * is the value of what the pointer is referring to. Note that this is different from the * used to multiply two numbers together (for instance, 4*4 or a*b) and is different from the * used to declare the pointer.
int *myPointer;
*myPointer = 4;
This changes the value of what myPointer is pointing to, and sets it to 4.
& is the reference operator. Using & talks about what the pointer is referring to.
int some_random_value, *myPointer;
myPointer = &some_random_value;
*myPointer = 4;
printf("%d\n", some_random_value);
Of course, this is probably getting a little confusing, and you're probably wondering what pointers have a use for at all.
When it comes to real-world stuff, they're mighty useful. They allow you to dynamically allocate memory (more on this later) and do proper garbage collection and keep track of forty nine million variables with just the one pointer.
However, we started this discussion whilst talking about arrays. So let's go back to arrays!
Arrays
So just to recap, we declare an array like so:
type array[size];
The cool thing about arrays is that they're just pointers!
"array" refers to the first block out of our size, "(array+1)" refers to the second block out of our size, "(array+2)" refers to the third block, etc. etc.
So, to write to any of these values, we can use the dereference operator (heh, remember that? :))
*array = 1;
*(array+1) = 3;
*(array+2) = 3;
*(array+3) = 7;
printf("%d%d%d%d\n", *(array), *(array+1), *(array+2), *(array+3));
But see, programmers are lazy. Why type "*(array+n)" when you want to modify the nth element of an array. Instead, they use the shorthand notation array[n]. This means exactly the same thing as *(array+n)
It is for this reason that we count arrays from 0. i.e., to access the first element (which theoretically lives at *(array+0)), we use array[0] = something.
So, using this newfound knowledge:
array[0] = 9;
array[1] = 0;
array[2] = 0;
array[3] = 1;
Note that the one caveat here is that you must make sure that the element you access is always less than the size that you declare. If you declare something like so:
int array[10];
you declare an array with 10 elements. This means that you can access array[0], array[1], ..., array[9] (count from 0 using your fingers; if you sum up from 0 to 9, you'll get 10), but accessing array[10] is like accessing a variable that doesn't exist, with one minor difference.
If you access "someVariable", and you haven't declared this variable, your compiler will yell at you and your code won't compile. If you access array[10] (i.e. the 11th element) and you've only declared an array with 10 elements, it will compile just fine, but when it comes to runtime you could be doing some seriously dangerous stuff).
Scopes
Every variable has a certain area in which it exists. These areas are referred to as 'scopes'. In total, there are five diferent types of scopes. However, only two of these require your immediate attention.
Global and Local
int x = 1;
int main(){
int y = 2;
}
If you look above, you'll notice that I've declared x outside of main() and y within it. 'x' is what is known as a 'global' variable. It means that it is valid throughout the entire file and is declared before the execution of main(). 'y', however, is what is known as a 'local' variable and only exists after the program has reached a point where it has been declared. Locals are also restricted to the block of code (including the blocks enclosed within said block) in which they are declared.
Note: A block of code consists of code that lies between two curly brackets ( { } ). For instance:
int main(){
int x;
{
int y;
y = 8;
}
x = 4;
printf("%d %d\n", x, y);
return 0;
}
will fail at compiling, as "y" doesn't exist after the }
There is one difference that scope makes.
For a moment, let's pretend that you have two separate parts of RAM. You have "automatic storage" (which is also commonly called "the stack") and you have "free storage" (often called "the heap"). These fulfill two different jobs. Automatic storage is designed to briefly hold variables and references - for instance, somewhere inside the Automatic storage is a reference to the "main()" function.
Free storage holds the rest of things. Large variables, dynamically allocated memory (which we'll cover at a later date) and most RAM-intensive operations should be performed here.
In terms of comparisons, free storage is slightly slower to create than automatic storage. It's just as fast to access, however the creation is slightly slowed. This has to do with the way a CPU works, and so we're not going to explain it here, however note that this is measured in terms of ten-thousandths of a second. In return, you get roughly 4MB of stack memory to play with, as opposed to heap, which is roughly RAM size minus 4MB.
Of course, these aren't actually two separate parts of RAM; they're one and the same, but relate to the way your program uses the available memory. Thus, be aware of them.
Now, how does this effect you?
Well, anything declared inside a function, as well as the declaration of the function itself, is declared on the stack (i.e. automatically stored). This includes all variables and arrays you declare. As such, you should be very careful with this, because you're limiting yourself to 4MB of data (unless you dynamically allocate, but more on this at a later date).
Anything declared globally is placed on the heap. "Globally" refers to any variable declared outside all functions
int x; // declared on the heap
int main(){ // there is a reference to main() that is declared on the stack
int someInt; // declared on the stack
char penis; // more stackwork.
return 0;
}
Anyway, 'til next time!
James and Ben (mostly Ben).
Compliments to my friend Georgie Brooke for the photo.
Saturday, February 6, 2010
Must We Waste Time?
Could everyone please register here: http://orac.amt.edu.au/jaz/cgi-bin/train/
This site contains a lot of programming problems that we will be working on over the year. All registrations are processed by a human so it may take a couple days. Feel free to take a peek at or attempt a few problems. Reading a few pages of the tutorial on cplusplus.com should help you solve the first few (look in Starter Problems 1).
James.
EDIT: ADD ME ON FACEBOOK (James Butler).
This site contains a lot of programming problems that we will be working on over the year. All registrations are processed by a human so it may take a couple days. Feel free to take a peek at or attempt a few problems. Reading a few pages of the tutorial on cplusplus.com should help you solve the first few (look in Starter Problems 1).
James.
EDIT: ADD ME ON FACEBOOK (James Butler).
Friday, February 5, 2010
You're Lazy And I Love It

Ubuntu ftw ^
In an ideal world you'd all have downloaded Code::blocks by now and tried to have a fiddle but, from past experiences (namely my own) I'm over 9000% sure that this is not the case. This doesn't really bother me but, our evil dictator Benjamin Aristotle has higher expectations of this year's intake and believes that without prompting, you'll all go here some time this week. Luckily I'm slightly nicer than him and have decided to take some time out of my busy schedule to point you in the right direction. Ignore my advice at the risk of Velociraptors and Ben. Or both. I'm still deciding what's worse.
In this post I'll be covering how to install an editor/IDE and how to write/compile code.
WINBLOWS (aka windows)
Just download the one file (the MinGW release). Run it. ????. Profit.
http://www.codeblocks.org/downloads/5
Mac OSX
You have two options: download codeblocks, or use Xcode. I suggest you use Code::blocks for the time being as Xcode can be a bit fiddly at the start (but is SOO worth it due to gargantuan amounts of sexiness).
http://www.codeblocks.org/downloads/5#mac
First, create a folder on your desktop called Programming. Next, download Code::Blocks from the link above. Due to the nature of Macs,
After installing Code:blocks and writing your program and saving it in your programming folder, open up terminal and copy paste the following:
cd ~/Desktop/Programming
g++ ProgramName.cpp -o ProgramName.app -O3 && ./ProgramName.app
Let me break that down for you.

(cd = change directory)
The first line changes the 'directory' which is basically where your computer is looking. By default, terminal opens at /Users/YourName/ which is the current user's Home folder (see above). In my case this is /Users/James/. /Users/YourName/ contains most of your files and all the main folders such as 'Desktop', 'Documents', 'Pictures' etc. ~ Is an extension which can be used to refer to the current user's home folder. So instead, the first line could read like this:
cd ~/Desktop/Programming
Since you didn't save your program in /Users/YourName/, you must tell your computer where to look instead by changing the directory. In this case is /Users/YourName/Desktop/Programming.
The second line is what will actually run your code. It calls upon g++ ,which is a compiler (what translates your code into something readable by the computer), and tells it to run your program. Because you're a Mac user (respect), you can only run .app files. -o ProgramName.app basically saves a copy of your code as a .app file. As a result, you will notice a number a few extra files for each program you have compiled. Finally, the && ./ProgramName.app runs the app file.
Linux
Mostly the same deal as the Mac; write your code in an editor and then compile it with a compiler (such as GNU G++).
To start, you'll probably want a decent code editor. You might have gedit or a similar text editor to start with, but to be honest, it's like coding with notepad.
Instead, open up terminal. (Either Applications->Accessories->Terminal, or hit Alt+F2 and type in gnome-terminal for Ubuntu, konsole for any KDE release or find your own equivalent).
From here:
sudo apt-get install konsole kate g++ build-essential.
This will install Kate and its in-built terminal, konsole, as well as install g++ and the basic gcc compiler.
From here, open up kate and code there. You might want to create a directory on your desktop to save all your code in, too. Let's call this directory "Programming" (right-click your destop, New Folder, etc. etc. to create this folder)
Once you've coded something up, open up either the konsole from kate or gnome-terminal from Alt+F2 or any other terminal you can find.
cd ~/Desktop/Programming/
This will change the directory terminal is working in to the current user's Programming folder, which was found inside the Desktop (as stated above, ~ is an extension for the current user's home folder - so my username is "ben", thus ~ expands to /home/ben)
Finally, g++ sourcecode.cpp -o sourcecode && ./sourcecode.
This uses g++ to turn your sourcecode.cpp file into a program named "sourcecode" and then run it. Of course, g++ has various options that it can take - for instance, you can optimise your code with -O2 or -O3 or -O1 or -Osize, or turn warnings on with -Wall, or turn warnings completely off with -w.
It would be a good idea to type in man gcc and read them all.
Any questions? Send me an email or IM me at butler_j@live.com or Ben at bgbnbigben@hotmail.com.
James.
Note: Make sure that you save your programs with the .cpp file extension. 10 points for whoever guesses what that does.
Subscribe to:
Comments (Atom)