HW Ông Vinh

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

ECE391: Computer System Engineering

Homework 1
1. Determine the content of variables after the following C++ code
C++ code Variables Check if error
int a[] = {1,4,7,8,9}; a[] = {1,4,7,8,9}; 
int b[] = {2,3,6,7,11}; b[] = {2,3,6,7,11};
int *p, *q;
int X;
p = a;
q = b;
*p = 5; a[] = {5, 4, 7, 8, 9} 
*q = 8; b[] = {8, 3, 6, 7, 11}
X = **&*&p; X=5 
X = *&*q; X=8 
X = ++p; X= 
X = *p--; X=5 
p = *&*q; p= 

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

2. Consider the following attempt to allocate a 10-element array of pointers to doubles


and initialize the associated double values to 0.0. Rewrite the following incorrect code
double* dp[10];
for (int i=0; i<10; i++) dp[i] = 0.0;

Rewrite:
double dp[10];
for (int i=0; i<10; i++)
dp[i] = 0.0;
3. Write a C++ function printArray(A, m ,n) that prints an mn two dimensional array A
of integers, declared to be “int** A”, to the standard output. Each of the m rows
should appear on separate line
#include <iostream>
using namespace std;
void printArray(int A[][3],int m ,int n)
{
int i,j;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
cout << A[i][j] <<' ';
cout<<endl;
}

4. What is different about the behavior of the following two function f() and g() which
increment a variable and print its value?
void f(int x)
{ std::cout << ++x;}
void g(int& x)
{ std::cout << ++x;}

In f() function, x is created in f() as a local variable. The compiler will copy the value of
input variable to x when the function is called. The original variable will not be affected. On the
other hand, g() function works directly with the memory location of the input variable.

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

5. Write a C++ class, AllKinds, that has three member variables of type int, long, and float,
respectively. Each class must include a constructor function that initializes each
variable to a nonzero value, and each class should include functions for setting the
value of each type, and computing and returning the sum of each possible
combination of types.
using namespace std;
class AllKinds
{
public:
class int_num
{

public:
int a;
int_num()
{
a=1;
};
void get()
{
cout<<"Enter integer a = ";
cin >> a;
}

};
class long_num
{
public:
long b;
long_num()
{
b=2000;
}
void get()
{
cout<<"Enter b = ";
cin >> b;

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

};

class float_num
{
public:
float c;
float_num()
{
c=3.1415;
}
void get()
{
cout<<"Enter c = ";
cin >> c;
}
};

int_num a;
long_num b;
float_num c;

long ab()
{
return a.a+b.b;
}
double ac()
{
return a.a+c.c;
}
double bc()
{
return b.b+c.c;
}
};

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

6. Write a short C++ function, isMultiple(), that takes two long values, n and m, and
returns true if and only if n is multiple of m, that is, n=m*i for some integer i.
#include <iostream>

using namespace std;

bool isMultiple(long m, long n)


{
double d=double(n)/double(m);
int p=int(n/m);
double dp=d-p;
if (dp==0)
return 1; //true
else
return 0;
}
7. Write a short C++ function, isTwoPower(), that takes and int i and returns true if and
only if i is a power of 2. Do not use multiplication or division, however.
using namespace std;
bool isTwoPower(int i)
{
if((i&(i-1))==0)
return true;
else
return false;
}

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

8. Write a short C++ function that takes an integer n and returns the sum of all the
integers smaller than n.
int sum(int n)
{
int i, sum=0;
for (i=0;i<n;i++)
{
sum += i;
}
return sum;
}

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

9. a) Declare the class STUDENT, which has the following members:


string ID;
string name;
unsigned int birthyear;

b) Write a constructor to assign the default values for ID, name, and birthyear;
ID = “0000”;
Name = “NO NAME”;
Birthyear = “0000”;
c) Write the overloading operator == to compare two students A and B, and return a
bool value. In this operator, each member of the class STUDENT is compared with each other.
d) Write the overloading operator = to assign copy the content of the class student A
to the class student B.
#include <iostream>
using namespace std;
class STUDENT
{
private:
string ID;
string name;
unsigned int birthyear;
public:
STUDENT() //constructor
{
ID = "0000";
name = "NO NAME";
birthyear = 0;
}
operator==( STUDENT & y)
{
return ID==y.ID && name==y.name && birthyear==y.birthyear;
}

STUDENT & operator=( STUDENT &y)


{
birthyear=y.birthyear;
ID=y.ID;
name=y.name;

Instructor: Dr. Truong Quang Vinh


Ho Chi Minh City University of Technology Department of Electronics and Electrical Engineering

};

Instructor: Dr. Truong Quang Vinh

You might also like