I was reading a text that I found on a portal on the Gopher network and I thought "hey, this is interesting, I'll take it to the people at Odysee". So I started translating the texts little by little and making slight modifications to the point where I no longer had the original text in hand, but something different. In practice I ended up spending two weeks of my life to translate two chapters, and now I'm going to try to bring the chapters shorter and adapted to our reality today.
I thought about bringing all the chapters in one file, but honestly, it's going to be a pain to read all this at once. It would be like trying to read a college textbook in one night, and we all know that doesn't work. So my release plan is to translate and adapt all 15 chapters maintained by Lowe, put together my personal opinions, mix it with some current knowledge and create something different, something that will be useful for someone just starting out.
Unlike the texts provided by Lowe, however, if they are reasonably successful, I won't just stop, just introduce more and more computer science concepts as time goes by, going through technical aspects of operating systems, artificial intelligence and several others.
Of course it won't be an easy task, much less simple, but I believe it will help many, so if this is useful to you in any way, help by sharing with your friends, tipping in the comments, commenting, engaging, donating or trying to help reach more people, to encourage me to keep going.
Thanks to Robert Lowe, who kept this 1989 computing gem alive today. You can even read the original texts here, through Floodgap's Gopher proxy.
Also thanks to Herbert Schildt for the excellent books C++: A Beginner's Guide and C: The Complete Reference and the website sololearn, which helped me clear up some programming questions to develop these articles.
With that topic finished, let's get to the text.
Special Considerations
The following text assumes that you are using Linux as your operating system. If you don't use a Linux-based system, I recommend that you switch to one or use a virtual machine to be able to carry out the proposed activities more easily.
Introduction to Unix
Introduction
This chapter will teach you a little about the terminal interface of Unix systems, including:
- Manipulate files, create, delete, rename and copy files
- Use man pages
- Organize files using directories
This is a guided lab. This means that you only need to read the instructions and repeat the commands to get the expected results.
Working with files and manuals
Open your terminal.
- Once you have opened the terminal, you will end up in your
home
system directory. Just like Windows or Mac OSX, Linux systems divide the system into directories (folders) and files, and your folderhome
, in general, will be the only part of the system where you actually mess with (and if you don't own system, the only one you will be allowed to touch). Let's see where you are now. Enter the following command:
$ pwd
The command pwdmeans something like "present working directory", and will return the path of the current folder. In your case it will be something like /home/user
.
- Another cool command is
hostname
. It shows the name of the current server or system you are connected to. - As with any system, you may not be connected to the server alone. When using the command
who
, you see a list of users currently connected to the system. - Now your screen should be completely full of junk. Type
clear
to clear it. - Now let's play around with some random parts of the interface a bit. Start by pressing the up (↑) and down (↓) keys: you'll notice that you can browse through your user's command history. Using the "erase" (backspace) key, you can change a command that is currently selected. Play around with it a little...
- Okay, time to fiddle with files a bit. Type
ls
to see the files inside the current folder. - Okay, you can see the files inside your folder, but that's not good for much, right? Display the data with more data, such as owner and change date, using the command
ls -al
:
Here we have some interesting information about the command. First, you may have noticed the .
and ..
folders, which stand for the current directory and the previous directory, respectively.
The l
in -al
stands for "list details", or "list details", and gives the file details separated in columns. The first column looks like some kind of secret hacking code, doesn't it? In fact, it is a record of the permissions of the files and their types, with the first character indicating whether it is a directory or not, and the following 9 characters indicating the permissions of the files and folders, following the following pattern:
- r → read permission
- w → write permission
- x → execute permission
There are three categories of permissions we can set for each file. The first three permission digits refer to the file owner's powers, the next three refer to the powers of a group of users, and the last three show the permissions of the rest of the people.
For example, imagine I have the following file:
-rwxr-xr-- 1 Fulano Users 0 fev 21 19:27 my-file.txt
The first digit is not a d
, but a dash (-
), which means it is not a directory. Owner permissions are rwx
, which means user So-and-so can read, change and execute the file. The group's permissions are rx
, which means that users belonging to the Users group can read and execute the file, but cannot edit it, and the permissions of the rest of the users are r--
, which means that if you are not the user So-and-so nor belong to the group Users, you will only be able to read.
An important detail to mention is that, if you want to open a directory, it must have execute permission. Otherwise, you will not be able to open it.
One of the superuser's "superpowers" is bypassing these special permissions, being able to read, edit and execute whatever they want.
- A nice thing is that if the permissions aren't too restrictive, you can view other users' data. The shortcut
~
redirects to the user's home folder, but you can use the same shortcut like this:~<username>
. This allows you to use this shortcut to redirect to a specific user's folder. Example:
$ ls ~admlab
This will show the return of the ls
command run inside the home folder of the admlab user. Look at some other examples:
- Now let's use the
~/gopher/files
folder that I created earlier (but this goes for any other folder). I'll enter it and type the commandls
:
They are text files. You probably even assumed this from the .txt
extension. But what about the last two files? They don't have an extension, do they? Unlike Windows, on Unix systems the use of extensions to define the file type is more of a recommendation than an obligation. I mean, we can still do it, and we really do, but not because we have to. However, this can end up causing some inconvenience, which is why the file
command exists. Its syntax is this:
$ file file[.extension]
Let's run the commands for each of the files:
And now we're sure the files are plain text files.
- Okay, and you want to read the contents of the file, right? For this we use the
cat
command. The syntax is:
$ cat file
Let's open the 1.txt
file as an example:
$ cat 1.txt
And the contents of the file will be printed in the terminal.
- Let's say the
2.txt
file is too big to print normally in the terminal. This is what themore
command is for. The syntax is:
$ more file
This will print the contents of the file to the screen, just like cat
. However, instead of just printing the file to the end, the command will write the content on the screen until the screen is full, then it will wait for the user to press space or enter to print more text on the screen.
- What if I want to copy one of these files from the
~/gopher/files
folder to the~/Documents
folder? This is what thecp
command is for. The syntax is as follows:
$ cp source-file target-file
Let's copy the ~/gopher/files/1.txt
file to the ~/Documents
folder. First, showing as if I'm inside the same folder as the file:
$ cp 1.txt ~/Documents/1.txt
Now in another random folder:
$ cp ~/gopher/files/1.txt ~/Documents/1.txt
You can save the copy under a different name from the original to differentiate the two:
$ cp ~/gopher/files/1.txt ~/Documents/backup.txt
And, of course, if you're not going to save it with a different name than the original, you can just pass the name of the destination folder:
$ cp ~/gopher/files/1.txt ~/Documents
- It might not be interesting to keep multiple copies of the same file, so let's remove our
~/gopher/files/1.txt
file, which is the original file. Enter the folder and run:
$ rm -i 1.txt
The -i
flag makes the remove command ask if you really want to remove the document. I put more for security, but you don't necessarily need to put this flag:
$ rm 1.txt
You can also pass the file path as we passed before:
$ rm ~/gopher/files/1.txt
And you can also remove a folder, but for that, you have to remember that, if it's not empty, you'll have to use the -r
flag, to delete the files inside it too:
$ rm -r files
Remember that this operation is irreversible.
- Damn, now we have a bunch of commands with a bunch of different options, and it's hard to remember what each of them does. So how about using a command to show what other commands do? The
man
command shows us the programs manual. Let's open the manual for thefile
command:
$ man file
This will show all the parameters of the file command, its usage instructions and other things.
Organizing directories
Now you can copy files, you can read the manual, and you know how to use your terminal for most everyday tasks. Now you need to learn how to organize your files.
- Let's start by creating a directory:
$ mkdir my_directory
- Now let's get inside it:
$ cd my_directory
You can also use the cd
command passing an absolute path:
$ cd ~/my_directory
And if you don't pass anything as a parameter it will go back to your home folder:
$ cd
- Now that we are inside
my_directory
, let's copy all the files from the~/Documents
folder to the current folder:
$ cp ~/Documents/* .
- Have fun :)
Choose your side in the editor wars and build your own legs with C++
In the previous chapter you learned the basics of a Unix system. Now it's time for you to choose a text editor so you can start coding. In this chapter, you will take your first steps in C++.
The first step will be to "choose your weapon", your text editor. A text editor is where you will spend most of your time while programming, after all, a program's code is, above all, text. There are many options on the market, but I recommend you to choose emacs or vi/vim.
After you've chosen vim... I mean, after you've chosen your editor, it's time for you to create your first program. You'll get a skeleton base program, but the code and ideas will come from you. Now, time to choose your editor.
Part I - Choosing your editor
ed
ed is one of the oldest editors to be used on Unix systems. Created in 1971 by Ken Thompson, it has some advanced features for the time and influenced editors created later. It was created at a time when Unix systems ran on a wide range of devices, including because of that you, theoretically, could easily use the ed editor without prejudice even on a "teletype" (a write connected to a computer). Because of this, ed is not a visually interactive editor, after all, how can you be interactive if what you write is going to be pasted directly on paper?
As ed is a modal text editor. This means that there are three input modes that the editor accepts, varying in form. There are three modes:
- command - commands are typed in this mode, such as
append text
oredit file
- input - you enter text in this mode
- view - the content of the file is shown on the screen. You can view the entire file, sessions of the file or just a single line
If you are interested in learning how to use ed, you can read its manual by typing man ed
in the terminal, but ed is a definitely old editor and is hardly used anymore today, so much so that it is practically only used out of historical curiosity
vi
The vi editor is an editor derived from the ex editor, which in turn is an editor derived from ed, so we can consider vi a "grandchild" of ed. vi is a screen-oriented editor, unlike its paper-oriented grandfather, and has become so popular that today there are several editors derived from it, the most popular being vim (VI iMproved ).
Like ed, vi is mode-based, consisting of two modes:
- Command - allows entering commands in the editor
- Insert - allows inserting text into the file
When vi is opened, it starts in command mode. You can switch to insert mode by pressing the "i" key, and back to command mode by pressing the ESC key. Of course, it's a little counterintuitive to do this at first, but after a few days of use, you'll get used to it.
vim
Vim is an editor derived from the old vi, but with a few extra things. I personally recommend it, as most current systems have replaced the original vi with vim among the packages installed by default on the system.
The default features of the old vi are all in vim, along with other features like adding plugins and configuring syntax.
emacs
emacs, an editor created by Stallman in 1976, was one of the first programs to be part of the GNU project. Being developed in C and lisp, emacs is one of the most extensible editors among the editors created for the terminal.
Unlike ed and vim, emacs is not a modal editor. This means that it doesn't use two different modes to perform different actions, but the same mode and key combination to perform different actions.
nano
The nano editor would be the terminal equivalent of notepad. Created to be easy to use and based on the old pico editor, the editor has only a short list of features.
cat > file
Okay, this isn't really an editor, but I thought I'd mention it as being the most minimalistic way to enter text. cat
reads a file and prints it to the screen. If you call cat with no arguments, it will just repeat what you type stdin to stdout. If you redirect this to a file, as in the case above, it will allow you to enter some text that the system will save to a file. Pressing Ctrl+D
will exit the program.
The main disadvantage here is that you have no way to change the text. Even text entered on the same line cannot be changed. If you're the type of person who gets everything done perfectly the first time, cat is your "editor" of choice!
Visual editors
After the 2000s, several visual text editors appeared, such as Notepad++. Currently, these editors have several advanced features, such as using artificial intelligence plugins to create template codes. If that's your choice, I recommend using some editors that have many advanced features and are easily extensible, such as:
- Sublime Text
- VS Code
- Atom
- IDEs such as Code::Blocks or KDevelop
Despite this, using a command-line editor will be useful at certain times, and I recommend that it be done (not least because a server accessed via SSH does not have a graphical interface).
Part II - creating your first program
- Go to the folder you want to save your program at the end
- Type
vimtutor
on your command line. This will open an interactive tutorial for the vim editor (ignore if you already know how to use it) - let's create a file called
hello.cpp
. To do this, type:
$ vi hello.cpp
- Add the following content to the file:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Hello, world!" << endl;
return 0;
}
- Save the file and exit vi
- Now let's compile the file. As we are using a Linux system, the compiler used will be g++, which is part of the gcc package. The simplest way to compile is to run:
$ g++ hello.cpp
This will save the compiled file into an executable named a.out
7. Run the code like this:
$./a.out
- Ok, but nobody wants to compile a file to have the final name
a.out
, right? To compile with the nameHelloWorld
, run like this:
$ g++ hello.cpp -o HelloWorld
And now you can rerun your program like this:
$ ./HelloWorld
Note: be careful when running this code. A very common mistake would be to type
g++ hello.cpp -o hello.cpp
, but that would replace the file where you have your code. You have to be able to run your program, but you also have to be able to keep viewing and changing your code.
- Now let's edit our program. Look at the code and add a line below the previous one to write the phrase "This is my first program" below the previous sentence.
Tip: the
cout
command is for printing messages on the screen.
- Compile and run your modified program.
Part III - Circle Area
The following exercise was extracted from Beecrowd, with some adaptations.
Problem
The formula for calculating the area of a circle is: area = π * radius²
. Considering for this problem that π = 3.14159:
- Calculate the area by squaring the radius value and multiplying by π.
Input
The program will display the message "Enter radius: ", and then ask for input. The input contains a floating point value (double precision), in this case, the variable radius.
Exit
Display the message "The area of your circle is: " followed by the value of the variable area, as shown below, with 4 places after the decimal point. Use double precision variables.
Initial tips
- Write your pseudocode showing how you intend to implement your program. It must contain:
- A list of program entries
- A list of program outputs
- The steps to be performed in the program
- Define a series of test cases (at least five). These tests should include a number of sample input and expected values.
- Use your favorite code editor and create a "skeleton" of your code, so you can understand how the program will run
- Translate this text to C++ and run it.
Step by step
You'll start by creating three variables of type float
:
float PI;
float area;
float radius;
There are several data types in C++. The int
type represents integers, for example, while the char
type represents characters. The float
type represents a floating point number, that is, a number with a comma, which is why we create our variables with the float
type.
Now let's put some data in our variables. Start by adding a line to your code that will change the PI
variable like this:
PI = 3.14159;
As we are not going to change this variable later, I prefer to put the value assignment together with the variable declaration:
float PI = 1.34359;
Now, we need to get the data the user enters. We've already seen that cout
is for outputting, that is, writing things to the terminal. But instead of writing something to the terminal, we want to get data typed into it. So for that, we'll use cin
:
cout << "Enter the radius: ";
cin >> radius;
This will put the data you write in the terminal into the radius
variable for you to use. Time to apply the formula. Assign the value to the area
variable like this:
area = PI * radius * radius;
And finally, finish writing the result to the screen:
cout << "The area of your circle is: " << area << endl;
In the end, your code will look something like this:
#include <iostream>
using namespace std;
int main() {
float PI = 3.14159;
float area;
float radius;
cout << "Enter the radius: ";
cin >> radius;
cout << endl;
area = PI * radius * radius;
cout << "The area of your circle is: " << area << endl;
return 0;
}
And that's it. Did you like today's text? Have something to add? Any tips? Comment? criticism? Tell me in the comments ☺️❤️