Professional Documents
Culture Documents
School of ICT
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
The corresponding C++ code for this algorithm would look like this:
Listing 1: student.cpp
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;
s_two . next =5
cl . record [11]= s_two ;
head =11;
}
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.
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
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 ;
}
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.