2 Lists and nums Flashcards
include
A growable array!
Iterator
-nested type of vector that represents position
iterator.begin() or iterator.end()
makes a pointer like object to the front or back of the vector
itr++
itr–
*itr
itr==itr
move forward
move backward
returns reference to object at itr location
true if same location
for ( vector::iterator it = v.begin();
it != v.end(); it++ )
cout «_space;*it «_space;endl;
this is the way to iteratew through aa vector with an iterator
Stacks
push
pop
top
pancakes!
7 2 3 + 8 *3+
50? I think?
Linked list?
Has every object point to the next with a next attribute as the next object
Queues
enqueue
dequeue
front()
back()
abstract data types meaning
data type without specifics about language!!
How to convert from decimal to radix?
hard to explain here is a few examples
10 from decimal to binary: 10 = 2^3 + 2^1 = 1010 or 10/2 = 5 r 0 5/2 = 2 r 1 2/2 = 1 r 0 1/2 = 0 r 1
So 1010
30 from decimal to base 5: 30/5 = 6 r 0 6/5 = 1 r 1 1/5 = 0 r 1 so 110
Convert from radix to decimal?
110 from base 5 to decimal
15^2 + 15^1 + 0*5^0 = 30
1010 = 2^3 + 2^1 = 10
How many hex digits is a byte?
2
How many bits is a byte?
8
Boolean math 0+0 0+1 1+0 1+1
0+0 = 0
0+1 =1
1+0 =1
1+1=0 carry 1
Unsigned int
no sign, 0 up to some number
a type can hold?
2^n -1 where n is number of bits
Switching endian-ness means what?
Swapping the BYTES!
0xdeadbeef is big endian.
Whats little endian form of this?
0xefbeadde
1111 0101 0000 1010
to little endian?
0000 1010 1111 0101
Point of two’s compliment?
to avoid two representations for zero
To find two’s compliment of a number with n-bit memory space.
-Zero is n 0s
-Positive numbers:
-Max vlue is 2^n-1 -1
-Sign bit is zero
-This, zero is a “positive”
number! (and is all zeros)
-Negative numbers:
-max value is -2^n-1
-take the absolute value and encode it t binry
-flip ALL the bits
-add 1 (if 0101 then 0110)
Overflow
like adding 1 to 0111 1111 (OR 127)… which is 1000 0000, which is -128.
-need more bits to make sure max value isn’t hit!
Int range vs unsigned int
-2^31 to 2^31 -1 is int
0 to 2^31 -1 is unsigned int
pros/cons of fixed point real representation
pros:
- Less computationally demanding
- Good for CPUs that don’t have an FPU
- Number representation space is uniform
cons:
- Less flexible
- Nobody uses it anymore
- Small range of values
0x41c80000 (big end float) to decimal
bin 0100 0001 1100 1000 0000 0000 0000 0000
sign bit = 0 so positive
Exponent 1000 0011 = 131-127 = 4
Mantissa 100 1000 0000 0000 0000 0000 = 1.1001 in base 2 or 1+ 1/2 + (1/2)^4 = 1.5625
1.5625 * 2^4 = 25
Largest positive float
2 * 2^127
smallest positive float
1.175494 x 10-38
Are floats spacially uniform?
No! Make the following numbers the next highest number 1.23 -> 1.24 12.3 -> 12.4 123 -> 124 1230 -> 1240
convert 42.125 to binary
0 positive
42.125 = 2^5 + 2^3 + 2^1 + 2^-3 = 101010.001 in binary
or 1.01010001 *2^5
so
0
exponent = 5 + 127 = 132 (10000100)
mantissa = 01010001
0100 0010 0010 1000 1000 0000 0000 0000 is answer
Floating point precision errors
.1 can’t be represented! (and other numbers obviously) so it rounds! but the actual value IS .09999999999999, even though it rounds when you print
given a=1;
b = .1+.1+.1+.1+.1+.1+.1+.1+.1+.1
c = .99999999999999999999
c==b?
b==a?
c==b
b!=a
How to solve floating point precision errors?
-float prints 7 digits of accuracy, so add .0000001 to the num THEN compare
// C++: #include & compile with -lm
#define EPSILON 0.000001 bool foo = fabs (a-b) < EPSILON;
What should you do to avoid floaating point nums?
USE RATIONALS
Two ways to declare an array
int someInts[3];
int someInts[] = {3,4,5};
Will compiler catch out of index bounds errors with arrays?
No! no index checking
How to copy an array?
Element by element! You cannot just do ‘=’.
How to compare arrays?
Compare value by value…. == will compare the address of first(0th) element
Arrays are POINTERS to the beginning element of the array (T/F)
True
int someInts[3];
cout «_space;someInts;
will print the &someInts[0]
int* someInts = new int[3];
What’ll this do?
This will make a int pointer called someInts that points to the first element of the array.
IF YOU DO THIS, you must delete [] someInts; afterwards