Dsa Assignment 2
Dsa Assignment 2
Total Marks: 90
Objective:
• This assignment aims to deepen your understanding of arrays in C++, focusing on key operations such as traversal, insertion,
deletion, searching, and sorting. You will also work with pointers and dynamic memory management to handle arrays
efficiently. The assignment is designed to be personalized based on your Registration Number (RegNo), Name, and
Birthdate, ensuring a unique experience for each student.
• Some concepts in this assignment may be new to you, and it is encouraged that you search and learn from different sources
(books, online resources, or programming forums). You can use tools like ChatGPT to help you understand the concepts, but
remember that true understanding is the key to success.
Instructions:
1. Write all solutions by hand on paper and submit the written assignment. Digital submissions are not allowed.
2. You are required to use functions and pointers for all parts of the assignment.
3. No built-in functions (e.g., sort, find, etc.) are allowed. You must manually write the logic for all array operations like
insertion, deletion, and sorting.
4. Ensure proper dynamic memory management by using new and delete to allocate and deallocate memory.
5. Your inputs (task IDs, event IDs, book IDs, etc.) will be generated based on your RegNo, Name, and Birthdate, as
detailed in each question. Each student will have different values for insertion, deletion, and searching based on their
unique details.
6. Code Commenting: Every function and major block of your code should be well-commented. Explain your logic in a
clear and concise manner.
7. Learning New Concepts: Some parts of the assignment may require you to learn new concepts or techniques, such as
dynamic memory management, pointer arithmetic, and sorting algorithms. It is recommended that you take the initiative
to research and explore these topics from different sources.
8. Use of ChatGPT: You are allowed to use ChatGPT to help you understand the concepts and logic required for this
assignment. However, you must write your solutions yourself. If you copy solutions directly without understanding, you
will struggle in the quiz, where your marks will depend entirely on how well you understand the assignment.
1
2
Important Note:
• After submitting your assignment, you will have a quiz will be announced by your instructor.
The marks you score in the quiz will determine the marks for your assignment. If you
complete the assignment correctly but without understanding, your quiz performance will
reflect this, and your assignment score will be reduced accordingly.
• This means your quiz score will directly impact your assignment grade.
2
3
Assignment Tasks:
You are tasked with building an advanced task management system where tasks are represented by unique task IDs (integers). The
system should use dynamically allocated arrays and pointers to manage tasks. Implement the following operations:
o Write a function to traverse and display all task IDs using pointer arithmetic.
o Ensure your function handles the case where no tasks are present.
o Personalized Instructions:
▪ Task ID: Convert your RegNo into a numeric format and use it as the task ID. For example, if your RegNo
is 2023007, the task ID will be 2023007.
▪ Position to Insert: Calculate the sum of the digits of your RegNo. If the sum exceeds the array size,
insert at the end. Otherwise, insert at the position determined by sum of digits % array size.
▪ Example: If RegNo = 2023007, the sum of digits is 2 + 0 + 2 + 3 + 0 + 0 + 7 = 14, so the task will be
inserted at position 14 % array size.
o Remove a task by its ID. After deletion, shift the remaining elements.
o Personalized Instructions:
▪ Task ID to Delete: The task ID to delete corresponds to the first digit of your RegNo.
▪ Example: If RegNo = 2023007, the first digit is 2, so you will delete the task ID 2.
o Personalized Instructions:
▪ Task ID to Search: The task ID to search for is calculated by taking your birthdate (ddmmyy) and
applying the modulus operation by 1000.
▪ Example: If your birthdate is 01/01/2000, the task ID to search for is 010100 % 1000 = 100.
o Write a function to sort the tasks in ascending order using bubble sort.
o Include logic to check if the array is already sorted and skip unnecessary comparisons.
3
4
You are developing a dynamic event scheduling system where events are represented by their unique event IDs (integers). Your
task is to perform the following operations on a dynamically allocated array of event IDs using pointers.
o Personalized Instructions:
▪ Event ID: Generate the event ID by taking the first three digits of your RegNo and converting them into
their ASCII values, then summing them.
▪ Example: If RegNo = 2023007, the first three digits are 202, and the ASCII values are:
▪ 2 = 50, 0 = 48, 2 = 50
▪ Position to Insert: Insert the event at the position equal to the sum of the last two digits of your birth
year.
o Remove an event by its ID and shift the elements left to maintain continuity.
o Ensure that after cancellation, the array dynamically shrinks if the size drops below a threshold (e.g., half the
current capacity).
o Implement binary search to find an event by its ID. Ensure the array is sorted before searching.
o Use Selection sort to sort the array of event IDs in ascending order.
4
5
You are tasked with developing a dynamic book catalog system where each book is represented by its unique book ID (integers).
Perform the following operations using pointers and dynamic memory.
o Personalized Instructions:
▪ Book ID: Generate your book ID as the sum of the ASCII values of your first name and last name, divided
by 2.
▪ Example: For Abdullah Bin Zarshaid, the ASCII values of the characters in the name sum to 1800.
▪ Position to Insert: Calculate the position by taking the sum of the digits of your birthdate (ddmmyy) mod
the current array size.
o Remove a book by its ID and shift the elements left to maintain continuity.
▪ Recursive Bubble Sort: Recursively sort the array by comparing adjacent elements and bubbling the
largest element to the end in each pass.
▪ Recursive Selection Sort: Recursively find the smallest element and place it at the beginning of the array,
then sort the remaining sub-array.
o Bubble Sort can be applied recursively by sorting the largest element in each pass and reducing the problem size
by one for the next recursive call.
5
6
o Selection Sort can be applied recursively by selecting the minimum element, swapping it with the first element,
and then recursively sorting the rest of the array.
For a student with RegNo = 2023007, Birthdate = 01/01/2000, and Name = Abdullah Bin Zarshaid:
• Question 1:
• Question 2:
o Event ID to Insert: 148 (sum of ASCII values of first three RegNo digits)
• Question 3:
o Book ID to Insert: 900 (sum of ASCII values of first and last name, divided by 2)
Bonus Challenge:
You can attempt these challenges to explore more advanced data structures and future applications, they are not
compulsory:
In this challenge, you will build a basic access control system where each user is assigned a unique ID (integer), and their access
to a secure system is managed using their credentials stored in a 2D array. Each row in the array represents a user, and the columns
store the user ID and a password.
2. Implement a simple encryption scheme for the passwords using a Caesar Cipher (shift each character by a certain
number).
3. Implement a function to add a new user and store their encrypted password.
4. Implement a function to verify user login by encrypting the password they provide and comparing it to the stored encrypted
password in the array.
5. Implement a function to remove a user from the array and shift the remaining users accordingly.
• In the future, using a hash table would allow faster lookups and validations, since you could hash user credentials for
constant-time access. For now, use linear search on the array to find users.
Challenge: Implement a Dynamic Power Management System for Devices using 1D Arrays
In this challenge, you will simulate a power management system where devices are represented by their power consumption
values (integers). Your task is to manage the power consumption of multiple devices using a 1D array.
2. Allow adding new devices and updating their power consumption values.
3. Implement a function to automatically turn off the device with the lowest power consumption once the total power
consumption exceeds a certain threshold.
4. Remove the device from the array and shift the remaining devices accordingly.
5. Implement a sorting algorithm (like bubble sort) to sort devices by power consumption in ascending order, ensuring the
device with the lowest power consumption is turned off first.
• In the future, using a priority queue would make this process more efficient, as it would automatically allow you to
retrieve the device with the lowest power consumption. For now, you can sort the array using bubble sort or other sorting
methods.
In this challenge, you will simulate a basic version control system that tracks versions of a file. Each version of the file is stored
in a row of a 2D array, with the version ID in the first column and the version content in the remaining columns.
3. Implement a function to rollback to a previous version by copying the content of an earlier version into the latest row.
4. Implement a function to delete specific versions and shift the remaining versions up in the array.
5. Provide the ability to list all versions of the file by traversing the 2D array.
• A linked list would be more efficient for managing file versions, as you could easily traverse and modify previous versions
without shifting elements. For now, use the 2D array to handle the version history and shift elements manually.
The bonus challenge will be worth an additional 03 points in your Quiz (Wednesday) and will be evaluated based on:
1. Creativity: The solution should demonstrate a deep understanding of how arrays can be used for the problem.
2. Correct Use of Arrays: Arrays should be used effectively to solve the problem, with proper memory management and
shifting of elements.
3. Functionality: The solution must be fully functional, and students should implement all required features.