SlideShare a Scribd company logo
1 of 4
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).
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:
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;
}
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

More Related Content

More from ajoy21

Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
ajoy21
 
Please answer the 6 questions and make a respond for the 2 comments..docx
Please answer the 6 questions and make a respond for the 2 comments..docxPlease answer the 6 questions and make a respond for the 2 comments..docx
Please answer the 6 questions and make a respond for the 2 comments..docx
ajoy21
 

More from ajoy21 (20)

Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 
please answer thi questionThere has been a lot published about.docx
please answer thi questionThere has been a lot published about.docxplease answer thi questionThere has been a lot published about.docx
please answer thi questionThere has been a lot published about.docx
 
please answer this question from thisText O’Keefe, Daniel .docx
please answer this question from thisText O’Keefe, Daniel .docxplease answer this question from thisText O’Keefe, Daniel .docx
please answer this question from thisText O’Keefe, Daniel .docx
 
Please answer the following 4 questions without plagarism. No specif.docx
Please answer the following 4 questions without plagarism. No specif.docxPlease answer the following 4 questions without plagarism. No specif.docx
Please answer the following 4 questions without plagarism. No specif.docx
 
Please answer the fololowing questions No plagiarism I need 4 pa.docx
Please answer the fololowing questions No plagiarism I need 4 pa.docxPlease answer the fololowing questions No plagiarism I need 4 pa.docx
Please answer the fololowing questions No plagiarism I need 4 pa.docx
 
Please answer the 5 questions.1.Describe the functiona.docx
Please answer the 5 questions.1.Describe the functiona.docxPlease answer the 5 questions.1.Describe the functiona.docx
Please answer the 5 questions.1.Describe the functiona.docx
 
Please answer the 6 questions and make a respond for the 2 comments..docx
Please answer the 6 questions and make a respond for the 2 comments..docxPlease answer the 6 questions and make a respond for the 2 comments..docx
Please answer the 6 questions and make a respond for the 2 comments..docx
 
Please answer professor question, Answer is below questionTh.docx
Please answer professor question, Answer is below questionTh.docxPlease answer professor question, Answer is below questionTh.docx
Please answer professor question, Answer is below questionTh.docx
 
please answer questions seperately anout the city of philadephia...docx
please answer questions seperately anout the city of philadephia...docxplease answer questions seperately anout the city of philadephia...docx
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