You are on page 1of 8

Ho Chi Minh City University of Technology

Department of Electrical and Electronics Engineering


MIDTERM EXAMINATION – Class TT01 ECE391: Computer System Engineering
Semester 2, 2019-2020, 29/05/2020 Duration: 60 minutes
Score Students are allowed to use one A4 page with two sides for Lecturer
reference.
Books and other documents are not allowed to use.

This examination consists of 5 pages Trương Quang


Vinh

Student name: ID:

Problem 1: (20pts) Given a following C++ declaration


int Arr[] = {2,4,6,8,10,12};
int *p = &Arr[0];
Write C++ code to do the following tasks. Answers
(Declare additional variables if needed.
Write “Error” if the code is wrong.)
a) If the following code is executed, what Error: Ivalue required as unary ‘&’ operand
shows on the output screen?
cout << **&*&(p+4);

b) Use the pointer p and FOR loop to


calculate the sum of all elements in the int sum = 0;
array Arr. int i = 0;

int a= sizeof(Arr)/ sizeof(int);

cout << a << endl;

for ( i = 0; i < (sizeof(Arr)/


sizeof(int)); i++)
{
sum += *p;
p++;
}

cout << sum;

1
c) Use the pointer p to take the product of
Arr[1] and Arr[3]. int temp, product;

temp = *(p+1);

product = temp * (*(p+3));


cout << product;

d) Write C++ program using pointer p to


calculate the result of Arr[2] power Arr[4]. int temp , result = 1;

for (int i = 0 ; i < *(p+4); i++)


{
p = &Arr[0];
result *= *(p+2);
}

cout << result;

Problem 2: (20pts) Find the Big-Oh Notation of the following functions


Functions Big-Oh Short explanation
Notation
50n + n2 an Because 50n + n2 <= C*an

Enery integer n > n0.

C and a are constant.

void bubbleSort(int a[], int n) N2 With outer loop, I = 0, 1, …n -1.


{ Because outer loop excutes n-
for (int i = 0; i < n; i++) times
{ for (int j = 0; j < n; j++)
{ if (a[j] > a[i]) With inner loop, I = 0, 1, …,
{ int tmp = a[i]; n -1.
a[i] = a[j]; Because inner loop excutes n -
a[j] = tmp; times
}
}
} So this function run n2 times.

2
Problem 3: (10pts) Fill in the following table of the Stack operations.

No. Operation Output Stack -> Top


1 push(100) - 100
2 push(50) - 100, 50
3 pop() - 100
4 pop() - empty
5 pop() error empty
6 push(200) - 200
7 size() 1 200
8 pop() - empty
9 isEmpty() true empty
10 size() 0 Empty

Problem 4: (25pts) Consider the class Face in the mobile app for face recognition:

1. (5pts) Write C++ code to declare the class Face with the default information as below:
Members Variable Type
Private Face_ID string
Gender bool
Bounding_box Integer array
size of 4
Face() -
Public Face(string faceNewID, bool newGender, int -
newBoundingBox[4])
operator = = (Face face) bool
face_getPosition() void

Write your C++ code here:


#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

class Face
{
private:
string Face_ID;
bool Genfer;

3
int Bounding_box[4];

public:

Face();
Face(string faceNewId, bool newGender, int newBoundingBox[4]);
bool operator ==(const Face& face1);
void face_getPosition();
}

int main()
{
return 0;
}

2. (5pts) Write C++ code for a constructor of the class Face to initialize a new variable of
Face with the default information as below:
Face_ID = “Unknown ID”
Gender = 0
Bounding_box = [0,0,0,0]

Face :: Face()
{
this->Face_ID = "Unkown ID";
this->Gender = 0;
int i;

for (i = 0; i < sizeof(Bounding_box)/ sizeof(int); i++)


{
this->Bounding_box[i] = 0;
}
}

3. (5pts) Write C++ code for a constructor of the class Face to initialize a new variable of
Face with the information of ID, gender and bounding box.

Face::Face(string faceNewId, bool newGender, int newBoudingBox[])


{
this->Face_ID = faceNewId;
this->Gender = newGender;
int i = 0;
for (i =0; i < 4; i++)

4
{
this->Bounding_box[i] = newBoudingBox[i];
}

4. (5pts) Write C++ code for the operator of the class Face to compare the two faces and
return true if two faces have the same ID and bounding box.

bool Face::operator ==(const Face& face1)


{
if (this->Face_ID == face1.Face_ID )
{
int i;

for( i = 0; i < 4;)


{
if(this->Bounding_box[i] == face1.Bounding_box[i])
{
i++;
}
else
{
return false;
}
}
return true;
}
else
return false;
}

5. (5pts) Write C++ code for the function Face::getPosition() to show 4 corners of the
bounding box of the face on the screen.
void Face::face_getPosition()
{
Face a;
}

Problem 5: (25pts) Given a list Face_list which is described as follow to store the list of faces.
list<Face*> Face_list;

1. (10pts) Write C++ code to add 3 faces with the information as follow to the Face_list:

5
No. Face_ID Gender Bounding box
1 Face_0001 0 [60,45,378,516]
2 Face_0002 1 [400,112,655,493]
3 Face_0003 1 [150,80,200,130]

#include <iostream>
#include <cstdlib>
#include <string>
#include <list>

using namespace std;


using std:: string;
using std:: list;

class Face
{
private:
string Face_ID;
bool Gender;
int Bounding_box[4];

public:

Face();
Face(string newFace_ID, bool newGender, int newBounding_box[4]);

~Face()
{
cout << "Finished !!!";
}

};

Face:: Face()
{
int i;
this->Face_ID = "Unknow";
this->Gender = 0;
for (i = 0; i < 4; i++)
{
this->Bounding_box[i] = 0;
}
}

Face::Face(string newFace_ID, bool newGender, int newBounding_box[4])

6
{
int i;
this->Face_ID = newFace_ID;
this->Gender = newGender;
for(i = 0 ; i < 4 ; i++)
{
this->Bounding_box[i] = newBounding_box[i];
}
}
int main()
{
int arr_a[4] = {60,45,378,516};
int arr_b[4] = {400,112,655,493};
int arr_c[4] = {150,80,200,130};

Face a;
Face *b = new Face("Face_0001", 0, arr_a);
Face *c = new Face("Face_0002", 1, arr_b);
Face *d = new Face("Face_0003", 1, arr_c);

list<Face*>Face_list;
Face_list.push_front(b);
Face_list.push_back(c);
Face_list.push_back(d);
return 0;
}

2. (5pts) Write a C++ code to remove the Face “Face_0003” out of the Face_list, and add a
new Face (“Face_0004”, 0, [ 190, 100, 320, 240]) into the Face_list.
Face_list.pop_back();
int arr_d[4] = {190, 100, 320, 240};
Face e("Face_0004", 0, arr_d);

Face_list.push_back(&e);

3. (5pts) Write C++ code to check the number of faces in the Face_list, and show on the
screen.
cout <<"The number of elements in list: "<< Face_list.size()<<endl;

7
4. (5pts) Write C++ code to show all position of the faces in Face_list on the screen by
using the Iterator p.

list <Face*>:: iterator p;

for(p = Face_list.begin(); p != Face_list.end(); ++p)


{
cout << *p<< endl;
}

--------------------------------------------------------END------------------------------------------------------

You might also like