Wednesday, 9 November 2011

[C] Hello World!

We shall start with a simple Hello World tutorial, as all programming classes start off :)
The purpose of this tutorial is to teach users how to print a line of words to the output environment and format them. 

The code is as follows: 

#include <stdio.h> int main ( )
{
printf("Hello World!\n");
return 0;}
First, we include the standard header, #include <stdio.h>. If you're new to programming, it'll take a while to get the hang of remembering the names of the headers to include, so you could remember it is "standard in-out". This header file basically stores a number of functions, such as the printf function as you would see later. This header is required to work most C code. Other headers are math.h, time.h, stdlib.h, string.h and so on, which you would discover later.

We have a main function which returns and integer 0. This is denoted by the int main ( ) and return 0; portions. The main function does not take in any arguments, so what's between the brackets are left void. 

Now we use curly braces { } to enclose our codes. Anything within a brace is a code block, meaning they will be executed together in a round. In this case, those within the { } braces will be executed during one round of the run of the main function. 

Now let me side step a little to explain a function. A function is something that takes an input and returns something as an output. In our main function here, it does not take any input, so it is left void and returns 0 as an output at the end of its execution. Sometimes, it may be written as: 
int main (void)
{
//Code here
return 0;
}
Main is the name of the function. The return type is integer, denotated by 'int'. You could think of a function as a blender. You pass in oranges and apples in the brackets (oranges, apples) to a Blender. What you get out is a orange-apple-juice. This is what a function does. A blender function might go like this: 

juice blender (fruit orange, fruit apple)
{
blend fruits;
return orange-apple-juice;
}
Okay, silly example. Let's move on. printf is the function call used to print a line of output. The text is formatted within " ". The \n means a new line. One very important thing is the semicolon ; behind each line. That basically tells the compiler that it's the end of a command. It works just like a full-stop in english writing. Sometimes you might get compiler errors, then check if there's a semicolon behind each statement as you want it to be; else you'll get errors! 

There you go! Type these in and you get a nice little output! 

More on printf function can be found here: 

This is a good reference for any type of codes you want. Some summaries you might want to take note of: 
  • "\n" makes a new line
  • "\t" makes a new tab (4 spaces) 
  • " " makes a space 
  • More on formatting numbers in output later :) 
Here, I've tried to explain how to print an output to a console. I hope it is useful. Any questions, please leave a comment or email me. Thank you! 

[C] Running C Codes in Windows

Just a quick post to rectify some problems you might face in coding, as I did in the first time.

You might see the black console appear once the code compiles then it disappears quickly. This happens if you're using a Windows computer and there's a hack to it.

In your main function, add the line 'system("pause");' before 'return 0;'

int main ( )
{
    //Code here
 
   system ("pause");   
    return 0;
}    

For Dev C++ or wxDev C++, to compile and run a file, press F9. That's a good shortcut.

[C] Setting up C/C++ IDE

You've decided to take the wonderful step to learn how to code in C. You will mostly be making .exe applications, i.e. those applications you see on a black screen with white words. Nothing fancy, but that's alright. Once you get the basics straight, you can easily pick up another language and start experimenting with those fancy stuff or code games.

This is an example console you would get throughout these tutorials. Pretty cool huh?



But first, you need and IDE, or Integrated Development Environment. That's basically something that has a region for you to code, a compiler and a debugger. I use Dev C++, but there's also Eclipse that's popular. So go to the websites, look at the screenshots and decide which ones you want to use.

Then, download the executable files, click 'Next' until the end and 'Accept Terms and Agreement' then click 'Finish'. You can open your program and start tinkering around with the features to be familiar with the environment.

[Books] How not to program in C++

Here's a really good book with 111 non-working programs and 3 working ones. How Not to Program in C++ by Steve Oualline will give you buggy programs for you to figure out what is the mistake in the program. There're hints and answers and also some short snippets of stories or jokes to relieve your brain - which would be tired from trying to find the errors.

It's a book for those who want to sharpen their programming skills or tracing skills. This will help when debugging your own programs. Most errors are very minor, like punctuation marks or so but they can make such a great difference to the output and the speed of the program. Something to take note of, and a good book to read during spare time.

You can find it at Google books here:
http://books.google.com/books/about/How_not_to_program_in_C++.html?id=wgwE0dQ15xEC

