Exam 2 Flashcards
(33 cards)
can you change the size of an array?
no
what do arrays do?
access!
what is an element
item in an array
how do you declare an array
dataType arrayName[numElements];
how do you initialize an array
int myArray[3] = {5, 7, 9};
how do you call an array
FunctionName(arrayName, numElements);
when a function is used in a parameter, what changes?
use brackets
FunctionName(arrayName[], numElements) {}
what does const do
use const in parameter to prevent a function from modifying an array
ex:
PrintArray(const double userNums[], int numVals)
examples of when to use const
- when sorting an array
- when reordering an array
*both reordering elements
*element reversal doesn’t have to modify the array, but it’s good practice to use const in this case
what do you use when swapping elements in an array?
A TEMP VALUE! –> 1. temp, 2. swap, 3 temp
ex:
int temp;
temp = array[0];
array[0] = array[size - 1];
array[size - 1] = temp;
*swaps first and last elments
main features of 2D arrays
array[row][col]
how do you initialize a 2D array
int numVals[2][3] = {{22, 33, 44,}, {49, 99, 34}};
what is a character array
an array that sorts items in a string into characters into elements
what is the null character
‘\0’
when a string is shorter than the array, this character indicates the end of the string and STOPS PRINTING
how do you declare a char array
char firstName[10] = “Henry”;
what are the important things to remember about char arrays
**STRING CANNOT EXCEED ARRAY
**you cannot reassign a char array later, but you can copy
what do pass by reference and pass by value concern?
how arguments are passed into functions and if they can be changed
pass by value
default in c++
argument’s are copied into local variables; you are altering copies of the original variable
pass by reference
use &
- refers directly to the argument variable’s memory location
- whatever you do to the parameter, you do to the argument
- use when outputs are related
ex:
int main {
int x = 1;
foo(x);
cout «_space;x;
}
void foo(int& p) {
p = 8;
}
*output will be 8!
what is ofstream
output file stream
- reads to files
- way to output data to screen, file, etc
what is ifstream
input file stream
- reading from files
cin
- predefined istream object associated with a system’s standard input (computer keyboard)
cout
- predefined ostream object (ostream cout;)
- cout returns a reference to the ostream that called the operator
ostream
output stream
class that supports output