You are on page 1of 5

The ICT University

Spring 2023 Continuous Assessment №2

School of ICT

Course Code: ICT 1222


Course Title: Algorithms and Data Structures II
Instructor: Engr. Daniel Moune

Instructions:

1. Your assignment should represent your own effort. However, you are not expected to
work alone. It is fine to discuss the exercises and try to find solutions together, but each
student shall write down and submit his/her solutions separately.
2. Please, include name, matricule, ICTU email and phone number in your submission.
3. For a each question, provide a textual explanation of your solution to the problem.
4. Always provide a pseudo-code version of your algorithms
5. Provide a translation of your pseudo-code in C++ code directly in your answer sheet
6. This assessment is made of 04 pages.
7. Convert your Answer sheet into PDF before uploading to Moodle
8. Documents in WORD format will not be accepted !!!
9. Make your answer sheet conformant to the template attached with this assessment

May 9, 2023
Algorithms and data structures II: CA 2 May 9, 2023

Exercise 1: Designing structures and classes

In C programming, struct is a composite data type that groups together variables


of different data types under a single name. This allows you to create a custom data
type that represents a collection of related data. For instance, imagine you would
like to describe an algorithm that would initialize some student records in a system,
you would proceed like this:
Algorithm 1: Create a new student
Data type:
record Student:
name: string
age: integer
major:string
next: integer
end
Input:
s : student
Output:
none
Begin
s.name ← ”JohnDoe”
s.age ← 24
s.major ← ”ICT ”
s.next ← −1
End

The corresponding C++ code for this algorithm would look like this:
Listing 1: student.cpp

# include < iostream >


# include < string >

using namespace std ;

struct Student {
string name ;
int age ;
string major ;
int next ;
};

struct ClassList {
string course_title ;
Student record [100];
int size ;
int head = 0;
};

int main () {

ClassList cl ;
cl . course_title = " Algorithms " ;
cl . size = 0;

Student s_one ;
s_one . name = " John � Doe " ;
s_one . age = 24;
s_one . major = " ICT " ;
s_one . next = -1;

Student s_two ; 1
s_two . name = " Bob � Bennet " ;
s_two . age = 23;
s_two . major = " ICT " ;
s_two . next = -1;

cl . record [5]= s_one ;


head = 5;

s_two . next =5
cl . record [11]= s_two ;
head =11;
}

Engr. Daniel Moune page 1/ 4


Algorithms and data structures II: CA 2 May 9, 2023

In this activity, we will describe algorithms allowing to manipulate a list of students


using a data structure called ‘ClassList’ .

1. What is the value of the head of the ‘ClassList’ at the end of the execution of the
program presented earlier?
What is the value of the size of the ‘ClassList’ at the end of the execution of the
2.
program presented earlier? Is it correct according to the logic of the program? If not
what should have been done to fix it?
3. What is the maximum capacity of the ‘ClassList’ ? (justify your answer with a solid
argument)

4. What is the rank of the student named ‘John Doe’ in the ‘ClassList’ ? (Assume
the rank of the head of the list is 01)

For each of the following questions provide an algorithm in pseudo-code and the
corresponding C++ program that corresponds to the signature provided.

int addStudentHead(ClassList cl, string name, int age, string major)


5. add a student at the head of a class list and return the new size of the list. If the
previous size of the list was k, return k + 1. if the capacity is exceeded , do not
add anything and return −1.
int addStudentTail(ClassList cl, string name, int age, string major)
6. add a student at the tail of a class list and return the new size of the list. If the
previous size of the list was k, return k + 1. if the capacity is exceeded , do not
add anything and return −1.
int getStudentRank(ClassList cl, student s)
7. return the rank of the student ‘s’ in the class list. For instance rank of ‘Bob Bennet’
in the sample code given is 01. this student is at the head of the list. If the student
is not in the list, return −1.
int getStudent(ClassList cl, student s, int rank)
8. provide in ‘s’ the records of a student located at a particular rank in the class list.
For instance records of ‘Bob Bennet’ are located at rank 01.If the rank exceeds
the list size, return −1.
int insertStudent(ClassList cl, string name, int age, string major, int
rank)
add the records of a student at a particular rank in the class list and returns the
9.
new size of the list. if some other records were already stored at this rank, then all
records from this rank to the end of list will be shifted one rank up. If the capacity
is exceeded or the rank exceeds the list size, do not insert anything and return
−1.
int removeStudent(ClassList cl, int rank)
remove records of a student at a particular rank in the class list and returns the new
10. size of the list. if some other records were already stored at this rank, then all records
from this rank to the end of list will be shifted one rank down. If the class list is
empty or the rank exceed the list size, do not remove anything and return −1

