CS Week 5- Arrays and Intro to Pointers Flashcards
array
an ordered list of items of a given data type
element
each item in an array
declaration of an array
dataType arrayName [numElements];
index type
int type only
to check if index is valid
if (nth >= 1 && nth <= 5) { }
iterate how many times through a loop
the size of the array that you should keep track up
array initilization
int myArray[3] = {5, 6, 7};
array vs vector
declaration and accessed and differences
Array -
Declaration: int myList[#];
Accessed: myList[i];
Vector-
Declaration: vector<int> myList(#)
Accessed: myList.at(0);</int>
Arrays have simpler syntax but do not have .size() feature
Vectors are safer
an array as a paramter
uses the [], but argument does not
passes in an array
reversing an array
loop numElements/2 times
tempVal = a[i];
a[i] = a[numElements - 1- i];
a[numElements - 1-i] = tempVal;
two dimensional array
int myArray [R][C]
row major order
compiler maps 2-dimensional array elements to one-dimensional memory, each row following the previous row
two-dimensional array initialization
int nums [2][3] = { {2,4,6}, {5,6,7} };
Or to make it more visible
int nums [2][3] = {
{2, 4, 6},
{5, 6, 7}
};
access row n and column m
array[n-1][m-1]
pointer
a variable that holds a memory address (rather than holding data)