0% found this document useful (0 votes)
5 views10 pages

Aim and Algorithm Oops

Oops

Uploaded by

priyacs104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Aim and Algorithm Oops

Oops

Uploaded by

priyacs104
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1)Tower of Hanoi in C++

Aim

To write a C++ program that solves the Tower of Hanoi problem using recursion.

Algorithm

1. Start with three pegs:

o Source (A): The starting peg with all disks.

o Auxiliary (B): Helper peg.

o Destination (C): Target peg where all disks need to be moved.

2. Rules:

o Only one disk can be moved at a time.

o A larger disk cannot be placed on a smaller disk.

o All disks start on the source peg and must end on the destination peg.

3. Recursive solution:

o If n == 1, move the single disk from Source → Destination.

o Otherwise:
a. Move n-1 disks from Source → Auxiliary (using Destination as helper).
b. Move the nth (largest) disk from Source → Destination.
c. Move the n-1 disks from Auxiliary → Destination (using Source as helper).

4. Stop when all disks are moved to the Destination peg.

2)Binary Search Tree Traversal in C++

Aim

To write a C++ program to construct a Binary Search Tree (BST) and traverse it using

1. Inorder Traversal

2. Preorder Traversal

3. Postorder Traversal
Algorithm

Step 1: Construct BST

1. Start with an empty tree.

2. For each element to be inserted:

o If the tree is empty, create a new node as the root.

o If the element is smaller than the current node, insert it in the left subtree.

o If the element is greater, insert it in the right subtree.

Step 2: Traversals

1. Inorder Traversal (LNR)

o Traverse the left subtree.

o Visit the root.

o Traverse the right subtree.

2. Preorder Traversal (NLR)

o Visit the root.

o Traverse the left subtree.

o Traverse the right subtree.

3. Postorder Traversal (LRN)

o Traverse the left subtree.

o Traverse the right subtree.

o Visit the root.

3)Stack Operations using Linked List in C++

Aim

To write a C++ program that implements a stack using a linked list and performs the following
operations:

1. Push (insert element)


2. Pop (remove element)

3. Peek/Top (view top element)

4. Display (show all stack elements)

Algorithm

Step 1: Define Node structure

• Each node will store:

o data (the value)

o next (pointer to next node)

Step 2: Push Operation

1. Create a new node.

2. Store the value in the new node.

3. Point new node’s next to the current top.

4. Update top to the new node.

Step 3: Pop Operation

1. If top == NULL, stack is empty → underflow.

2. Otherwise, store top->data, move top to top->next, and delete the old node.

Step 4: Peek Operation

1. If stack is empty, return error.

2. Otherwise, return top->data.

Step 5: Display Operation

1. Traverse from top to NULL.

2. Print each node’s data.


4)Circular Queue in C++

Aim

To write a C++ program that implements a Circular Queue and performs the following
operations:

1. Enqueue (insert element)

2. Dequeue (remove element)

3. Peek/Front (view the front element)

4. Display (show all elements in the queue)

Algorithm

Step 1: Initialization

• Create an array queue[] of size MAX.

• Maintain two variables:

o front → index of the first element.

o rear → index of the last element.

• Initially, front = rear = -1.

Step 2: Enqueue (Insert)

1. Check overflow: if (front == 0 && rear == MAX-1) || (front == rear+1) → queue is full.

2. If queue is empty (front == -1), set front = rear = 0.

3. Otherwise, update rear = (rear + 1) % MAX.

4. Insert the element at queue[rear].

Step 3: Dequeue (Remove)

1. Check underflow: if front == -1, queue is empty.

2. Store element at queue[front].

3. If front == rear, set front = rear = -1 (queue becomes empty).

4. Otherwise, update front = (front + 1) % MAX.


Step 4: Peek

• If queue is empty, return error.

• Otherwise, return queue[front].

Step 5: Display

• If queue is empty, print "Queue is empty".

• Otherwise, print elements from front to rear in circular order.

5) Quick Sort in C++

Quick Sort in C++

Aim

To write a C++ program to sort an array of elements using the Quick Sort algorithm.

Algorithm

Quick Sort (Divide and Conquer)

1. Choose a Pivot (commonly the last element).

2. Partition the Array:

o Rearrange the array so that all elements smaller than pivot are placed before it,
and all elements greater than pivot are placed after it.

3. Recursively Apply Quick Sort to the sub-arrays:

o Left side of pivot

o Right side of pivot

4. Continue until sub-arrays have only one element (base case).

6)Heap Sort in C++

Aim

To write a C++ program to sort an array of elements in ascending order using the Heap Sort
algorithm.
Algorithm

Heap Sort works in two main steps:

1. Build a Max Heap

