please answer questions seperately anout the city of philadephia...docx
What is the difference between static and dynamic arrays Explain he.docx
1. What is the difference between static and dynamic arrays?
Explain heap and stack data structure. What is the major
difference and when they used? What is function overloading
What are inline functions and why they are used
Solution
a).
A static array has memory allocated at compile time
A dynamic array has memory allocated at run time
A static array variable holds a value of type, array. A
dynamic array variable holds a pointer to an array value. Thanks
to automatic pointer dereferencing and automatic index padding,
there is very little difference in the code that you write to use
either type of array.
Static arrays are created in the variable declaration [
StaticArray[50] ], whereas dynamic arrays are created using the
operator provided by language
(in C- use malloc, calloc. In C++ - use new operator)
b).
2. A Heap is a Tree with following properties.
1) It’s a complete tree (All levels are completely filled except
possibly the last level and the last level has all keys as left as
possible). This property of Heap makes them suitable to be
stored in an array.
2) A Heap is either Min Heap or Max Heap. In a Min Heap, the
key at root must be minimum among all keys present in Heap.
The same property must be recursively true for all nodes in
Tree.
Max Heap is similar to Min Heap.
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in
O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently
implemented using Binary Heap because it supports insert(),
delete() and extractmax(), decreaseKey() operations in O(logn)
time. Binomoial Heap and Fibonacci Heap are variations of
Binary Heap. These variations perform union also efficiently.
3) Graph Algorithms: The priority queues are especially used
in Graph Algorithms like Dijkstra’s Shortest Path and Prim’s
Minimum Spanning Tree.
Stack is a linear data structure which follows a particular
order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In
Last Out).
Applications of stack:
3. Balancing of symbols:
Infix to Postfix/Prefix conversion
Redo-undo features at many places like editors, photoshop.
Forward and backward feature in web browsers
Used in many algorithms like Tower of Hanoi, tree traversals,
stock span problem, histogram problem.
Other applications can be Backtracking, Knight tour problem,
rat in a maze, N queen problem and sudoku solver
c).
Function overloading (also method overloading) is a
programming concept that allows programmers to define two or
more functions with the same name. Each function has a unique
signature (or header), which is derived from: function/procedure
name. number of arguments. arguments ' type.
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
4. d).
C++ inline function is powerful concept that is commonly
used with classes. If a function is inline, the compiler places a
copy of the code of that function at each point where the
function is called at compile time.
To inline a function, place the keyword inline before the
function name and define the function before any calls are made
to the function. The compiler can ignore the inline qualifier in
case defined function is more than a line.
Advantages :-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack,
while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction
cache.
5) After in-lining compiler can also apply intraprocedural
optmization if specified. This is the most important one, i n this
way compiler can now focus on dead code elimination, can give
more stress on branch prediction, induction variable elimination
etc