Lab 2

Concerns and Grading Format

Common problems:

  1.  Indice misunderstanding (-10 pt, major misunderstanding; -15 pt, can't compile)

Basically the problem here is that you assume the elements in a vector starts at 1, not 0.  Example:

WRONG (for accessing the first element of a vector):
vector<int> num;
num[1] = 1;

CORRECT (for accessing the first element of a vector):
vector<int> num;
num[0] = 1;

  2.  Loop usage for vectors (-5 pt, minor problem; -10 pt, major misunderstanding)

I got a variety of misunderstandings for this one.  Some of it was just trying to stick with the while loop and modifying the while loop without success.  The other one was badly constructed for loops or just basic iteration problems.  Then there were people who just didn't do the loops outright.

The way that you want to do loops for iterating through a vector is based on a few basis that we can work from.  The first is that vectors start at the 0th index and goes to size of vector minus 1.  The size function in vector will tell you exactly how many elements are in the vector so basically you want to iterate to the size-1th element.  Here is a good example of a while loop that does it:

vector<int> num;
... //some code that basically added
... //
some number of elements into num
int i = 0;
  cout << num[i] << endl;
} while ( i < num.size() );

Ok, if you want to be a real code ninja you can do it like what the real programmers do and do this:

vector<int> num;
for (int i = 0; i<num.size(); i++)
  cout << num[i] << endl;

  3.  When you haven't allocated room for an element, you can't access it. (-10 pt, major misunderstanding; -15 pt, did not compile)

Ok, the problem here is that if the vector is not that big, you can't access the stuff that is not in the vector.  What that sums down to is that the size of the vector is n and you are trying to get at the (n+m)th element.  Which means that if you have size of 8, you can't access element 100.  Here is a good example of what I mean:

WRONG (accessing non-existant elements):
vector<int> num(10); //contains element 0-9
num[100] = 23; //accessing an element that is out of bound

CORRECT (creating new element):
vector<int> num(100); //this contains element 0-99
num.push_back(23); //makes a new element, element 100

//you can also resize but that's more complicated