HW Ông Vinh
HW Ông Vinh
HW Ông Vinh
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=
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 mn 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.
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;
};
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;
}
};
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>
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;
}
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;
}
};