In-Class Review I#
This is a review of some of the concepts we covered up to this point in the semester.
Important
This set of examples is not exhaustive, so you should go back and read through the notes and run the example codes.
Bash shell#
You log into a computer–how do you find out what directory you are currently in?
solution
pwdThe
pwdcommand stands for print working directory.How can you return to your home directory from anywhere on the filesystem?
solution
You can simply do:
cdcdwith out any arguments always brings you home.You could also do:
cd ~or give
cdthe full / absolute path to your home directory.What is the full command that will transfer a file from your home directory on
portalto your current directory on the desktop computer you are sitting at in the MathLab?solution
We use
scp(secure copy) for this:scp username@portal.mathlab.stonybrook.edu:~/file .That will transfer a file called
filein your home directory (~/) onportalto the current directory (.) on your local machine. You would substitute your NetID forusername.Here’s an example filesystem on a computer you are using:
/ ├── etc ├── home │ ├── common │ ├── system-admin │ ├── teacher │ └── user │ └── files │ ├── report1.txt │ ├── report2.txt │ └── report3.txt └── opt
Your home directory is
userWhat is the full (absolute) path to the file
report1.txt?solution
/home/user/files/report1.txt
Remember: absolute paths begin at the root of the filesystem (
/).If you are in the
files/directory, how do you copy all of the files there to thecommon/directory? Give a single command for this.solution
We would use the copy command,
cp. There are several variations of how we could specify the source files and the destination. You could do:cp report1.txt report2.txt report3.txt /home/commonWith
cp, the last argument on the command line is always the destination. Here we explicitly write out the 3 files we wish to copy.We could also use wildcards, like:
cp *.txt /home/commonIn this case,
*.txtwill match any file in our directory that ends in.txt.Using wildcards, what is a single expression that matches all of the files under
files/?solution
In addition to
*.txtthat we used above, we could do any of these:*: this matches every file in the directory. In this case, this is okay, since we want all the files. But usually that is not the case.report*.txt: this will match any file that starts withreportand has the.txtextension.report?.txt: this will match any file that starts with report, then has a single character (can be a letter or a number) and then ends in.txt.
You want to delete the directory
files/and everything in it. How do you do this?solution
Starting in your home directory, you can do:
rm -f filesThis will recursively delete the directory, by first deleting all the files in it, and then removing the directory. This can be dangerous if you did not setup the
aliasforrmto always berm -i(prompt you before deleting).Another way would be:
cd files rm *.txt cd .. rmdir files
This first changes directory into
files, then (using a wildcard) removes all the files, then changes directory back up a level (..) and then usesrmdir, which only works on an empty directory.
You have a file on a group server (
directory.txt)–how do you make sure that anyone on the machine can read it, but only you can write to it?solution
You can do this in a few ways. One way is:
chmod a+r directory.txt chmod a-w directory.txt chmod u+w directory.txt
The first two lines set the permissions for everyone (user, group, and other), while the last line restores write permissions for the user.
What do you expect the output of the following command sequence to mean?
grep -i yellow database.txt | wc -lsolution
This will return the number of lines in
database.txtthat contain the wordyellow, without worrying about case.Suppose you wanted to see the last 10 commands you entered at the Bash prompt–what command sequence could do this?
solution
You could do:
history 10or if you didn’t realize
historycould take a number (like me!), you could do:history | tail -10You have an executable in your current directory called
hello. If you just do:helloyou get an error (“command not found”). How do you run this.
solution
You need to give the path to the executable. A relative path works, so you can just do
./helloHere, the
./means “in this directory.What command do I use to search for all instances of a particular word in a file?
solution
We use
grep.
C++ basics#
Suppose I wanted to store the number
0.3with the most precision possible–what C++ data type would I use?solution
Since this number has a decimal point, we need a floating point type. We know
floatanddouble, butdoublehas more precision (it is double precision, or 64-bit floating point).How can I compute \(\sqrt{5.0}\)? Is there a particular header file I would need to include?
solution
We could do:
std::sqrt(5.0);
We could also do:
std::pow(5.0, 0.5);
In either case, we need to include the
cmathheader.How do I compile a program called
power.cppto make an executable namedpower?solution
We do:
g++ -o power power.cppWhen I write
std::cout, what is the meaning of the::?solution
This is the scope operator. It means “look inside
stdto findcout”. We callstdthe namespace.Consider this code:
double x{1.5}; double y = 2.0 + x * 5.0;
what is the value of
y?solution
The thing to remember here is that multiplication,
*, has higher precedence than addition,+. So this is2.0 + (1.5 * 5.0)or9.5.Consider this code:
int x{2}; int y = 2 * x * x / 3;
what is the value of
y?solution
All operators here have the same precedence, so we go left to right. Just computing this we would get
8/3, but the result is anint, so the decimal part is discarded, and we get2.Consider this code:
int x{2}; int y = 2*-x;
what is the value of
y?solution
Here, the
-is the unary minus or “negation operator”. It simply changes the sign ofx, and it has the highest precision here. So this is equivalent to2 * (-2).Every C++ program needs a function with what name?
solution
mainConsider the following code:
double x{1/3}; std::cout << 3 * x << std::endl;
what value is output to the screen?
solution
The key here is that
1/3is integer math, so this evaluates to0, and as a result,0is output to the screen.Consider the following code:
double x{1.0}; double eps{1.e-30}; bool y = (x + eps) != 1.0
what value would you expect
yto have?solution
Compared to
1.0, the number1.e-30(or \(10^{-30}\)) is very small, and it is below the roundoff precision, so we would get1.0 + 1.e-30 = 1.0. (Remember that machine epsilon is ~1.e-16).The
!=relation operator is not equals, so this is effectively asking if 1.0 is not equal to 1.0, and the answer isfalse, soygets set tofalseor0.