ECE391: Computer System Engineering Homework 1: C++ Code Variables Check If Error
ECE391: Computer System Engineering Homework 1: C++ Code Variables Check If Error
Engineering
Homework 1
1. Determine the content of variables after the following C++ code
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;
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
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;}
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.
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.
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.
8. Write a short C++ function that takes an integer n and returns the sum of all the integers smaller
than n.
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.