CS Week 1 - Vector and Functions Review Flashcards

1
Q

Radix sort

A

sort smaller pieces, than sort those pieces
- sorting last names, sort 26 stacks, put together sorted

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

modular development

A

process of dividing a program into separate modules that can be developed and tested separately and then integrated into a single program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

incremental development

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

function stub

A

function definition whose statements have not yet been written
- cout fixme
- helps capture the correct high-level behavior

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

function

A

defined to compute a mathematical calculation involving several numerical parameters and a number result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

difference in using a function in main

A

void - a statement that calls it to cout or updates variables
any other type - a returned value use as an expression or cout it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

unit testing

A

process of individually testing a small part of a program, typically a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

test bench

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

test vector

A

each unique set of input values
- make sure you use border cases and inputs that thoroughly exercise the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what does assert() do, how to include and use

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

pass by value

A

arguments value is copied into a local variable for the parameter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

pass by reference

A

does not create a local copy of the argument, refers directly to the arguments
variable memory location
append & to type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is pass by reference for

A

changing the original variable
more than one return
big data types like strings and vectors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

reference variable

A

variable type that refers to another variable
type& name = other;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

scope

A

the name of a defined variable or function item is only visible to a part of a program
-starts after it is declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

global variable and why it should be avoided

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

side effects

A

a function that updates a global variable has side effects that go beyond its parameters and return value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

function declaration

A

declared before main with everything but { } and statements
- ends with ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

rand() and what to include

A

generates a random number
# include <cstdlib>
range from 0 to RAND_MAX</cstdlib>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what to use to make the range of a random number

A

%

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

how many possible values in rand() % N and what range

A

N possible values from 0 to N-1

22
Q

specific range of a random number

A

rand() % (large - small + 1) + small

23
Q

pseudo random

A

not actually but having the appearance of
- the ints generated by rand()

24
Q

how to get different sequence of random numbers and how each time

A

srand( seedYouWant);
- called once before rand()

srand(time());
#include <ctime></ctime>

25
Q

vector and what to include

A

ordered list of items of a given data type - an object
#inlcude <vector></vector>

26
Q

element

A

each item in a vector

27
Q

declare a vector with a size, with the same value, and all different values

A

vector<type> name (numElements);</type>

vector<type> name (numElements, value);</type>

vector<type> name = {value, value, ...};</type>

28
Q

how to find vector size

A

name..size();

29
Q

how to know if a vector is empty

A

name.empty()
- returns a bool
- 0 - false
- 1- true

30
Q

how to access an element in a vector and how to nth element

A

name.at(index);

index of nth element = n - 1

31
Q

difference between using v[index] and v.at(index)

A

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

32
Q

iterating through a vector

A

iterate .size() times

33
Q

how to hold aside memory for a vector and why

A

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

34
Q

how to add a new element to a vector

A

v.push_back(elementToAdd);

35
Q

how to add an element to the beginning of a vector

A

v.push_front(elementToAdd);

36
Q

how to find/access the last element of a vector

A

v.back();
- returns last element

37
Q

how to get rid of the last element of a vector

A

v.pop_back();

38
Q

why can .at() be used on both side of =

A

because you can change its value and use its value

39
Q

vector copy operation

A

=
vecB = vecA
b is a copy of a

40
Q

vector equality operation

A

==
vecB == vecA
compares a and b

41
Q

how to reverse a vector

A

iterate .size() / 2 times
temp val = .at(i)
.at(i) = .at( .size() - 1- i);
.at( .size() - 1 - i ) = temp val

42
Q

auto

A

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

43
Q

how to print the type of a variable

A

typeid( variableName).name()

44
Q

types of d, i, and PKc

A

d - double
i = int
PKc = string (point, const, char)
- a string is a const char*

45
Q

range based loop

A
  • 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 &laquo_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&raquo_space; n2;
n1 = n2;
}

46
Q

what is cerr

A

not for normal output
errors

47
Q

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>

A

using namespace std;

std : vector <type> name;</type>

48
Q

two first statements

A

include <iostream>
using namespace std;</iostream>

49
Q

how to test your input

A

if ( !cin) {
cerr &laquo_space;“bad” &laquo_space;endl;
}

50
Q

.good();
.eof();
.fail();
.bad();

A

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

51
Q

if ( conditional ) break;

if ( conditional) continue;

and where can you use these

A
  • get out of loop
  • skip this iteration
  • in a loop before all other statements
52
Q

look over ch2, ch 4, ch 5, CH 6!!, ch 7, ch 8, ch 9 from 10A

A

:)