0% found this document useful (0 votes)
17 views8 pages

Dsa Assignment 2

DSA assignment 2 giki

Uploaded by

danishahmed3232
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)
17 views8 pages

Dsa Assignment 2

DSA assignment 2 giki

Uploaded by

danishahmed3232
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/ 8

1

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology

Faculty of Computer Science and Engineering


CS221/CE222- DSA (Fall 2024)
Assignment # 2
Instructor: Abdullah Bin Zarshaid.

Total Marks: 90

Array Operations Using Pointers and Functions in C++:

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:

Question 1: Advanced Task Management System

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:

1. Display the Current Tasks (Traversal):

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.

2. Add a New Task (Insertion):

o Insert a new task ID at a specific position in the array.

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.

3. Remove a Completed Task (Deletion):

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.

4. Find a Task (Searching):

o Implement a function to search for a task by its ID using linear search.

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.

5. Sort Tasks by Task ID (Sorting):

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

Question 2: Dynamic Event Scheduling System

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.

1. View All Events (Traversal):

o Write a function to display all event IDs using pointers.

o If there are no events, print an appropriate message.

2. Schedule a New Event (Insertion):

o Insert a new event at a specific index.

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

▪ So, the event ID to insert is 50 + 48 + 50 = 148.

▪ Position to Insert: Insert the event at the position equal to the sum of the last two digits of your birth
year.

▪ Example: If your birth year is 2000, the position is 0 + 0 = 0.

3. Cancel an Event (Deletion):

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).

4. Search for an Event (Searching):

o Implement binary search to find an event by its ID. Ensure the array is sorted before searching.

5. Sort Events by Event ID (Sorting):

o Use Selection sort to sort the array of event IDs in ascending order.

4
5

Question 3: Dynamic Book Catalog System

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.

1. List All Books (Traversal):

o Write a function to display all book IDs using pointer arithmetic.

o The function should handle empty arrays efficiently.

2. Add a New Book (Insertion):

o Insert a new book ID at a specific position.

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.

▪ Divide by 2: 1800 / 2 = 900. So, the book ID to insert is 900.

▪ Position to Insert: Calculate the position by taking the sum of the digits of your birthdate (ddmmyy) mod
the current array size.

▪ Example: For birthdate 01/01/2000, the sum of digits is 0 + 1 + 0 + 1 + 0 + 0 = 2.

▪ Position to insert is 2 mod array size.

3. Remove a Book (Deletion):

o Remove a book by its ID and shift the elements left to maintain continuity.

4. Search for a Book (Searching):

o Implement a recursive linear search to find a specific book by its ID.

5. Sort the Catalog by Book ID (Sorting):

o Implement one of the following sorting algorithms using recursion:

▪ 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 The goal is to sort the book IDs in ascending order.

Hint: Explanation of Recursion in Sorting:

1. Recursive Bubble Sort:

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

2. Recursive Selection Sort:

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.

Example for Personalization:

For a student with RegNo = 2023007, Birthdate = 01/01/2000, and Name = Abdullah Bin Zarshaid:

• Question 1:

o Task ID to Insert: 2023007

o Insert at Position: Position 14 % array size (sum of RegNo digits is 14)

o Task ID to Delete: 2 (first digit of RegNo)

o Task ID to Search: 100 (birthdate 010100 % 1000)

• Question 2:

o Event ID to Insert: 148 (sum of ASCII values of first three RegNo digits)

o Insert at Position: 0 (sum of last two birth year digits)

• Question 3:

o Book ID to Insert: 900 (sum of ASCII values of first and last name, divided by 2)

o Insert at Position: 2 mod array size (sum of birthdate digits is 2)

Grading Criteria: Each question carries 30 marks each.


Criteria Points Description
Correctness of Solution 10 Does the program perform all
operations correctly
(insertion, deletion, traversal,
etc.)?
Pointer Usage 05 Are pointers used effectively
to manipulate the array and
perform operations?
Dynamic Memory 03 Are new and delete used
Management correctly for memory
allocation and deallocation?
Modular Code with Functions 03 Is the code modular, with
separate functions for each
task?
Edge Case Handling 03 Does the code handle edge
cases like deleting from an
empty array or inserting into
a full array?
Commenting and Code 06 Is the code well-commented
Readability and easy to understand?
6
7

Bonus Challenge:

You can attempt these challenges to explore more advanced data structures and future applications, they are not
compulsory:

For Cybersecurity Students:

Challenge: Implement a Simple Access Control System using 2D Arrays

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.

Your system should:

1. Store user IDs and passwords in a 2D array.

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.

Hint for Future Improvements:

• 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.

For Electrical Engineering Students:

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.

Your system should:

1. Store the power consumption of devices in 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.

Hint for Future Improvements:


7
8

• 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.

For Software Engineering Students:

Challenge: Build a Dynamic Version Control System using 2D Arrays

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.

Your system should:

1. Store file versions (version ID and file content) in a 2D array.

2. Implement a function to add a new version of the file.

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.

Hint for Future Improvements:

• 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.

Grading for Bonus Challenge:

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.

4. Efficiency: Solutions should be as efficient as possible given the use of arrays.

You might also like