



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
data structeure and algorithm laboratory
Typology: Papers
1 / 7
This page cannot be seen from the preview
Don't miss anything!
General Tips
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.
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:
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; } } }