CS Week 1 - Vector and Functions Review Flashcards
Radix sort
sort smaller pieces, than sort those pieces
- sorting last names, sort 26 stacks, put together sorted
modular development
process of dividing a program into separate modules that can be developed and tested separately and then integrated into a single program
incremental development
process in which a programmer writes, compiles, and tests a small amount of code, then writes compiles and tests a small amount more and so on
function stub
function definition whose statements have not yet been written
- cout fixme
- helps capture the correct high-level behavior
function
defined to compute a mathematical calculation involving several numerical parameters and a number result
difference in using a function in main
void - a statement that calls it to cout or updates variables
any other type - a returned value use as an expression or cout it
unit testing
process of individually testing a small part of a program, typically a function
test bench
a separate program whose sole purpose is to check that a function returns the correct output values for a variety of inputs
- used to conduct unit testing
test vector
each unique set of input values
- make sure you use border cases and inputs that thoroughly exercise the program
what does assert() do, how to include and use
- tests your function in main
# include <cassert>
assert ( functionCall(arguments) == correct output );</cassert> - if false, exits and tells you the line number and expression
- or cout that you are testing the input and the expected output and what you got by calling the function
pass by value
arguments value is copied into a local variable for the parameter
pass by reference
does not create a local copy of the argument, refers directly to the arguments
variable memory location
append & to type
what is pass by reference for
changing the original variable
more than one return
big data types like strings and vectors
reference variable
variable type that refers to another variable
type& name = other;
scope
the name of a defined variable or function item is only visible to a part of a program
-starts after it is declared
global variable and why it should be avoided
variable declared outside
any function
- reaches into functions
- better for constants
-a local defined with same name than global is ignored AND
-if a function changes it, its effects go beyond its return and parameters
side effects
a function that updates a global variable has side effects that go beyond its parameters and return value
function declaration
declared before main with everything but { } and statements
- ends with ;
rand() and what to include
generates a random number
# include <cstdlib>
range from 0 to RAND_MAX</cstdlib>
what to use to make the range of a random number
%
how many possible values in rand() % N and what range
N possible values from 0 to N-1
specific range of a random number
rand() % (large - small + 1) + small
pseudo random
not actually but having the appearance of
- the ints generated by rand()
how to get different sequence of random numbers and how each time
srand( seedYouWant);
- called once before rand()
srand(time());
#include <ctime></ctime>
vector and what to include
ordered list of items of a given data type - an object
#inlcude <vector></vector>
element
each item in a vector
declare a vector with a size, with the same value, and all different values
vector<type> name (numElements);</type>
vector<type> name (numElements, value);</type>
vector<type> name = {value, value, ...};</type>
how to find vector size
name..size();
how to know if a vector is empty
name.empty()
- returns a bool
- 0 - false
- 1- true
how to access an element in a vector and how to nth element
name.at(index);
index of nth element = n - 1
difference between using v[index] and v.at(index)
v[index] faster but does not check if index is legal - not safe
v.at(index) check is an index is legal - slower but safer
iterating through a vector
iterate .size() times
how to hold aside memory for a vector and why
name.reserve(amount);
- does not change size, just holds memory
- when you add another index, it makes a copy of it all in new memory, give compiler an idea/guess of what memory to hold aside
how to add a new element to a vector
v.push_back(elementToAdd);
how to add an element to the beginning of a vector
v.push_front(elementToAdd);
how to find/access the last element of a vector
v.back();
- returns last element
how to get rid of the last element of a vector
v.pop_back();
why can .at() be used on both side of =
because you can change its value and use its value
vector copy operation
=
vecB = vecA
b is a copy of a
vector equality operation
==
vecB == vecA
compares a and b
how to reverse a vector
iterate .size() / 2 times
temp val = .at(i)
.at(i) = .at( .size() - 1- i);
.at( .size() - 1 - i ) = temp val
auto
tells compiler to determine the variables type using the value we give it
auto v = 2;
compiler now knows its type can use auto in a loop with this variable
- this is the context you need to use auto elsewhere
how to print the type of a variable
typeid( variableName).name()
types of d, i, and PKc
d - double
i = int
PKc = string (point, const, char)
- a string is a const char*
range based loop
- does not use an index
- copy first value of vector into a variable and run the loop until there are no more elements in the vector
for ( type variable : vector) {
cout «_space;variable;
}
- use const and & so that variable is not changed and it just reads vector
- when variable is called, go find the address
for ( const auto(or type)& variable : vector) {
sum += x;
}
- add auto so that x holds the right type
—————————————–
- if variable is a reference, it will change the vector directly
for (type& n1 : vector) {
type n2;
cin»_space; n2;
n1 = n2;
}
what is cerr
not for normal output
errors
what allows for this to work :
vector<type> name; and how would it look different if we did not include what makes it work</type>
using namespace std;
std : vector <type> name;</type>
two first statements
include <iostream>
using namespace std;</iostream>
how to test your input
if ( !cin) {
cerr «_space;“bad” «_space;endl;
}
.good();
.eof();
.fail();
.bad();
good - returns true if no stream errors have occurred
eof - returns value of eofbit if end of line reached
fail - returns true if fail bit or bad bit is set/error for previous operation
bad - returns true of bad bit is set, indicating the stream is bad
if ( conditional ) break;
if ( conditional) continue;
and where can you use these
- get out of loop
- skip this iteration
- in a loop before all other statements
look over ch2, ch 4, ch 5, CH 6!!, ch 7, ch 8, ch 9 from 10A
:)