Lab 1
Lab 1
Image courtesy: https://www.geeksforgeeks.org/binary-tree-data-structure/ Nodes without children are called leaf nodes or leaves.
Preliminary: Directory trees
root/directory1/directory2/…/fil
ename
You might have come across these on your computer in the form
C:\Users\Person1\ilovecp.txt
The root directory for Windows is in the form drive:\ but for Linux it is simply /
Preliminary: File Paths
But sometimes when you
are already in a directory,
you might want the path Absolute filepath of
from the current directory Handout.pdf
to the file you are looking
for. This is called the
relative file path of the
file to the current
directory.
Relative filepath of Handout.pdf
to Courses
A double period (..) refers to the parent directory of the current directory. For
example, when your current directory is Courses, .. refers to My_second_sem,
../Clubs refers to My_second_sem/Clubs, and so on.
This is done using text-based inputs called command lines that achieve some
specific task.
The kernel then works closely with your computer’s hardware to perform the
specific task specified by the command line.
For this session you only need to be concerned with the terminal.
The structure of a command line
When you first open a new terminal, it gives you a command prompt, a piece
of text usually ending with a $, > or % (depending on your shell) that indicates
it is ready to accept a new command line. A prompt is also given after every
subsequent command line is executed.
Recall that a tilde (~) refers to the
logged in user’s home directory.
The tilde in the image shows that
your current directory is the
home directory. Your command
Command Prompt
goes here
The structure of a command line
Every command line follows a standard structure.
The ls command can also be used to list the items in a directory other than the working directory.
For this, you need to also provide the relative path of the other directory to the working directory as
an argument (input) separated by a whitespace.
For example, the relative path of Courses to My_second_sem is ./Courses. Using ls ./Courses,
you can list the items in Courses while still being in My_second_sem!
Commands: cd
Sometimes, you might want to change the working directory itself. For this, you
need to use the cd command. The argument is the relative path of the directory
you want to change to. For example, here, we will change the directory from
My_second_sem to Clubs.
There is a problem: When there is a space in the name of the directory, the two words
separated by the space are read as separate arguments.
To fix this precede every space with a backslash or enclose the name in double
quotes
Commands: rmdir
The rmdir command (short for remove directory) is used to remove an
empty directory from the working directory. The name of the directory to be
removed is the argument.
Commands: touch, cat and rm
To make a new file inside the working directory, use the touch command. The
argument is the name of the new file with its extension.
To view the file, use the cat command (short for concatenate) followed by the
complete filename.
To remove the file, use the rm command (short for remove) followed by the complete
filename. Be very careful while using rm!
Commands: chmod
chmod is a command that lets you change the permissions of a file or directory to all
types of users.
Here’s the syntax of the chmod command:
chmod <Operations> <File/Directory Name>
You can grant or revoke the permission by replacing the Operations in the above
command.
These operations need to be preceded with a '+' or '-' operator.
The above command adds read permission for the sample.txt file.
Commands: gcc
GCC, the GNU Compiler Collection, works together with a few other programs to compile the C
source code file that you can understand into an executable file (Windows - .exe; Linux, macOS -
.out) specific to your computer’s architecture.
The name of the executable file can be chosen by adding the option -o followed by a complete
name for the file.
The main arithmetic operators are the addition (+), subtraction (-),
multiplication (*), division (/) and modulo (%) operators.
The assignment
statement x=2 is always
true, so the statement
y=5 will always be
executed
Understanding
nested if else
statements
EXAMPLE
What if I want to use multiple if else statements instead of nested if else statements?
Use of logical operators in if-else statements
1. LOGICAL AND (&&)
OPERATOR
if (condition-1 || condition-2)
{
statement-1;
}
else
If either one of the conditions, i.e.
{
statement-2; Condition-1 and Condition-2 is
} true