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

ECE391 - Final - 26 12 2022 Solution

The document provides a set of problems related to data structures and algorithms. It includes questions on arrays, vectors, binary trees, recursion, classes and more. Code snippets or short answers are required for each problem. The problems cover a variety of fundamental CS topics.

Uploaded by

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

ECE391 - Final - 26 12 2022 Solution

The document provides a set of problems related to data structures and algorithms. It includes questions on arrays, vectors, binary trees, recursion, classes and more. Code snippets or short answers are required for each problem. The problems cover a variety of fundamental CS topics.

Uploaded by

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

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

int A[7]={5,8,3,-1,-4,3,7};
int *p = &A[1];
Write C++ code to do the following Answers
tasks.
Declare additional variables if needed.
Write “Error” if the code is wrong.

a) If the following code is executed, 0


what shows on the output screen?
cout << (*(p+2) == *(p+5));

for (int i = 0; i < 7; i++ ) {


b) Write a program to print all the if (A[i] > 0){
positive element of array A. cout << A[i] << endl;
}
}
int find_min (int *A, int n){
c) Write a function named find_min to int min = *A;
take two inputs: array A and length n of int pos = 0;
A. for (int i = 0 ; i < n ; i++){
This function returns the position of the if (min > *(A+i) ){
minimum element in A. min = *(A+i);
pos = i;
}
}
return pos;
}
int check_x (int *A, int n, int x){
d) Write a function named check_x to for (int i = 0 ; i < n ; i++){
take three inputs: array A, length n of A if (*(A+i)==x ){
and a number x. return 1;
This function checks if array A contains }}
x or not. If not, return 0, and vice versa, return 0;
return 1. }

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


Functions Big-Oh Short explanation
Notation
bool is_odd(int n)
{
if (n&0x01==1) O(1)

Write student’s name and ID into your exam paper Page 1/8
return true; The function has no loop.
else
return false;
}
void printSomething (int size)
{
for (int i = 0; i < size*size; i++) O(n2) The function has a loop of
{ size*size
cout<<”Hello World!”<<endl;
}
}

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


No. Operation Output Front <- Vector -> rear
1 push_back(3) - {3}
2 push_back(7) - {3,7}
3 front() 3 {3,7}
4 insert(0,8) - {8,3,7}
5 insert(1,-1) - {8,-1,3,7}
6 at(2) 3 {8,-1,3,7}
7 set(3,10) - {8,-1,3,10}
8 insert(3,4) - {8,-1,3,4,10}
9 pop_back() 10 {8,-1,3,4}
10 size() 4 {8,-1,3,4}

Problem 4: (10pts) Write a program in C++ to calculate the power of any number using
recursion.
Input: x, y => Output: xy
long int calculate_power(int x,int y)
{
long int result=1;
if (y == 0) return result;
result=x*calculate_power (x,y-1);
}
Problem 5: (40pts) Consider a class of LicensePlate.
1. (5pts) Write C++ code to declare the class with default information below:
Members Variable Type
Private nameUser string
purpose string
timeOfRegister unsigned
numberLicense unsigned[5]

Write student’s name and ID into your exam paper Page 2/8
LicensePlate() -
Public LicensePlate(string newNameUser, string newPurpose, -
unsigned newTimeOfRegister, unsigned
*newNumberLicense)
operator == (LicensePlate comparedLicense) bool
getInfor() void

Write your C++ code here:

class LicensePlate{
private:
string nameUser;
string purpose;
unsigned timeOfRegister;
unsigned numberLicense[5];
public:
LicensePlate();
LicensePlate(string newNameUser, string newPurpose, unsigned newTimeOfRegister,
unsigned *newNumberLicense);
bool operator == (LicensePlate comparedLicense);
void getInfor();
};
2. (5pts) Write C++ code for a default constructor of the LicensePlate class to
initialize the object with default information as below:
nameUser = “Unknown”;
purpose = “Unknown”;
timeOfRegister = 0000;
numberLicense = {0,0,0,0,0};

LicensePlate:: LicensePlate(){
nameUser = "Unknown";
purpose = "Unknown";
timeOfRegister = 0;
for (int i = 0; i< 5; i++){
numberLicense[i]=0;
}
}

3. (5pts) Write C++ code for a parameterized constructor of the LicensePlate class
to initialize the object with particular information.

LicensePlate:: LicensePlate(string newNameUser, string newPurpose, unsigned

Write student’s name and ID into your exam paper Page 3/8
newTimeOfRegister, unsigned *newNumberLicense){
nameUser = newNameUser;
purpose = newPurpose;
timeOfRegister = newTimeOfRegister;
for (int i = 0; i< 5; i++){
numberLicense[i]=newNumberLicense[i];
}
}

4. (5pts) Write C++ code for the overloading operator of the class of LicensePlate
to compare 2 license plates and return true if all the information are the same.

bool LicensePlate:: operator == (LicensePlate comparedLicense){


bool temp=true;
for (int i = 0;i<5;i++){
temp = temp && (numberLicense[i] == comparedLicense.numberLicense[i]);
}
return (nameUser == comparedLicense.nameUser) &&
(purpose == comparedLicense.purpose) &&
(timeOfRegister == comparedLicense.timeOfRegister) &&
temp;
}

5. (5pts) Write C++ code for the getInfor() function to show all the information of a
license plate.
void LicensePlate:: getInfor(){
cout << "Name: " << nameUser << endl;
cout << "Purpose: " << purpose << endl;
cout << "Time of Register: " << timeOfRegister << endl;
cout << "Number License: " ;
for (int i = 0; i<5; i++){
cout << numberLicense[i] ;
}
cout << "\n";
}

Write student’s name and ID into your exam paper Page 4/8
6. (5pts) Write a program to init a list of LicensePlate of which parameters are
shown in the table.
Parameters LicensePlate A LicensePlate B LicensePlate C
nameUser “Alice” “Tom” “Helen”
Purpose “Private” “Public” “Grab”
timeOfRegister 2021 2022 2019
numberLicense {3,4,6,1,2} {2,4,5,6,1} {4,5,1,0,1}

unsigned licenseA[5] = {3,4,6,1,2};


LicensePlate A("Alice","Private",2021,licenseA);
unsigned licenseB[5] = {2,4,5,6,1};
LicensePlate B("Tom","Public",2022,licenseB);
unsigned licenseC[5] = {4,5,1,0,1};
LicensePlate C("Helen","Grab",2019,licenseC);

list <LicensePlate> my_license_plate;


my_license_plate.push_back(A);
my_license_plate.push_back(B);
my_license_plate.push_back(C);

7. (5pts) Write C++ code show all the information of the license plates in the list in
question 6.
list <LicensePlate> :: iterator it;
for (it=my_license_plate.begin(); it!=my_license_plate.end(); it++){
it->getInfor();
}

Problem 6: (10pts) Given a binary tree as below.

Write student’s name and ID into your exam paper Page 5/8
A

D C

B F

E G

Answer the following questions:


1. What is the height of the tree? 3
2. What is the depth of the node D? 1
3. What is the number of external nodes ? 4
4. Find the pre-order traversal of the tree. ADBEGFC
5. Find post-order traversal of the tree. EGBFDCA
Problem 7: (5pts) Given an empty binary search tree.
1. (5pts) Insert into the binary search tree items with the following key (in this
order): 12, 4, 6, 15, 18, 8, 20, 13, 2, 24. Draw the final binary search tree after
insertion.

Write student’s name and ID into your exam paper Page 6/8
Write student’s name and ID into your exam paper Page 7/8

You might also like