Data structure – List Programming Assignment 3
Adnan Salman
Objective: To learn how to use array-based list.
1) The array-based list discussed in class displays a message and exits the program if we try to
insert an item into a full list. Instead, we can grow the size of the list whenever it is full. In
this assignment, your task is to double the size of the array list whenever we try to insert it
into a full list.
2) Write a class Student that has the following 3 attributes
class Student {
public:
// add any necessary public methods
private:
string name;
string id;
float gpa;
}
Then generate 1000,000 object of type student and insert them in an ordered array list. Each
student object must have a unique id. Use the following functions to generate unique Ids.
string generateUniqueId() {
string uniqueId;
time_t now = time(nullptr);
uniqueId = to_string(now);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dist(1000, 9999);
int randomNumber = dist(gen);
uniqueId += to_string(randomNumber);
return uniqueId;
}
Also, you can use the following function to generate names of some length.
string generateArbitraryName(int length) {
string name;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dist('a', 'z');
for (int i = 0; i < length; ++i) name += char(dist(gen));
return name;
}
Measure the time needed to search for an item that is not in the list using the binary search
method compared to the linear search. You can use one of the following codes to measure the
required time:
using namespace std;
using namespace std::chrono;
int main() {
auto start = high_resolution_clock::now();
// code to time
auto stop = high_resolution_clock::now();
duration<double> duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: " << duration.count() << " microseconds" <<
endl;
return 0;
}
Or
#include <time.h>
using namespace std;
int main() {
time_t begin, end;
time(&begin);
// code to time
time(&end);
time_t elapsed = end - begin;
printf("Time measured: %ld seconds.\n", elapsed);
return 0;
}
Note: co compare different student objects for the binary search, you need to overload the
three operator (==, <, >) of the class Student.
3) The function remove of the class UnorderedArrayList removes only the first occurrence of an
element. Add the function removeAll to the class UnorderedArrayList that would remove all
occurrences of a given element. Also, write the definition of the function removeAll and a
program to test this function
4) Add a function findAll (item) to the class UnorderedArrayList that finds all occurrences of
the item “item” and returns a list of integers that contains the locations of these occurrences.
5) Add the function min to the class UnorderedArrayList to return the smallest element of the
list. Also, write the definition of the function min and a program to test this function.
Grading
1) If the program doesn’t compile, you will get 0, otherwise, each part worth 12 points = 5 *
12 = 60
2) 15 points comments + program organization.
3) 15 points if there is a submission.
4) 10 points up to the grader to decide.
Good luck