index | submit | rank | book

Computer Science by Example

<< 1. Introduction   |   2. Setting up what you need   |   3. Programming Basics >>

2. Setting up what you need

In this chapter, you will set up what you need to start learning Computer Science and programming: you need to have a computer and an operating system installed; you need to know how to deal with files, directories and the command line; then you need to choose and install a programming language. Feel free to skip or just skim through the text of topics you are already familiar with. ◻

This book already assumes some familiarity with using a computer. ◻

If you are working through this book as part of an Academic programme, a programming environment may have been already set up for you in the laboratory of your institution. Check with you Lecturer, Teacher or Teaching assistant. You can nevertheless use this chapter to set up an environment on your personal computer. ◻

2.1. Choosing a computer to work on

You are expected to be at a computer while reading this book. Ideally you should also have internet access so you can check your exercise solutions in the CScx Online Judge. ◻

Now, on what computer you should be working?

Ideally you should be using a personal computer: one with a keyboard and a screen. This could be a desktop computer or a laptop. As programming is typing intensive a physical keyboard can make a difference.

(In North America, “Personal Computer” or “PC” is often used in contrast with Apple “Mac” computers. In reality Macs can be classified as personal computers as they are computers designed for individual use. Feel free to use a Mac if you like.)

You do not need something expensive or even brand new. At least at first, programming is not a very demanding job in terms of computer processing power. If you are looking for something cheap, use an old computer or attach a monitor, keyboard and mouse to a Raspberry Pi.

Less ideally, you could follow this book on a tablet computer with a physical keyboard attachment. In the worst case, you could use a smartphone but only as a last resort solution. It can be tricky to setup a development environment on tablets and mobile phones as this is unusual. Again, use an actual desktop or laptop computer — even an old device will do. ◻

2.2. Choosing an operating system

An Operating System is the program that starts, or boots, after you turn on your computer. You can follow this book on virtually any operating system so long as it supports a programming environment. ◻

Your choice of operating system may have already been “made” by the computer you have available as in general computers are sold with pre-installed operating systems.

If you do not already have an operating system or are looking for something free and user-friendly: you can use Ubuntu Linux or Linux Mint. You can download, use and redistribute them for free. The installation instructions are on their website. ◻

Later in this chapter, there will be instructions on how to set up a programming environment on Linux and Microsoft Windows. ◻

Exercise. Play around with your operating system a bit if you have not done so already. Make sure you know how to open a few different applications such as: a plain text editor, a rich text editor, a drawing program, a calculator, etc. ◻

2.3. Dealing with files and directories

We can use files to store information on a computer. Files can contain text, music, images, programs, etc. Each file has a name and contents. By convention a file name is usually followed with an extension which indicates the file type:

We can use directories to organize files in a computer. Directories do not have extensions but they can contain directories within themselves. Directories are represented as folders graphically. ◻

Graphical view of Files and Directories

The figure above shows a Graphical User Interface (GUI) view of a directory with three files and one subdirectory using Thunar on Linux. Be aware that some systems hide extensions by default on GUIs. (The acronym GUI is pronounced “gooey”.) ◻

Exercise. Create an plain text file on your system. You can do this in several ways:

2.4. Using the command line or the shell

Besides GUIs we can also interact with systems through the command line or shell. This is important as we will be writing command line programs in a couple of chapters.

The command line is a textual interface. You type in a command and below this command you see a textual reply. ◻

The shell (Linux / BSD / Mac / Solaris / POSIX)

On Ubuntu Linux you can access the shell by opening the Terminal application. In general, you can access the shell by opening any terminal emulator program: xterm, urxvt, gnome-terminal, xfce4-terminal, konsole. One of these is probably pre-installed in your Linux system. ◻

After opening the shell, you can execute commands and run programs. The following table shows a few commands and their meaning.

Shell command meaning
ls list the contents of the working directory
pwd print the working directory
cd directory change the working directory to <dir>
cd .. go up a directory in the directory tree
cat file.txt display the contents of <file> in the screen

The above commands also work on Mac OS. ◻

Below is an example session on the shell. Lines beginning with a dollar sign ($) are followed by user input.

$ pwd
/home/user

$ ls
cscx  music  pictures  log  txt

$ cd cscx

$ pwd
/home/user/cscx

$ ls
subdirectory  picture.jpg  'Bourrée in E minor.mp3'  shopping-list.txt

$ cat shopping-list.txt
milk
eggs
flour

Exercise. Try to replicate something similar to the above on the Shell. Use ls to discover files and directories and cd to change directory. Try to locate a text file and display it on the screen. You should only need to type what comes after $. ◻

The Windows command line

On Microsoft Windows you can open the Windows command line by:

After opening the command prompt, you can execute commands and run programs. The following table shows a few commands and their meaning.

Windows command meaning
dir list the contents of the current directory
cd shows the current directory
cd directory change the current directory to directory
cd .. go up a directory in the directory tree
C: change to the main disk drive
D: change to the secondary disk drive
type file.txt show contents of file.txt in the screen