Engr. Daniel Moune page 2/ 4


Algorithms and data structures II: CA 2 May 9, 2023

Exercise 2:
For this second activity, we would like to modernize the data structure of Exercise
1 with an object-oriented data structure. In C++ programming, a class is a
user-defined data type that encapsulates data and algorithms that operate on that
data. A class is a blueprint for creating objects, which are instances of the class.
Classes can be combined with pointers to provide dynamic data structures like in
the example provided below:
Algorithm 1: Create a new student
Data type:
record Student:
name: string
age: integer
major:string
next: ↑Student
end
Input:
s : student
Output:
none
Begin
s.name ← ”JohnDoe”
s.age ← 24
s.major ← ”ICT ”
s.next ← N U LL
End

The corresponding C++ code for this algorithm would look like this:
Listing 1: student.cpp

# include < iostream >


# include < string >

using namespace std ;

class Student {
private :
string name ;
int age ;
string major ;
Student * next ;
public :
Student ( string name , int age , string major ) {
this . name = name ;
this . age = age ;
this . major = major ;
next = null_ptr ;
}
};

class ClassList {
public :
string course_title ;
Student * head ;
};

int main () {
ClassList cl ;
cl . course_title = " Algorithms " ;
Student s_one ( " Joe � Barton " ,24 , ICT ) ;
Student s_two ( " Klint � Faucet " ,23 , ICT ) ;
cl . head = & s_one ; 1
s_two . next = & s_one ;
cl . head = & s_two ;
}

Engr. Daniel Moune page 3/ 4


Algorithms and data structures II: CA 2 May 9, 2023

1. Compare the number of lines of code of ‘Exercise 1’ with the number of lines of
code of ‘Exercise 2’ . What does this suggest to you?
2. What is the size of the ‘ClassList’ at the end of the execution of the program
presented earlier?
3. What is the maximum capacity of the ‘ClassList’ ? (justify your answer with a solid
argument)
4. What is the name of the student which is at the tail of ‘ClassList’ at the end of
the execution of the program?

For each of the following questions provide an algorithm in pseudo-code and the
corresponding C++ program that corresponds to the signature provided.

int addStudentHead(ClassList cl, string name, int age, string major)


5. add a student at the head of a class list and return the new size of the list. If the
previous size of the list was k, return k + 1.If the capacity is exceeded , do not add
anything and return −1.
int addStudentTail(ClassList cl, string name, int age, string major)
6. add a student at the tail of a class list and return the new size of the list. If the
previous size of the list was k, return k + 1.If the capacity is exceeded , do not add
anything and return −1.
int getSize(ClassList cl)
7. return the ‘size’ in the class list. For instance the ‘size of the class list’ in the
sample code given is 02.
int getStudentRank(ClassList cl, student s)
8. return the rank of the student ‘s’ in the class list. For instance rank of ‘klint Faucet’
in the sample code given is 01. this student is at the head of the list.
int getStudent(ClassList cl, student s, int rank)
9.
provide in ‘s’ the records of a student located at a particular rank in the class list.
For instance records of ‘klint Faucet’ are located at rank 01.
int insertStudent(ClassList cl, string name, int age, string major, int
rank)
10. add the records of a student at a particular rank in the class list and returns the
new size of the list. if some other records were already stored at this rank, then all
records from this rank to the end of list will be shifted one rank up. If the capacity
is exceeded , do not insert anything and return −1.
int removeStudent(ClassList cl, int rank)
remove records of a student at a particular rank in the class list and returns the new
11. size of the list. if some other records were already stored at this rank, then all records
from this rank to the end of list will be shifted one rank down. If the class list is
empty or the rank exceed the list size, do not remove anything and return −1

Engr. Daniel Moune page 4/ 4

You might also like