tags:

views:

179

answers:

1

Hey guys, I have a CS exam tomorrow. Just want to get a few questions cleared up. Thanks a lot, and I really appreciate the help.

Que 1. What are parallel vectors?

  1. Vectors of the same length that contain data that is meant to be processed together
  2. Vectors that are all of the same data type
  3. Vectors that are of the same length
  4. Any vector of data type parallel

Que 2. Arrays are faster and more efficient than vectors.

  1. True
  2. False

Que 3. Arrays can be a return type of a function call.

  1. True
  2. False

Que 4. Vectors can be a return type of a function call.

  1. True
  2. False
+7  A: 

Question 1
The term "parallel vector" is non-standard... (to me, it means that the dot product of their directions is 1!), so you will need to look at your notes and see what the teacher's own meaning of "parallel" is.

Question 2
This is a tricky question. Array construction (of primitives w/o initialization) is faster and more efficient than vector construction (because vectors will initialize their contents). However, if you are just passing around vectors by constant reference and using the subscript operator to access their content then there is no difference in efficiency (those subscript operations are inlined and don't perform any bounds checking). Best ask your teacher, because this is arguably not something that can be presented as a true/false question.

Question 3
Your teacher really likes trick questions, it would seem. No, you cannot return a fixed-sized array from a function; however, you can return an array as a pointer to the first element of a heap-allocated array. Most likely your teacher intends the answer to this question to be false, but the nuances are important.

Question 4
True. This is the only trivial question in this list.

Michael Aaron Safyan
You can also return references or pointers to (stack-allocated) arrays, only directly returning by value doesn't work.
Georg Fritzsche
@gf, yep, that's correct.
Michael Aaron Safyan
thanks a lot, Michael.
xbonez
And you can return `boost::array` or C++0x `std::array`… hmm, somehow it doesn't feel so dirty when they aren't asking conceptual questions…
Potatoswatter
re #2: Array construction is only faster for built-ins and only if the objects aren't initialized.
sbi
@sbi, that's correct... which is why it is a trick question.
Michael Aaron Safyan
#2 I'd say no. I think this question is to remind that going low level, doesn't mean you'll automaticaly get better performance (as still too many people think...). Answering no, isn't saying vectors are faster.
Tristram Gräbener
@Tristram: You'd think that, if the instructor was a good C++ programmer. Sadly, not all of them are.
sbi
@Tristram, I agree, which is why I said it is a trick question; for pretty much everything, you get the same performance (and you get much better, less buggy code with std::vector); however, std::vector may be slightly less efficient for certain construction operations (although the equivalent construction... i.e., if you had a loop that initialized all the elements, would not be any different).
Michael Aaron Safyan