Or if you want the pdf, but not so nice version, here it is: http://www.uml.org.cn/c++/pdf/HowNottoPrograinC%2B%2B.pdf

Thursday, 3 November 2011

LaTeX vs. MS Word?

I don't use LaTeX for every single piece of document I have to write. As with most things, LaTeX has its advantages and disadvantages.

Let me write some reasons why I use LaTeX for some documents. These documents have a lot of math symbols inside (yes, I did use it for my statistics journal) and using LaTeX not only makes writing math symbols a lot easier, it makes them prettier. You don't have to open Equation Editor all the time and slowly find your alpha symbol or your greater/equal to sign or your double arrow. You just need to type these commands \alpha, \geq, \doublearrow respectively and typeset your document and here it is! 

Here are the math equations compared (taken from http://amath.colorado.edu/documentation/LaTeX/information/whether.html

1a. TeX version

1b. Word version

2a.
TeX
2b.
Word
As you can see, it's prettier in TeX, and more professional looking too.
Another reason why I use TeX is because I could divide my document into sections and the output pdf will give me each separate section as a bookmark. This makes things easier to find. Plus, the section formatting is pretty simple and easy to do. It can make a title page and a content page, plus you can add footnotes in between your paragraph words, so that you can keep track of your references as you use them.

For these reasons, I use LaTeX.

But of course, I use MS Word too. MS Word is a WYSIWYG, or What You See Is What You Get, editor. This makes it much easier to organise my thoughts when I see the words and pictures out on the screen I am looking at, rather than waiting it to compile then seeing if it works. LaTeX can give you typesetting errors, which occurs when you forget a close brace or backslash command. Sometimes my friends don't know LaTeX so it isn't easy to work with that.

And also, some things are easier to do in MS Word, like drawing graphics or copy-and-pasting things.

LaTeX Links

Here are some links to great pages that teach LaTeX pretty well.
Or just links that you would go to when you need to find the command for a certain thing.
I can't possibly write every single thing in this blog, and the Internet's a great place to find help, so make use of it.



  • The Not So Short Introduction to LATEX by Tobias Oetiker Hubert Partl, Irene Hyna and Elisabeth Schlegl. http://mirror.unl.edu/ctan/info/lshort/
  • LaTeX for Word Processor Users by Guido Gonzato. http://www.ctan.org/tex-archive/info/latex4wp/latex4wp.pdf
  • Online Tutorials for LaTeX by India TUG. http://www.tug.org/tutorials/tugindia/
  • Hypertext Help with LaTeX by Dr Sheldon Green. http://www.giss.nasa.gov/tools/latex/
  • Office of Information Technology, Colorando. http://amath.colorado.edu/documentation/LaTeX/
  • The Not-So-Short Guide to LaTeX by Tobias Oetiker: http://tobi.oetiker.ch/lshort/lshort.pdf

And of course, there's the LaTeX wiki page here: http://en.wikibooks.org/wiki/LaTeX. This is a very good reference for any LaTeX functionality, so be sure to go back to it whenever you need to! 

Lastly, I need to credit the original LaTeX page: http://www.latex-project.org/. It has a documentation page too. 


LaTeX editor


LaTeX is a document-processor, just like Microsoft Word that takes away all the graphics interface you see in a common MS Office document like those buttons and such but adds in loads of functionality. There's loads that you can do with LaTeX, which includes formatting documents in awesome cool styles and typing math pretty easily. I've been using LaTeX for quite a number of documents and I really like it. It has a little bit of a learning curve, but not that much. Most of all, it isn't programming. It is simple typing of commands and the command names all make perfect sense so you can catch on pretty easily if you want. If you put away all that preconceived notions and learn it, you'll pick it up easily.

For example, you can get this:

Picture taken from http://amath.colorado.edu/documentation/LaTeX/basics/example.html

First, before you actually start coding in LaTeX to get those cool documents, you need to download a TeX editor. I use MikTeX, though there's a newer version called proTeX that's been built on MikTeX. Or if you're using a Mac, you would download MacTeX. There's also more graphics-based editors like TeXniCenter and all, but I still like MikTeX, simple and sleek.

Full links of obtaining LaTeX are available here.

Install the LaTeX packages and go on to the next post!