Thursday, February 4, 2010

Week One: Welcome?


It looks like DLSPC is set for a slightly better year than last with 9 more people on board (yes, it was only me and Ben (Alex and Rami don't count because all they did was talk and play games)). A little message to the new kids on the block, don't let Ben's poor teaching put you off, he means well (usually). You might not understand what's going on at the moment and that's perfectly normal. Just give it some time and it'll all start coming together. That's when the real fun starts :p

So, just in case Ben bored you to death and as a result you didn't really learn anything or if you didn't consider anything he said important (which would be perfectly understanable), then here's a summary of what we did today.

"Hello World"  (Basic structure of a C/++ program)


#include  <cstdio>

int main(){
   printf ("Hello, World!\n");
   return 0;
}


Ouput

Hello World!


Let's examine that code:

#include <cstdio>
(C Standard In/Out)
This tells the program to open the 'cstdio' library. This library is a file which contains various functions, information etc. By 'including' a library you are giving your program access to these resources.

Another way to look at #includes is to think of each as a shelf in the library of C/++ (forget my previous use of the word 'library'). Each shelf contains a number of books. So if we consider <cstdio> to be a shelf full with books, then the function printf is one of those books.


using namespace std;
Don't ask, just type.


int main(){
One way that programmers deal with data is by using 'functions'. I don't think I have to explain to you that each function does something. Each function is designed to carry out one or more jobs. For example, the printf() function prints data for you. Functions are easy to spot as they are always follwed by a set of parenthesis (). These brackets are used to pass any necessary information to whatever function. Note that not all functions require you to feed them data. An example of such a function is main()

In C/++, the main() function is where a program will start from. No matter where it is placed, a program will always start from main(). Because of this, all C/++ require a main() function.

Curly brackets are used to open and close blocks of code.

printf("Hello World!\n");
This calls the printf function and tells it to print a string. This string contains all the above characters (including the space). The \n tells the computer to print a new line.

return 0;
This return statement will cause the main function to quit. Return statements may be followed by a return code. In this case, the return code is 0 (which is generally interpreted as meaning the code ran without any errors).

Notes:
  • The two quotation marks (" ") indicate that we're referring to a string. A string is a sequence of characters. If we wanted to just print one character we could use two apostrophes (' '). However, keep in mind that a newline ('\n') takes up one character. Because it is a special character (which is why it has the \ there), it only takes up one location in memory and thus only counts as one "thing".
    Think of it this way: Only one thing is printed to your screen when you type \n, or \t, or \\, or \b, or \. Thus, it's only one "thing" and you can use either '\n' or "\n".
  •  In C/++,  a semicolon (;) tells the computer that we have finished a statement.
  • Also in C/++, curly brackets ({ }) open and close blocks of code.
If you have any questions or want to know MOAR, don't hesitate to talk to either Ben or I. It is our sole purpose in life to make you awesome at coding.

Also, www.cplusplus.com is a good place to go. Here's a link to their extensive tutorial on C++. Don't worry if you don't understand some things you read on there, you will over time (not to mention you could always just ask Ben or I to explain it to you).

This has been episode one of coding from your friendly neighbourhood coder,

James. (and Ben?)

PS. Sure was a scorcher.

    5 comments:

    1. Nice intro. Never got that part when I started my CS degree a few years ago. I dropped out the following week. :)

      Anyway, the above code... does that just go into any file via gedit, or is there a specific program I should use to enter the code?

      Also, re note #2: "a semicolon tells the computer that we have finished a statement." What is a 'statement'?

      ReplyDelete
    2. i was worried you'd ask that. a statement is one action in your code. ie.

      int x;

      declaring a variable is a statement.

      printf("hey\n");

      printing is a statement.

      however, there are exceptions when it comes to semi colons. when you have related actions such as assigning values or declaring ints you can sometimes do it it on one line.

      ie. int x, a, b, c;
      x = 5, c = 4;
      etc.

      ReplyDelete
    3. as to what you'd need to edit and compile your code, download a text editor such as kate. next, ask ben what compiler to get / the name of it in the repositories. i think you just sudo apt-get install gcc but i'd check with him just to be sure.

      ReplyDelete
    4. To compile on linux, "sudo apt-get install gcc g++ build-essential kate konsole"

      let that run and install.
      Whilst gedit is perfectly acceptable, it's like coding in notepad :P
      kate has the ability to do syntax highlighting and is customisable and rather cool.
      konsole is just a version of terminal from inside kate.

      Aside from that, once you've got your file saved in a specific directory (Let's say you've put it in a folder called "Coding" on your Desktop), save it as .cpp (obviously, substitute for something meaningful:

      cd ~/Desktop/Coding
      g++ .cpp -o filename && ./filename

      This changes the directory your terminal is working in to your Coding folder (~ is the extension for the home folder of the current user - if you're logged in as "dave" ~ will be /home/dave), and then calls upon g++ to take .cpp, output it (i.e. -o) to a file name "filename" and then run it via the ./ segment.
      g++ has a whole pile of options - the more common are -O1 or -O2 or -O3 or -Osize (these are different levels of optimisations, respectively), -Wall (Warnings (all)), -Werror (turn warnings into erros), etc. etc.
      It's best to have a bit of a read through gcc's man page - 'man gcc' in terminal.

      ReplyDelete