2024-05-14
Max. 100 points
Name:
Task | Max. | Achieved |
---|---|---|
1 | 20 | |
2 | 20 | |
3 | 20 | |
4 | 40 | |
Sum | 100 |
Statement | True | False |
---|---|---|
C++ allows procedural, object oriented, generic and functional programming. | ||
In C++, the scope resolution operator ::
allows accessing identifiers within a given namespace. | ||
A C++ vector is a container that stores
key / value pairs. | ||
A C++ compiler translates high level code to a lower level (assembly language). | ||
The assembler combines object files into a unified executable program. | ||
It is recommended to use magic-based for-loops wherever possible. | ||
A function declaration serves as a promise to the compiler that the function will exist when linking. | ||
using namespace std is a good practice in
C++ programs. | ||
It is impossible to write C++ close to the fundamental aspects of hardware like C. | ||
Range-based for loops provide easy access to each element in a container. |
int main(.)
(unless main is part of the snippet).
Assume all neccessary
#include
s to be present.stdout
?
Write their exact output below each snippet.
int val(123);
std::cout << "Hello " << val() << std::endl;
std::vector<int> v{1, 2, 3};
for (auto it = v.rbegin(); it != v.rend(); ++it) std::cout << *it << " ";
std::cout << std::endl;
int i{3};
int& j{i};
int k{i};
j = 5;
std::cout << i << ' ' << j << ' ' << k << std::endl;
char name[] = "Sue";
char* n = name;
while (*n) {
std::cout << *n;
n++;
}
int a = 10;
int* p = &a;
int** pp = &p;
**pp *= **pp;
std::cout << a;
std::size_t str_len(char* str);
that returns the number of characters in str
excluding the
trailing '\0'. Do not use any library functions. (20 points)
std::size_t str_len(char* str) {
std::size_t len(0);
while (*str++) {len++;}
return len;
}
struct
that stores the name of a book, the
number of its pages and its price. Use appropriate data types for each
member. Then implement a function
double total_price(std::vector<Book> books);
that returns the total price of all books in books
.
Assume that while all required includes are present, no
using
directives were used.
(40 points)
struct Book {
std::string name;
uint32_t pages;
double price;
};
double total_price(std::vector<Book> books) {
double total = 0.0;
for (const auto& book : books) {
total += book.price;
}
return total;
}