You are on page 1of 5

Row major, cache-friendly, use this method to access all items in array sequenti

ally.
for (i=0; i<numRow; i++) // ROW FIRST = EFFECTIVE
for (j=0; j<numCol; j++)
array[i][j] = 0;
Column major, NOT cache-friendly, DO NOT use this method or you'll get very poor
performance. Why? you will learn this in Computer Architecture course. For now,
just
take note that computer access array data in row major
--------
Vector
Vector Trick - A resizable array
Static array has a drawback when the size of input is not known beforehand. If t
hat is the
case, you may want to consider vector, a resizable array.
There are other data structures which offer much more efficient resizing capabil
ity such
as Linked List, etc.
TIPS: UTILIZING C++ VECTOR STL
C++ STL has vector template for you.
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
void main() {
// just do this, write vector<the type you want,
// in this case, integer> and the vector name
vector<int> v;
// try inserting 7 different integers, not ordered
v.push_back(3); v.push_back(1); v.push_back(2);
v.push_back(7); v.push_back(6); v.push_back(5);
v.push_back(4);
vector<int>::iterator i; // to access the element, you need an iterator .
..
printf("Unsorted version\n");
for ( i = v.begin() ; i!= v.end() ; i++) // start with 'begin', end with
'end', advance with i++
printf("%d ",*i); // iterator's pointer hold the value
printf("\n");
sort(v.begin(),v.end()); // default sort, ascending
printf("Sorted version\n");
for (i = v.begin(); i!= v.end(); i++) //Attention to this i ->vector<int
>::iterator i
printf("%d ",*i); // iterator's pointer hold the value
printf("\n");
}
-------
Link List
A demo on the usage of STL list. The underlying data structure is a
doubly link list.
#include <stdio.h>
#include <list> // this is where list implementation resides
using namespace std;
list<int> l; // just do this, write list<the type you want,
list<int>::iterator i; // in this case, integer> and the list name
void print() {
for (i = l.begin(); i != l.end(); i++)
printf("%d ",*i); // remember... use pointer!!!
printf("\n");
}
void main() {
// try inserting 8 different integers, has duplicates
l.push_back(3); l.push_back(1); l.push_back(2);
l.push_back(7); l.push_back(6); l.push_back(5);
l.push_back(4); l.push_back(7);
print();
l.sort(); // sort the list, wow sorting linked list...
print();
l.remove(3); // remove element '3' from the list
print();
l.unique(); // remove duplicates in SORTED list!!!
print();
i = l.begin(); // set iterator to head of the list
i++; // 2nd node of the list
l.insert(i,1,10); // insert 1 copy of '10' here
print();
}
---------
Scanf("kind" , name , max of string)
scanf("%char numbers that U want to get" , string name , maximum number of strin
g)
example : char line[21];
scanf( "%12s" , line , 21);
To read strings not delimited by whitespace characters, a set of characters in b
rackets ([ ]) can be substituted for the s (string) type character.
The set of characters in brackets is referred to as a control string.
The corresponding input field is read up to the first character that does not ap
pear in the control string.
If the first character in the set is a caret (^), the effect is reversed: The in
put field is read up to the first character that does appear in the rest of the
character set.
Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This
is a common scanf function extension, but note that the ANSI standard does not r
equire it.
scanf("%[^\n]",line); == cin.getline(line , 21);
------------
Be careful about using gets() and scanf() together !
input:
ABCDEF
bbbbbXXX B=Blank
scanf("%s\n",&dummy);
gets(name);
cout<<dummy<<'\n'<<name;
output:
ABCDEF
XXX
BUT if code like blowe code & input was: bbbbbXXX
gets(name);
cout<<name;
output: bbbbbXXX b=Blank
-----------
Note: for those who don't know, in C, long long n is read using: scanf("%lld",&n
); and unsigned long long n is read using:
scanf("%llu",&n); For 64 bit data: typedef unsigned long long int64; int64 test_
data=64000000000LL
-----------
MATH ART
If d is a divisor of n, then so is n/d, but d & n/d cannot both be greater than
sqrt(n).
-----------
exp(log(base)*power) = pow(base,power)
left equation is faster than right one
-----------
SO SO important & beautiful algorithm it's reallity art for this algorithm
it's change the Fibonacci run time to O(log n)
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
void main() //Fibonacci
{
int i,h,j,k,n,t;
cin>>n;
i = h = 1;
j = k = 0;
while (n > 0)
{
if (n%2 == 1)
{ // if n is odd
t = j*h;
j = i*h + j*k + t;
i = i*k + t;
}
t = h*h;
h = 2*k*h + t;
k = k*k + t;
n = (int) n/2;
}
cout<<"fainal result is : "<<j<<endl;
}
----------
Greatest Common Divisor (GCD)
it made up of 3 XOR
#include <math.h>
#include <iostream>
using namespace std;
void main()
{
int a,b;
cin>>a>>b;
while(b>0)
{
a=a%b;
a^=b;
b^=a;
a^=b;
}
cout<<a;
}
----------
Lowest Common Multiple (LCM)
LCM (m,n) = (m * n) / GCD (m,n)
----------
find N is prime or NOT
If 2^N modulo N = 2 then N has a high probability to be a prime number.
----------
"Sieve of Eratosthenes" is the fast & best way to find prim number
it's complitly contest about it in (ART OF PROGRAMMING) , page 105
----------
ART OF SORTING
Quck sort is one of the best sort that we can choose it
it wrote in <Stdlib> header file
qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);
one thing that's reality important here is compare_function
this part open programer hand to sort alot thing in paralle
so programer foces on how compare things
INTIGER
int compare_function(const void *a,const void *b)
{
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}
STRING
#include <string.h>
int compare_function(const void *a,const void *b)
{
return (strcmp((char *)a,(char *)b));
}
FLOAT
int compare_function(const void *a,const void *b)
{
double *x = (double *) a;
double *y = (double *) b;
// return *x - *y; // this is WRONG...Becuse float have
if (*x < *y)
return -1;
else if (*x > *y)
return 1;
return 0;
}
one important thing is work with pointer variable
----------
ART OF SEARCHIG
Bainery search is one of best search that wee know
Bainery Tree
-----------
ART OF GREEDY ALGORITHMs
Greedy algorithms are fast, generally linear to quadratic and require little ext
ra memory.
Unfortunately, they usually aren't correct. But when they do work, they are ofte
n easy to
implement and fast enough to execute.
In general, greedy algorithms have five pillars:
- A candidate set, from which a solution is created
- A selection function, which chooses the best candidate to be added to the solu
tion
- A feasibility function, that is used to determine if a candidate can be used t
o
contribute to a solution
- An objective function, which assigns a value to a solution, or a partial solut
ion, and
- A solution function, which will indicate when we have discovered a complete
solution

You might also like