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

ECE391 Homework 1

The document contains the homework assignment of a student named Nguyễn Chí Thành from Ho Chi Minh City University of Technology. The homework contains 9 questions related to C++ programming. The questions cover topics like arrays, pointers, classes, functions, operators overloading. For each question, the student provided the answer in C++ code along with explanations.

Uploaded by

Chí Thành
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)
33 views7 pages

ECE391 Homework 1

The document contains the homework assignment of a student named Nguyễn Chí Thành from Ho Chi Minh City University of Technology. The homework contains 9 questions related to C++ programming. The questions cover topics like arrays, pointers, classes, functions, operators overloading. For each question, the student provided the answer in C++ code along with explanations.

Uploaded by

Chí Thành
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

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

Engineering

NAME: NGUYỄN CHÍ THÀNH_1951018

ECE391: Computer System Engineering

Homework 1
1. Determine the content of variables after the following C++ code

ANSWER:

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
X = *p--; X=1 
p = *&*q; p= X

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;

ANSWER: Correct the previous 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

ANSWER: Code is:

#include <iostream>
#include <stdio.h>

Instructor: Dr. Truong Quang Vinh


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

//Function printArra to print elements of array


void printArray(int** A,int m,int n)
{
//Printing elements of the array
cout<<"\nThe elements of the array are:"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout<<A[i][j]<<" ";
}
cout<<endl;
}
}

int main()
{ //Defining m and n
int m = 5;
int n = 6;
//Declaring array with two dimensions
//Here m is rows
int** A = new int*[m];
//Allocating columns of size n to dynamic array
for (int i=0; i < m; i++)
A[i] = new int[n];

//Taking input of elements in array


cout<<"Enter elements of the array:"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin>>A[i][j];
}
}

//Calling the function


printArray(A,m,n);
}
4. What is different about the behavior of the following two function f() and g() which increment a
variable and print its value?

Instructor: Dr. Truong Quang Vinh


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

void f(int x)
{ std::cout << ++x;}
void g(int& x)
{ std::cout << ++x;}

ANSWER: The different between f() and g() is:


f(): Do not change the value of the input variable.
g(): Increase the value of the input variable by 1 because it changes the value of in the memory area.

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.

ANSWER:

#include <iostream>

#include <cstdlib>

class Allkinds{

private:

Int num1;

long num2;

float num3;

public:

Allkinds(int anum1 =1,long anum2 =1,float anum3 =1){

num1=anum1;

num2=anum2;

num3=anum3;

void get_num1(int anum1){

num1=anum1;

Instructor: Dr. Truong Quang Vinh


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

void get_num2(long anum2){

num2=anum2;

void get_num3(float anum3){

num3=anum3;

void get_infor(){

std::cout<<"Num1 is "<<num1<<std::endl;

std::cout<<"Num2 is "<<num2<<std::endl;

std::cout<<"Num3 is "<<num3<<std::endl;

operator float() const{

return int(num1)+long(num2)+float(num3);

};

int main(){

float sum_all_types;

Allkinds First;

First.get_num1(10);

First.get_num2(1000);

First.get_num3(7.7);

sum_all_types=First;

std::cout<<"Sum of 3 types is :"<< sum_all_types <<std::endl;

First.get_infor();

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.

ANSWER:

bool isMultiple(long n, long m) {


return !(n % m);
}

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.

ANSWER:

#include <math.h>

bool isTwoPower(int i) {
return !((int)log2(i) % 1);
}
8. Write a short C++ function that takes an integer n and returns the sum of all the integers smaller
than n.

ANSWER:

int cau8(int n) {
int s = 0;

for (int i = 1; i < n; i++) s += i;

return s;
}
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.

Instructor: Dr. Truong Quang Vinh


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

d) Write the overloading operator = to assign copy the content of the class student A to the class
student B.
ANSWER:
#include <iostream>
#include <cstdlib>
class Student{
private:
std::string ID;
std::string name;
assigned int birthyear;

public:
Student(const std::string& a_ID="0000",const std::string& a_name="NO NAME", int
a_birthyear=0000){
ID=a_ID;
name=a_name;
birthyear=a_birthyear;
}
friend bool operator== (const Student& c1, const Student& c2);
friend bool operator!= (const Student& c1, const Student& c2);

const Student & operator=(const Student &right){ ////////* Copy Function *///////////////
if( this != &right){
ID = right.ID;
name = right.name;
birthyear = right.birthyear;
return *this;
}
}
void get_infor(){ ////////////////* Get information */////////////////
std::cout<<" The information will be:"<<std::endl;
std::cout<<" "<<ID<<std::endl;
std::cout<<" "<<name<<std::endl;
std::cout<<" "<<birthyear<<std::endl;
}
};

bool operator== (const Student& c1, const Student& c2)


{
return (c1.ID == c2.ID &&
c1.name == c2.name && c1.birthyear == c2.birthyear);
}
bool operator!= (const Student& c1, const Student& c2)

Instructor: Dr. Truong Quang Vinh


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

{
return (c1.ID != c2.ID ||
c1.name != c2.name || c1.birthyear != c2.birthyear);
}

int main(){
Student studentA("1951018","THANH",2001); Student studentB("1951013","NAM",2001);
if(studentA == studentB){
std::cout<<" Two student are the same"<<std::endl;
}
if(studentA != studentB){
std::cout<<" Two students are not the same"<<std::endl;
}
studentB = studentA; ///////*copy the content of the class student A to the class student B.*////
studentB.get_infor();
}

Instructor: Dr. Truong Quang Vinh

You might also like