Starting from:

$30

Homework 01 ECE 449/590

Homework 01
ECE 449/590
1. (20 points) (Exercise 1-1 and 1-2)
A. Are the following definitions valid? Why or why not?
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
B. Are the following definitions valid? Why or why not?
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
2. (20 points) The assignment operator = works with two operands L and R in the
form L=R. For the following code to generate an output of 3 3 3 3, what should
be the associativity of = and what should be the result and the side effects of L=R?
int a(0), b(1), c(2), d(3);
a=b=c=d;
std::cout << a << " " << b << " " << c << " " << d << std::endl;
3. (20 points) (Exercise 4-8)
If the following code is legal, what can we infer about the return type of f?
double d = f()[n];
4. (20 points) (Exercise 6-3 and 6-4, also see Chapter 6.1)
A. The following program attempts to copy from u into v. What’s wrong?
std::vector<int> u(10, 100);
std::vector<int> v;
std::copy(u.begin(), u.end(), v.begin());
B. Correct the above program. There are at least two possible ways to correct
the program but you are only required to implement one.
5. (20 points) Suppose integers is a container with int elements. Implement a
function to sort integers from the largest to the smallest. (Hint: use std::sort.)
1

More products