Below is an example session on the Windows command line. Lines beginning with > are followed by user input. The other lines are the output by the system:

> dir
Directory of C:\users\user  
31/12/1970  11:59 PM  <DIR>   cscx
  1/1/1970  00:00 AM  <DIR>   Desktop
  1/1/1970  00:00 AM  <DIR>   Downloads

> cd cscx

> dir
Directory of C:\users\user\cscx
28/07/1750  11:59 PM          Bourrée in E minor.mp3
31/12/1970  11:59 PM          picture.jpg
31/12/1970  11:59 PM          shopping-list.txt
31/12/1970  11:59 PM  <DIR>   subdirectory

> type shopping-list.txt
milk
eggs
flour

Exercise. Try to replicate something similar to the above on the Windows Command Line. Use dir to discover files and directories and cd to change directory. Try to locate a text file and display it on the screen. You should only need to type what comes after >. ◻

2.5. Choosing a programming language

A programming language is the language in which we can explain a computer what do to. A computer program is written in a programming language. ◻

If you ask seasoned programmers, they likely will have strong and diverging opinions on which programming language should be the first that someone learns. That does not matter too much: whether you start with Python, C, Haskell or any other language, the skills you learn are transferable. Eventually learning several programming languages is more important than which language you start with. However, if you are a first time programmer, do not try to learn more than one programming language at once. First learn a language, be comfortable with it, then learn another. ◻

In this book, we take an unusual approach: the decision of which language to start with is left to the reader. This book offer three choices: Python, C and Haskell. This section talks a bit about each of them to help you decide.

language C Python Haskell
runtime compiled interpreted both
object oriented no yes no
paradigm imperative imperative functional
level low-level high-level high-level
typing static dynamic static

The above table summarizes differences between C, Python and Haskell. Some of the terms used in the table are not defined until later in this book. ◻

Python is a high-level language that is interpreted and object oriented. Of the three choices offered in the book, it is the easiest to learn. Python is a popular choice as an introductory CS teaching language. If you are unsure, Python is not a bad choice. ◻

C is a low level language, it is closer to machine-readable code. In C one can exercise low level concepts unavailable in Python or Haskell such as pointers and manual memory allocation. C was very influential in its syntax. Many widely used languages, some of these very different from C, use a C-like syntax, such as: C++, C#, Java, JavaScript. ◻

Haskell is a functional and lazy high-level language. Haskell’s syntax helps one write mathematical functions quite elegantly. Of the three choices offered in the book, it is the most unusual as a first programming language. Haskell can be a bit harder to grasp at first but it can improve the way one thinks about programming. ◻

Similarities. C, Python and Haskell do have their similarities. They are general purpose programming languages, in other words, they were not designed to be restricted to a certain niche. They are also portable, the same program can be compiled to or interpreted in a variety of systems. ◻

Examples. Here is an example in each of the three languages.

The following Python function computes the GCD of two integers:

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

The following C function computes the GCD of two integers:

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    else
        return gcd(b, a % b)
}

The following Haskell function computes the GCD of two integers:

gcd :: Int -> Int -> Int
gcd a 0  =  a
gcd a b  =  gcd b (a `mod` b)

Throughout the book, you will see boxes like these. You should read the one of your chosen language and skip the other two. ◻

Other languages. The examples in the book are in C, Python or Haskell, so, if this is your first time learning programming, stick to one of these three. However if you already know how to program, you can use this book to learn other languages: C#, C++, Java, JavaScript, Lua and Ruby. All of these are supported by the CScx Online Judge. ◻

If you are using this book as part of an academic programme, your instructor may require you to use specific language or pick a language from a restricted set. ◻

2.6. Installing a compiler or interpreter

To start programming, you need either a compiler or an interpreter. A compiler transforms human-readable code into machine-readable code producing an executable program. An interpreter executes human-readable code directly. ◻

In this section you will find information on how to install compilers or interpreters for C, Python and Haskell on Linux and Microsoft Windows. You do not need to install all of them, just the one you will be using. Please jump to the appropriate section with the instructions for your chosen language and system.

If you have difficulty doing this on your own, try to find someone with experience to help. Here is a hint: on Linux we install software using a package manager, on Windows we install software by downloading and running the installer from a website. ◻

Installing a C compiler on Linux

To compile C programs on Linux you can use GCC, the GNU C Compiler. ◻

To test if GCC is installed, run the following on the command line:

gcc --version

If you get a version number along with copyright information, GCC is already installed. If you get instead “command not found”, you will need to install GCC – see the following paragraphs. You will need root privileges. ◻

Installing GCC on Debian or Ubuntu. On Debian and Ubuntu run the following on the command line to install GCC:

apt-get install gcc

Then use gcc --version to check if the installation was successful. ◻

Installing GCC on Arch Linux. On Arch Linux, run the following on the command line to install GCC:

pacman -S gcc

Installing GCC on Red Hat, CentOS or Fedora. On Red Hat and CentOS systems, run the following on the command line to install GCC:

yum -y install gcc

Installing Python on Linux

