0% found this document useful (0 votes)
487 views7 pages

CCS0015L (Data Structures and Algorithms) : Practical Exam

Uploaded by

202310350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
487 views7 pages

CCS0015L (Data Structures and Algorithms) : Practical Exam

Uploaded by

202310350
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

COLLEGE OF COMPUTER STUDIES AND MULTIMEDIA ARTS

CCS0015L
(DATA STRUCTURES AND ALGORITHMS)

PRACTICAL EXAM

1
Student Name / Group Aaron Castro
Name:
Name Role
Members (if Group):

Section:
TC02
Professor:
Julius Claour
Directions: Complete the program below that satisfy the given problem specification and output. Look for
the functions or sections of the function with //TODO. Paste your completed source code in the Code
portion then highlight the statement(s) that you filled in the incomplete program with color yellow.

Problem: You are tasked to complete the program that will implement a StudentSet ADT with the following
functions:

 StudentSet(): The constructor initializes an empty set with size 0.


 add(string name): This function takes a string parameter 'name' and adds it to the set if the set is
not full and 'name' does not already exist in the set. If the set is full, it prints an error message.
 remove(string name): This function removes a string parameter 'name' from the set if it exists in the
set. If the 'name' is not in the set, it does nothing.
 contains(string name): This function takes a string parameter 'name' and checks if it exists in the
set. It returns a boolean value 'true' if the 'name' exists in the set and 'false' otherwise.
 print(): This function prints the current set of students. It iterates over the set and prints each
student's name.

Given Program:
#include <iostream>
#include <string>
using namespace std;

const int MAX_SIZE = 5;

class StudentSet {
private:
string set[MAX_SIZE];
int size;

public:
StudentSet() {
size = 0;
}

void add(string name) {


if (size >= MAX_SIZE) {
cout << "Error: Set is full." << endl;
return;
}

// Check if the item already exists in the set


//TODO

// Add the item to the set


set[size] = name;

CCS0015L-Data Structures and Algorithms Page 2 of


7
size++;
}

void remove(string name) {


// Search for the item in the set
int index = -1;
for (int i = 0; i < size; i++) {
if (set[i] == name) {
index = i;
break;
}
}

// If the item is found, remove it from the set


if (index >= 0) {
for (int i = index; i < size - 1; i++) {
set[i] = set[i + 1];
}
size--;
}
}

bool contains(string name) {


// Search for the item in the set
//TODO
}

void print() {
//TODO
}
};

int main() {
StudentSet s;
string name;
// Add some students to the set
s.add("Akira");
s.add("JL");
s.add("Gelo");
s.add("Mikki");
s.add("Nate");

// Print the set


s.print();

// Check if a student is in the set


cout<<"Who do you want to check? ";
cin>>name;

if (s.contains(name)) {
cout << "Yipee!!! He is in the set." << endl;
} else {
cout << "Oops! He is not in the set." << endl;

CCS0015L-Data Structures and Algorithms Page 3 of


7
}

// Remove a student from the set


cout<<"Who do you want to remove? ";
cin>>name;
s.remove(name);

// Print the updated set


s.print();

// Check if a student is in the set


if (s.contains(name)) {
cout << "Yipee! He is in the set." << endl;
} else {
cout << "Saddd!!! "<<name<<" is not anymore in the set." << endl;
}

return 0;
}

Sample Output:

Code:

#include <iostream>
#include <string>
using namespace std;

const int MAX_SIZE = 5;

class StudentSet
{
private:
string set[MAX_SIZE];
int size;

public:
StudentSet()
{
size = 0;
}

CCS0015L-Data Structures and Algorithms Page 4 of


7
void add(string name)
{
if (size >= MAX_SIZE)
{
cout << "Error: Set is full." << endl;
return;
}

// Check if the item already exists in the set


for (int i = 0; i < size; i++)
{
if (set[i] == name)
{
cout << "Error: " << name << " already exists in the set." <<
endl;
return;
}
}

// Add the item to the set


set[size] = name;
size++;
}

void remove(string name)


{
// Search for the item in the set
int index = -1;
for (int i = 0; i < size; i++)
{
if (set[i] == name)
{
index = i;
break;
}
}

// If the item is found, remove it from the set


if (index >= 0)
{
for (int i = index; i < size - 1; i++)
{
set[i] = set[i + 1];
}
size--;
}
}

bool contains(string name)


{
// Search for the item in the set
for (int i = 0; i < size; i++)

CCS0015L-Data Structures and Algorithms Page 5 of


7
{
if (set[i] == name)
{
return true;
}
}
return false;
}

void print()
{
// Print the current set of students
cout << "Current set of students:" << endl;
for (int i = 0; i < size; i++)
{
cout << set[i] << endl;
}
}
};

int main()
{
StudentSet s;
string name;
// Add some students to the set
s.add("Akira");
s.add("JL");
s.add("Gelo");
s.add("Mikki");
s.add("Nate");

// Print the set


s.print();

// Check if a student is in the set


cout << "Who do you want to check? ";
cin >> name;

if (s.contains(name))
{
cout << "Yipee!!! " << name << " is in the set." << endl;
} else {
cout << "Oops! " << name << " is not in the set." << endl;
}

// Remove a student from the set


cout << "Who do you want to remove? ";
cin >> name;
s.remove(name);

// Print the updated set


s.print();

CCS0015L-Data Structures and Algorithms Page 6 of


7
// Check if a student is in the set
if (s.contains(name))
{
cout << "Yipee! " << name << " is in the set." << endl;
} else
{
cout << "Saddd!!! " << name << " is not anymore in the set." << endl;
}

return 0;
}

Output:(screenshot of the output)

CCS0015L-Data Structures and Algorithms Page 7 of


7

You might also like