o Arrange the array elements to satisfy the max-heap property:


Each parent is greater than or equal to its children.

2. Heap Sort Process

o Swap the root (largest element) with the last element.

o Reduce the heap size by one.

o Heapify the root again to restore the max-heap property.

o Repeat until the heap size is 1.

7)Knapsack Problem using Greedy Method (Fractional Knapsack)

Aim

To write a C++ program to solve the Knapsack problem using the Greedy method (Fractional
Knapsack approach).

Algorithm

1. Input number of items n and knapsack capacity W.

2. For each item, input:

o Weight (w[i])

o Profit (p[i])

3. Compute Profit-to-Weight ratio for each item.

4. Sort items in descending order of ratio (p[i]/w[i]).

5. Initialize totalProfit = 0 and remainingCapacity = W.

6. For each item in sorted order:

o If the entire item can fit, take it completely.

o Otherwise, take the fractional part of the item that fits.

o Update totalProfit.
7. Stop when the knapsack is full or all items are considered.

8. Output the maximum profit.

8) Search in a Tree using Divide & Conquer (BST)

Aim

To write a C++ program to search for an element in a binary search tree (BST) using the divide
and conquer strategy.

Algorithm

1. Construct a BST from user input.

2. To search for a given key:

o If the tree is empty → return "Not Found".

o Compare key with the root node.

▪ If key == root->data → return "Found".

▪ If key < root->data → search in the left subtree.

▪ If key > root->data → search in the right subtree.

3. Repeat until the element is found or the subtree becomes empty.

This recursive approach is a divide & conquer method.

9) 8 Queens Problem using Backtracking

Aim

To write a C++ program to place 8 queens on an 8 × 8 chessboard such that no two queens
attack each other (i.e., no two queens share the same row, column, or diagonal).

Algorithm (Backtracking)

1. Place queens column by column (or row by row).

2. For each column:

o Try placing a queen in every row.

o If safe (no queen attacks it in row, column, or diagonal), place the queen.
o Recursively try to place queens in the next column.

3. If all 8 queens are placed → solution found.

4. If no row is safe in the current column → backtrack (remove last placed queen and try
another row).

5. Continue until all solutions are found or one solution is printed.

10) Virtual Function in C++

Aim

To write a C++ program that demonstrates the concept of virtual functions for achieving
runtime polymorphism.

Algorithm

1. Create a base class with a function declared as virtual.

2. Derive one or more subclasses that override the virtual function.

3. Use a base class pointer to point to objects of derived classes.

4. Call the virtual function through the base class pointer.

o At runtime, the function of the actual object’s class (not the pointer type) is
invoked.

5. This demonstrates runtime polymorphism.

11) Parameterized Constructor in C++

Aim

To write a C++ program that demonstrates the use of a parameterized constructor to initialize
objects with given values.

Algorithm

1. Define a class (e.g., Student) with data members.

2. Define a parameterized constructor that takes arguments and initializes data members.

3. Create objects of the class, passing values to the constructor.


4. Display the initialized values using a member function.

12) Friend Function in C++

Aim

To write a C++ program that demonstrates the use of a friend function to access the private
data members of a class.

Algorithm

1. Define a class with private data members.

2. Declare a friend function inside the class using the keyword friend.

3. Define the friend function outside the class.

o A friend function is not a member of the class but can access private members.

4. Create an object of the class and call the friend function.

5. Display the accessed or modified private data.

13) Function Overloading in C++

Aim

To write a C++ program that demonstrates function overloading, where multiple functions have
the same name but different parameter lists.

Algorithm

1. Define a class (or global functions) with multiple functions having the same name.

2. Ensure that the functions differ by:

o Number of arguments, or

o Type of arguments.

3. Call the function with different argument types/numbers.

4. At compile-time, the compiler decides which version of the function to execute (static
polymorphism).
14) Single Inheritance in C++

Aim

To write a C++ program that demonstrates single inheritance, where a derived class inherits
from a base class.

Algorithm

1. Define a base class (e.g., Person) with data members and member functions.

2. Define a derived class (e.g., Student) that inherits from the base class using the : symbol.

3. In the derived class, add extra data members/functions.

4. Create an object of the derived class.

5. Access both base class and derived class functions to demonstrate inheritance.

15) Employee Details using Files in C++

Aim

To write a C++ program that uses file handling to store and retrieve employee details.

Algorithm

1. Define a class Employee with data members like id, name, and salary.

2. Create functions to:

o Accept employee details from the user.

o Write employee details to a file.

o Read employee details from the file and display them.

3. Use ofstream for writing data and ifstream for reading data.

4. Store details of multiple employees in a file.

5. Display the stored employee details by reading them back from the file.

You might also like