Friday, May 6, 2011

Chapter[0] : Before You Start

Computers are, both by name and purpose, a form of calculators. We have used our computers to listen to music, watch videos, play games, prepare documents etc. However, computers basically do not understand anything but zeros (0) and ones (1). So when you, an user, are running an application such as a music player on your PC, your machine actually does the work for playing the music by performing some simple calculations in the background. Like a music player, there are many other software available, and they serve a variety of purposes. Due to the abundance and flexibility of these software, computers are now widely used for almost everything. To create a software like these, we have to write programs which instruct the computer to behave just the way we want!

There was a time when programs had to be written as sequence of 0s and 1s, as the computers can not understand any other form of language other than its own. It was called the Machine language, and had only 0 and 1 as it alphabets. As you might expect, memorizing sequences of 0s and 1s was probably the hardest way of programming. As such, Assembly language was introduced, which provided programmers a set of English instructions that they could write in their programs to instruct the computer. For example, the ADD instruction performs addition, MUL did multiplication, and so on. But computers, as we have already said, will only understand 0s and 1s, not ADD or MUL or any other form of instruction for that matter. Therefore, the job of the Assembler was to convert these English instructions to machine language (0s and 1s). With time, our demands have increased, and the need for more functionality kept growing. More functionality requires more lines of code. Assembly language wasn't appropriate for writing big programs that consisted a large code-base. The need for an easier and more improved system have led to the development of newer languages such as Fortran, Basic, Pascal and C. These classical languages were followed by the more recent Visual Basic, Java, C-sharp (C#), Perl, PHP, Python and Ruby. And there are more to come. What makes these programming languages different is the difference between the compilers for each. It is the compiler that converts the more advanced set of instructions that we write to the machine language that the computer understands. That way, we do not have to worry about whether the computer will understand what we have written.

Writing a program involves three primary steps. First of all, the programmer must have a clear knowledge of what he wants to accomplish. Next, an algorithm is developed. This involves digging out appropriate logic and laying them out in proper order. Once the designing phase is complete, the algorithm we have devised is put into code using a known programming language.

In this book, we will try to learn some fundamental concepts of programming, and write our codes in C. This book assumes that you are comfortable using a computer and completely unfamiliar with programming. I have chosen C for the examples given throughout this book as C is very popular and powerful as a programming language, in spite of being quite old. C programs are very helpful for understanding the fundamentals of programming. Moreover, in all the popular programming contests, C stands out being the most widely used amongst the few programming languages supported. However, we are not going to learn everything about C in this book, but take in only what is required to understand the fundamental concepts of programming. Once you finish reading this book, you may start reading books dedicated to teach the C language, or any other programming language for that matter (for example C++, Java or Python). Check the Appendix where I have mentioned names of some useful books in this regard.

You will need three things to actually understand the contents of this book; a computer (preferably with an internet connection ), a C compiler and sufficient time. To get the most of out this book, read it slow, probably taking two to three months. Do not haste to the end just to realize you have learned nothing. Reading alone is not sufficient to learn programming. You have to stretch those fingers out and spend more time coding every single example in the book. When asked a question, you should think about it and try to find an answer. If you are taking too long to think, do not worry. It is indeed a very good practice. A programmer must grow the habit of thinking about a problem at a long stretch, even if its a span of a few days. You can not start reading the next chapter unless you have completely understood the previous one. Also, if any of the chapters seem very easy to you, do not skip it. Read it through, code all the programs in the chapter, and only then move on. There is a big difference between general studies and the way we should learn programming. Learning to program involves studying the material and practice it alongside. Keep in mind that this book will not make you a programmer. It will only start your journey towards a complete programmer.

Okay, now that you have a computer, we have to find a C compiler for you. There are many compilers for C, available for all the platforms in use today. If you are using Mac or Linux, gcc the best option for you. Usually, most of the Linux distributions have gcc per-installed. If you don't find it in your machine, you must install the package. In Windows, you can use Codeblocks (http://www.codeblocks.org/). Its a free and open source IDE that also runs on Mac and Linux machines. You can always use a general purpose text editor to write your code and compile it to get the executable. But most of the IDEs will do it for you. They have a text editor and a compiler built-in, along with many other tools. Using an IDE is the easiest way to learn to code.

You may download Codeblocks from their website http://www.codeblocks.org. Go the the Downloads page, and click on Binaries. You will bee provided two download options:  codeblocks-10.05-setup.exe and codeblocks-10.05mingw-setup.exe .  Download the latter (74.0 MB). Once downloaded, you can install in much the same way you would install any other application or game. Ubuntu users can download the software using Ubuntu Software Center (Applications > Ubuntu Software Center).

To develop you programming skills, you will need a lot of practice. There are quite a few web sites where you will find a list of problems that you can solve by writing programs. All these sites will allow you to submit solutions coded in C. What's more interesting is that some of these sites also arrange programming contests on a regular basis. Participating in these contests will not only hone your programming skills, but will also put you in a community of some really great programmers all over the world. However, to perform well in these contests, you should also have good analytical skills to solve mathematical problems. You can learn more about programming contests at the end of this book.

The number under each program in this book denotes the chapter and index of the program in that chapter. For example, program 2.5 refers to the fifth program of chapter two.

This isn't really a story book that you can just read trough and think you are done. To actually learn the materials of the book, you need to type in every single program whenever you come across it in the book, and check the output. When in problems, don't skip it. Try to find out where things could have possibly gone wrong. And don't take too long to realize that its actually fun!

I hope you will enjoy reading all the chapters with patience and typing in all the programs on your machine. Best wishes.

No comments:

Post a Comment