Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Advanced Programming Workshop Day2

The document outlines topics for a coding session, focusing on functions, recursion, and the Standard Template Library (STL) in C++. It explains various types of functions, recursion techniques, and STL components like vectors, maps, sets, stacks, and queues, along with their respective functions. Additionally, it includes an example problem related to balanced parentheses to illustrate practical application.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Advanced Programming Workshop Day2

The document outlines topics for a coding session, focusing on functions, recursion, and the Standard Template Library (STL) in C++. It explains various types of functions, recursion techniques, and STL components like vectors, maps, sets, stacks, and queues, along with their respective functions. Additionally, it includes an example problem related to balanced parentheses to illustrate practical application.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

HOLA FELLOW CODERS!!

Let’s Begin!
TOPICS TO BE COVERED

Day 2:

○ Functions and Function Calling


○ Recursion
○ STL (Standard Template Library)
■ Iterators
■ Stack/Queue
■ Vectors
■ Map/Set
What is a Function in C++?
A function in C++ is a block of reusable code that performs a specific task. It
helps in modular programming by breaking down complex tasks into smaller,
manageable parts. Functions improve code reusability and readability.

Types of Functions in C++


1. Library Functions – Predefined functions like sqrt(), pow(), abs()
from standard libraries.
2. User-Defined Functions – Functions created by the programmer to
perform specific tasks.
Ques : Print the table for any number n.
Explanation:
1. In main(), x and y are actual
parameters (values passed to
add()).
2. In add(int a, int b), a and
b are formal parameters
(receiving values of x and y).
FUNCTION CALLING
Parameters (or arguments) are values passed to functions to perform
operations. They can be classified as follows:

1⃣ Call by Value

● A copy of the argument is passed to the function.


● Changes inside the function do not affect the original variable.

2⃣ Pass by Reference

● The function receives a reference to the original variable.


● Changes inside the function reflect on the original variable.
1⃣ Call by Value 2⃣ Pass by Reference
3⃣ Pass by Pointer
● The function receives a pointer to the original variable.
● It allows modifying the original value using dereferencing.
4⃣ Default Parameters
● Assigns default values to function parameters.
● If no argument is passed, the default value is used.
3⃣ Pass by Pointer 4⃣ Default Parameters
RECURSION
Recursion is a programming technique where a function calls itself
to solve smaller instances of a problem until a base condition is
met.

Conditions for Recursion:


- Base Case – A condition where recursion stops.
- Recursive Case – A condition where the function calls itself.
Example 1: Fibonacci Series using Recursion
The Fibonacci sequence is defined as:
F(n) = F(n−1) + F(n−2)

with base cases:


F(0) = 0 , F(1) = 1

Fibonacci Sequence : 0 1 1 2 3 5 8 13 21……


Time Complexity : O(2^n)
Space Complexity : O(n)
Example 2: Factorial using Recursion
Factorial of a number n is defined as:
n! = n × (n−1)!
with base case:
0! = 1
1! = 1
Time Complexity : O(n)
Space Complexity : O(n)
STL : Standard Template Library
Some basic and commonly used STL
● Stack
● Queue
● Vector
● Map - multimap / unordered_map
● Set - multiset / unordered_set
What is an iterator ??
An iterator is an object that points to an element inside a container and provides operations
to access and move through the container.
Just like pointers, you can dereference an iterator using the * operator to get the element it
points to, and you can increment (++) it to move to the next element.
Vector
In C++, vector is a dynamic array with the ability to resize itself automatically
when an element is inserted or deleted. It is the part Standard Template Library
(STL) and provide various useful functions for data manipulation.
VECTOR FUNCTIONS
1. push_back(value) : Adds an element to the end.
2. pop_back() : Removes the last element.
3. insert(position, value) : Inserts an element at the given position.
4. erase(position) : Removes an element at the given position.
5. clear() : Removes all elements.
6. resize(n ): Resizes the vector to n elements.
7. begin(), end() : Iterators for traversing elements.
8. size() : Returns the number of elements.
9. capacity() : Returns the current storage capacity.
10. front() : Returns the first element.
11. back() : Returns the last element.
Map
Maps store elements in a mapped fashion. Each element has a key value
and a mapped value. No two mapped values can have the same key values.
MAP FUNCTIONS

1. insert({key, value}): Inserts a key-value pair.


2. emplace(key, value): Constructs and inserts an element (more efficient
than insert).
3. erase(key): Removes the element with the specified key.
4. clear(): Removes all elements.
5. size(): Returns the number of elements.
6. empty(): Checks if the map is empty.
7. find(key): Returns an iterator to the key if found, else map.end().
Set
Set stores unique elements where they:

● Are sorted automatically in ascending order.


● Are unique, meaning equal or duplicate
values are ignored.
● Can be added or removed, but the value of
an existing element cannot be changed.
● Cannot be accessed by index numbers,
because the order is based on sorting and
not indexing.
SET FUNCTIONS
1. insert(value): Inserts an element into the set.
2. erase(value): Removes an element if it exists.
3. clear(): Removes all elements.
4. count(value): Returns 1 if the element exists, otherwise 0.
5. find(value): Returns an iterator to the element if found, else set.end().
6. size(): Returns the number of elements.
7. empty(): Checks if the set is empty.
Stack
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working,
where a new element is added at one end (top) and an element is removed from that
end only.
STACK FUNCTIONS

1. push(value): Pushes an element onto the stack.


2. pop(): Removes the top element.
3. top(): Returns the top element of the stack.
4. size(): Returns the number of elements.
5. empty(): Checks if the stack is empty.
Queue
Queues are a type of container adaptors that operate in a first in first out
(FIFO) type of arrangement. Elements are inserted at the back (end) and are
deleted from the front.
QUEUE FUNCTIONS

1. push(value): Adds an element to the back of the queue.


2. pop(): Removes the front element.
3. front(): Returns the front element.
4. back(): Returns the last element.
5. size(): Returns the number of elements.
6. empty(): Checks if the queue is empty.
Difference between stack and queue
Multiset

A multiset in C++ is an associative


container that allows multiple copies
of equivalent elements. These
elements are stored in sorted order
by their value. It’s similar to a set but
allows duplicates.
Unordered_map

An unordered_set in C++
is an associative container
that stores unique elements
in an unordered manner. It
does not allow duplicate
values and provides fast
average-time complexity for
insertions, deletions, and
lookups using a hash table.
In-built STL Algorithms
1. sort(): Sorts elements in a range.
2. reverse(): Reverses the order of elements.
3. find(): Finds the first occurrence of an element.
4. binary_search(): Checks if an element exists in a sorted range.
5. lower_bound(): Returns the first position where a value can be inserted
without disrupting the order.
6. upper_bound(): Returns the first position where a value is strictly greater
than the given value.
7. max(), min(): Returns the larger or smaller of two values.
8. accumulate(): Computes the sum of elements.
9. count(): Counts occurrences of a value.
Ques : Balanced Parenthesis Problem?

Given a string s representing an expression containing various types of


brackets: {}, (), and [], the task is to determine whether the brackets in
the expression are balanced or not. A balanced expression is one
where every opening bracket has a corresponding closing bracket in
the correct order ?
THE END!!

You might also like