To run Python programs on Linux, we need the official Python interpreter. Python version 3 is recommended.

To test if Python is installed, run the following on the command line:

python --version

To test if Python 3 is installed, run the following on the command line:

python3 --version

If you get a version number along with copyright information, Python is already installed. If you get instead “command not found”, you will need to install Python – see the following paragraphs. You will need root privileges. ◻

Installing Python on Debian or Ubuntu On Debian and Ubuntu run the following on the command line to install Python 3:

apt-get install python3

Then use python --version to check if the installation was successful.

Installing Python on Arch Linux On Arch Linux, run the following on the command line to install Python 3:

pacman -S python

Installing Python on Red Hat, CentOS or Fedora. On Red Hat, CentOS or Fedora, run the following on the command line to install Python 3:

dnf install python3

Installing a Haskell compiler on Linux

To compile Haskell on Linux you can use GHC, the Glasgow Haskell Compiler.

To test if GHC is installed, run the following on the command line:

ghc --version

If you get a version number along with copyright information, GHC is already installed. If you get instead “command not found”, you will need to install GHC – see the following paragraphs. You will need root privileges. ◻

Installing GHC on Debian or Ubuntu. On Debian and Ubuntu, run the following on the command line to install GHC:

apt-get install ghc

Then use ghc --version to check if the installation was successful. ◻

Installing GHC on Arch Linux. On Arch Linux, run the following on the command line to install GHC:

pacman -S ghc

Installing GHC on Red Hat, CentOS or Fedora. On Red Hat, CentOS or Fedora, run the following on the command line to install GHC:

dnf install ghc

Installing a text editor on Linux

Besides a compiler, you also need a plain text editor. The gedit text editor is a simple and popular choice that comes pre-installed on Ubuntu: search for “gedit” or “Text Editor” on the application menu or simply execute gedit on the terminal. ◻

In case gedit is not installed, do the following steps:

Installing gedit on Debian or Ubuntu. On Debian and Ubuntu run the following on the command line to install gedit:

apt-get install gedit

Installing gedit on Arch Linux. On Arch Linux, run the following on the command line to install gedit:

pacman -S gedit

Installing gedit on Red Hat, CentOS or Fedora. On Red Hat, CentOS or Fedora, run the following on the command line to install gedit:

dnf install gedit

Other choices. Other popular choices of easy-to-use text editors are: kwrite and geany. Other popular choices of not-so-easy-to-use text editors are: vim and emacs. You can install these using a package manager. ◻

Installing a C compiler on Windows

To compile C programs on Windows we can use the GCC compiler bundled with Code::Blocks or Visual Studio Community. This section provides instructions on how to intall Code::Blocks. ◻

You can download the Code::Blocks installer from the Code::Blocks website. First click on “Downloads” then on “Download the binary release”.

Code Blocks webpage

Download the “codeblocks-20.03mingw-setup.exe” installer. The website has several options, it is important to choose the one that comes with “MinGW” thus including the compiler.

Code Blocks installation wizard

Execute the installer and follow the install instructions. Select a “Full” installation when prompted.

Code Blocks on Windows

If your installation is successful, you should be able to open Code::Blocks from the start menu. ◻

Code Blocks on Windows with a source file open

Code::Blocks is an integrated development environment. You can use it to write and compile programs. You can use the file menu or icons to create new programs and to open existing programs. You can build and run your programs using the “cog” and “play” icons or using the build menu option at the top which are active when a program is opened. ◻

Installing Python on Windows

Python for Windows can be freely downloaded from Python’s official website. Click on “Downloads” then on “Windows” to reach the “Python Releases for Windows” webpage. Python 3 is recommended.

Python Website

From the “Stable Releases” section, download the latest “Windows x86-64 executable installer” for Python 3.

Python Installation Wizard

After downloading the installer, execute it and follow the automated installation steps. When prompted, make sure you check the box “Add Python 3 to PATH”, so you are able to run Python from the command line, then click “Install Now”.

Python's IDLE

By default, Python comes with IDLE, Python’s Integrated Development and Learning Environment. You can use it to write your Python programs in. You can find it on the Windows start menu.

Python's IDLE with a source file loaded

Later when solving exercises from the book, you can run Python programs directly from IDLE using the “Run” menu item.

Check if your installation was successful by opening IDLE or by running python from the command line:

> python --version
Python 3

Installing Haskell on Windows

To compile Haskell programs on Windows, we can use the GHC compiler bundled with the Haskell Platform. ◻

Haskell Webiste

To install Haskell on Windows, visit the Haskell Platform releases page. Download and execute the latest Haskell Platform Installer and follow the installation instructions. When prompted you can unmark “Stack” to make the installation go faster.

Haskell Platform Installer Wizard

If your installation was successful, you should be able to run ghc from the command line:

> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6

You will also need a text editor to write your programs in. You can use Notepad++ or Visual Studio Code. Both are available for free. ◻


<< 1. Introduction   |   2. Setting up what you need   |   3. Programming Basics >>

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
All rights reserved