ECE391 - Final - 26 12 2022 Solution
ECE391 - Final - 26 12 2022 Solution
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.
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 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
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.
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.
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}
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();
}
Write student’s name and ID into your exam paper Page 5/8
A
D C
B F
E G
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