Final Exam Review#
Note
The final exam will cover the topics since the previous exam.
Python will be part of the exam, however, there will not be questions on NumPy and matplotlib, since we did not have enough time to practice those.
Important
This set of examples is not exhaustive, so you should go back and read through the notes, run the example codes, and look at my homework solutions.
File I/O#
You create a file as:
std::ofstream of("file.txt");
How do you output a string to it?
If the file already exists, what happens to its existing contents when you open it this way?
How would you change this line if you wanted to read from
file.txt?
Standard Library Algorithms#
Given a
std::vector<int> v{2, 5, 8, 5, 10};, write one line usingstd::ranges::countto count how many times 5 appears.What does
v.end()point to? Why should an iterator loop usually stop at it< v.end()or it!= v.end()instead of includingv.end()?Use
std::ranges::findto search for 8 in a vector. What should you check before dereferencing the returned iterator?Write a lambda that returns true if an integer is odd. Then use it with
std::ranges::count_if.For
std::views::iota(3, 8), what values are produced?Suppose an iterator
pospoints to the element 200 in a vector. What doesv.insert(pos, 150)do?In this example, we use a lambda function. If we instead wanted to use a traditional function, how would we rewrite this (show both the new function and the updated
sortcall).std::ranges::sort(titles, [] (const std::string& a, const std::string& b) {return a.size() < b.size();});
Software Engineering / Version Control#
These lines are in a
GNUmakefile:planet_sort_split.o: planet_sort_split.cpp planet.H g++ -std=c++20 -c planet_sort_split.cpp
In the first line, what does the name to the left of the
:mean? and what are the files to the right of the:?What does the
-cdo in theg++command?
If I compile a
program.cppas:g++ -O3 -o program program.cppwhat is the meaning of
-O3?git statusshowsnewton.cppunderUntracked files—how do you put this file under git control?What does it mean for a variable to be shadowing another variable in C++?
We have a header
solver.Hin our current directory. This header provides a functionnewtonin it. We want to use this in our code. How do we include this header so the compiler will find it.
Classes#
What is the main difference between a
structand aclassin C++?What is a constructor, and when is it called? What name is given to the constructor function?
What is an initialization list? In the code below, what member data is initialized?
SolarSystem(double mass) : star_mass(mass) {}
Consider the following:
struct Circle { // member data double radius{}; // constructor Circle(double r) { radius = r; } };
How would you create a
Circlewith a radius of5?Rewrite this to make the member data private and add a getter function that returns the radius.
Show how you would call this function given a
Circleobjectc.How would you add another constructor that takes no arguments and sets the radius to
1by default?
In our
Vector2dclass, we had 2 different operators for multiplying by a scalar:Vector2d operator*(double a) { return Vector2d(x * a, y * a); } friend Vector2d operator*(double a, const Vector2d& v);
What is the difference between these?
Write a class
Timerwith private member datasecondsand a member functionadd_time(double dt)that addsdtto the storedseconds, and another member function,get_time()that returns the stored time.Currency exchange.
Create a class called
Currencythat holds adoubleand astring. Thedoubleis thevalueand thestringis thecountryof the currency (e.g.,"US","Euro", …). Give a constructor that takes both pieces of data.We want to be able to add two
Currencyobjects. The operator function will look like:Currency operator+ (const Currency& other)
Fill in the details—include a check that we are adding currencies from the same country.
Python#
What is the python version of a C++
std::vectorthat we focused on in class?Given the following:
a = [1, 2, 4, 8, 16]
write a loop (in python) that loops over the elements and prints them out.
Some times in class, I put an
fin front of the"in a string—what does this mean?How would I write this C++ if-test in python?
int sign; if (x == 0) { sign = 0; } else if (x > 0) { sign = 1; } else { sign = -1; }
What is the python equivalent of this C++ code
double x{2.5}; double y{1.2}; double z = std::pow(x, y);
What is the result of the following python code:
def func(x, normalization=None): xi = x if normalization: xi = x / normalization return xi**3 + xi + 1 func(2, normalization=2)
This was the first class we wrote in C++:
struct Circle { // member data double radius{}; // constructor Circle(double r) { radius = r; } // member functions double circumference() { return 2.0 * std::numbers::pi * radius; } double area() { return std::numbers::pi * radius * radius; } };
write a python version of it.