Data Structures & Algorithms: BSCS-402
Data Structures & Algorithms: BSCS-402
Data Structures
& Algorithms
BSCS-402
ASSIGNMENTS
Course Incharge:
Sir Muhammed Saeed
Assignment No# 1:
Permutations & Combinations
This C++ program generates all non-empty subsets of a given set using
bitmasking. It iterates through all bitmask values from `1` to `2^n - 1`, where each
bit represents whether an element is included in the subset. The subsets are
displayed in the format `{ element1 element2 ... }`, excluding the empty subset.
The approach efficiently generates all possible subsets without calculating
permutations or combinations. For example, with the set `{1, 2, 3}`, the output will
include `{ 1 }`, `{ 2 }`, `{ 3 }`, `{ 1 2 }`, `{ 1 3 }`, `{ 2 3 }`, and `{ 1 2 3 }`. This
method offers a simple and efficient solution for generating subsets.
Code Implementation:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int arr[] = {1, 2, 3}; // C++98-compatible array
vector<int> set(arr, arr + sizeof(arr) / sizeof(arr[0])); // Initialize vector from array
generateSubsets(set);
return 0;
}
Output:
Assignment No# 2:
Sorting a messy closet
In this assignment, we created a C++ program to organize a messy closet by
reading clothing items from a text file, sorting them alphabetically, and
implementing a search functionality to locate a specific item. The
`readClosetItems` function opens a text file (`closet.txt`) and reads each clothing
item, represented by a character, into a vector. The `sortCloset` function sorts these
items alphabetically using the `sort` algorithm, and the `searchCloset` function
implements binary search to check if a specific item exists in the sorted closet. The
program first displays the unorganized closet, then shows the organized closet, and
allows the user to search for a clothing item. For example, if the closet items are `P
S H T J`, the program first prints them as `P S H T J`, then after sorting, it displays
`H J P S T`. The user can search for any item, and the program will confirm its
presence or absence. This assignment demonstrates efficient closet management
using file input, sorting, and searching techniques.
Code Implementation:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
if (file.is_open()) {
char item;
while (file >> item) {
closet.push_back(item);
}
file.close();
} else {
cout << "Unable to open file.\n";
}
return closet;
}
int main() {
string filename = "closet.txt"; // Input file containing unorganized closet items
vector<char> closet = readClosetItems(filename);
return 0;
}
Output:
Assignment No# # 3:
Row Major & Column Major in Arrays
This assignment presents an N-dimensional dynamic array in C++ supporting both
row-major and column-major ordering. The `NDArray` class stores dimensions
and data in vectors, with helper functions to calculate linear indices for efficient
element access. Key methods `setRowMajor` and `getRowMajor` handle row-
major operations, while `setColumnMajor` and `getColumnMajor` support
column-major access. In `main`, we create a 3x3x3 array, set elements, and
retrieve them using both orderings. Display functions print all elements in each
format, demonstrating flexible and efficient multi-dimensional storage and access.
Code Implementation:
#include <iostream>
#include <vector>
class NDArray {
private:
std::vector<int> dimensions; // Stores the size of each dimension
std::vector<int> data; // Flat vector for storing all elements
int totalSize; // Total number of elements
public:
// Constructor to initialize the array with given dimensions
NDArray(const std::vector<int>& dims) : dimensions(dims) {
// Check if dimensions are non-empty
if (dimensions.empty()) {
std::cerr << "Error: Dimensions vector is empty." << std::endl;
exit(1); // Exit the program if dimensions are invalid
}
totalSize = 1;
for (size_t i = 0; i < dimensions.size(); ++i) { // Replaced range-based loop
totalSize *= dimensions[i]; // Access via index
}
data.resize(totalSize, 0); // Initialize all elements to 0
}
int main() {
// Initialize dimensions with push_back (for C++98 compatibility)
std::vector<int> dims;
dims.push_back(3);
dims.push_back(3);
dims.push_back(3); // 3x3x3 array
indices[0] = 1;
indices[1] = 2;
indices[2] = 2;
array.setRowMajor(indices, 9);
// Getting elements in row-major order
std::cout << "Row-Major Order Access:\n";
indices[0] = 0;
indices[1] = 0;
indices[2] = 0;
std::cout << "array[0][0][0] = " << array.getRowMajor(indices) << std::endl;
indices[2] = 1;
std::cout << "array[0][0][1] = " << array.getRowMajor(indices) << std::endl;
indices[0] = 1;
indices[1] = 2;
indices[2] = 2;
std::cout << "array[1][2][2] = " << array.getRowMajor(indices) << std::endl;
indices[2] = 1;
array.setColumnMajor(indices, 4);
indices[0] = 2;
indices[1] = 2;
indices[2] = 2;
array.setColumnMajor(indices, 10);
indices[2] = 1;
std::cout << "array[0][0][1] = " << array.getColumnMajor(indices) << std::endl;
indices[0] = 2;
indices[1] = 2;
indices[2] = 2;
std::cout << "array[2][2][2] = " << array.getColumnMajor(indices) << std::endl;
return 0;
}
Output:
Assignment No# # 4:
Implementaions of ADT:
1. Dynamic Linked list:
In this assignment, implement a dynamic linked list in C++ with essential
operations. Create a `LinkedList` class that includes methods for appending
(`append`), inserting at a specific position (`insert`), displaying all nodes
(`display`), searching for a value (`search`), and deleting a node by value
(`deleteNode`). Use a `Node` structure to store an integer value and a pointer
to the next node. In `main()`, demonstrate the list by appending values 10,
20, and 30, inserting 15 at position 1, displaying the list, searching for values
20 and 25, deleting the node with value 20, and displaying the final list.
Implement a destructor to clean up memory, ensuring no memory leaks
occur.
Code Implementation:
#include <iostream>
using namespace std;
// Node structure for the linked list
struct Node {
int data;
Node* next;
public:
LinkedList() : head(NULL) {}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
if (temp->next) {
Node* toDelete = temp->next;
temp->next = temp->next->next;
delete toDelete;
} else {
cout << "Value not found in the list" << endl;
}
}
int main() {
LinkedList list;
list.append(10);
list.append(20);
list.append(30);
list.insert(15, 1); // Inserts 15 at position 1
list.display();
cout << "Searching for 20: " << (list.search(20) ? "Found" : "Not Found") << endl;
cout << "Searching for 25: " << (list.search(25) ? "Found" : "Not Found") << endl;
return 0;
}
Output: