Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

data structeure and algorithm laboratory, Papers of Data Structures and Algorithms

data structeure and algorithm laboratory

Typology: Papers

2024/2025

Uploaded on 04/23/2025

rachit-bansal-2
rachit-bansal-2 🇮🇳

2 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Birla Institute of Technology and Science, Pilani Hyderabad Campus
CS F211: Data Structures and Algorithms
2nd Semester 2024-25 Lab No:3 (Dynamic Arrays)
General Tips
Use of STL like vector class is strictly prohibited in this lab. You should not use any inbuilt data
structure/ algorithm to do any task.
Indent your code appropriately and use proper variable names, if not already provided in the
question. Also, use comments wherever necessary.
Make sure to debug your code after every task or after every code block to avoid having to debug
your entire code at once in the last minute.
Overview:
We have already covered basics of C++ programming and are well versed will Object Oriented Programming
concepts like Encapsulation, Abstraction, Inheritance, Polymorphism etc. in the previous two labs. For this lab, our
goal is to learn about Arrays which are concrete data structures. You must have learnt Arrays in computer
programming course done in earlier semesters. However, in this lab we will learn how to use Arrays to build an
application using Objects that will automatically adjust its’ size. While C++ STL has a vector as a sequence
container (data structure that can store similar types) that can change its’ size dynamically, in this lab you will
implement your own dynamic array without using vectors.
Program 1 (DynamicArray.cpp attached):
This task is about implementing a dynamic array class with member functions for insert, delete, search, sort, grow,
shrink etc. The below C++ code fragment has complete functions and classes for all the required objects as we
discussed in the class. The complete cpp file is given in the class page (DynamicArray.cpp). You should see the
output as given below:
Output:
Task 1:
Add a new method reverse () to the Dynamic1DArray class given in the attached code that reverses the order of
elements in the dynamic array in place.
Use case:
arr.display(); // [3, 5, 7, 15]
pf3
pf4
pf5

Partial preview of the text

Download data structeure and algorithm laboratory and more Papers Data Structures and Algorithms in PDF only on Docsity!

Birla Institute of Technology and Science, Pilani Hyderabad Campus

CS F211: Data Structures and Algorithms

nd

Semester 2024-25 Lab No:3 (Dynamic Arrays)

General Tips

  • Use of STL like vector class is strictly prohibited in this lab. You should not use any inbuilt data

structure/ algorithm to do any task.

  • Indent your code appropriately and use proper variable names, if not already provided in the

question. Also, use comments wherever necessary.

  • Make sure to debug your code after every task or after every code block to avoid having to debug

your entire code at once in the last minute.

Overview:

We have already covered basics of C++ programming and are well versed will Object Oriented Programming concepts like Encapsulation, Abstraction, Inheritance, Polymorphism etc. in the previous two labs. For this lab, our goal is to learn about Arrays which are concrete data structures. You must have learnt Arrays in computer programming course done in earlier semesters. However, in this lab we will learn how to use Arrays to build an application using Objects that will automatically adjust its’ size. While C++ STL has a vector as a sequence container (data structure that can store similar types) that can change its’ size dynamically, in this lab you will implement your own dynamic array without using vectors.

Program 1 (DynamicArray.cpp attached):

This task is about implementing a dynamic array class with member functions for insert, delete, search, sort, grow, shrink etc. The below C++ code fragment has complete functions and classes for all the required objects as we discussed in the class. The complete cpp file is given in the class page (DynamicArray.cpp). You should see the output as given below:

Output:

Task 1: Add a new method reverse () to the Dynamic1DArray class given in the attached code that reverses the order of elements in the dynamic array in place. Use case: arr.display(); // [3, 5, 7, 15]

arr.reverse(); arr.display(); // [15, 7, 5, 3]

Program 2 (GameEntry.cpp as discussed in the class is attached):

This task is about the GameEntry class discussed in the class. Pl. refer to the GameEntry.cpp source file uploaded along with this lab sheet. Output of the run is as shown below (as discussed):

Task 2:

Modify the code given so that you will have another option for printing how many number of entries are there for each player at any point in time during the run. The code fragment to complete this task is given partly as below with comments. Your task here is to understand the code fragment and complete the missing statements. If you want to use a different logic to implement the task, you are welcome to do so.

void Scores::printPlayersCount() { // isPicked is a table that helps us record whether a player was already considered for calculating his/her count. bool isPicked[numEntries]{false};

for (int i = 0; i < numEntries; i++) { if (isPicked[i]) // this player was considered previously continue;

showOptions(); cin >> option; switch (option) { case 1: cout << "Enter Player Name and Score\n"; cin >> playerName >> score; scoresObj.add(GameEntry(playerName, score)); break; case 2: int index; cout << "Enter the index\n"; cin >> index; scoresObj.remove(index); break; case 3: scoresObj.printAllScores(); break; case 4: scoresObj.printPlayersCount(); break; case 5: return EXIT_SUCCESS; } } }

You should get the output as shown below:

Task 3:

Modify the above code to display unique entries for each player. Part solution is given in GameEntry_Unique.cpp. You should get the output as shown below:

Task 4:

Modify the code given so that you will have another option for printing the name of players having sum of all entries in a specified range i.e. min value <= sum of player’s entries <= max value at any point in time during the run. The range of scores (max value, min value) will be taken as input by the user. The code fragment to complete this task is given partly as below with comments. Your task here is to understand the code fragment and complete the missing statements.

void Scores::printPlayersInScoreRange(int maxValue, int minValue) { // isPicked is a table that helps us record whether a player was already // considered for calculating his/her count. bool isPicked[numEntries]{false};

for (int i = 0; i < numEntries; i++) { if (isPicked[i]) // this player was considered previously continue;

//Initialize the sum with ith player's score(Task 4) int sum;

string playerName = entries[i].getName();

for (int j = i + 1; j < numEntries; j++) { if (isPicked[j]) // this player was considered previously continue;

//Compare the name of ith player with jth player and if they are //equal then update the sum and also //update the isPicked table's flag for jth player to True. If //they are not equal, then skip and go to next jth player (Task4) }

Task 5:

//Check if sum of scores of current player is between minValue

cout << "Enter max value and min value\n"; cin >> maxValue >> minValue; scoresObj.printPlayersInScoreRange (maxValue, minValue); break; case 5: return EXIT_SUCCESS; } } }