ECE391 Homework 1
ECE391 Homework 1
Engineering
Homework 1
1. Determine the content of variables after the following C++ code
ANSWER:
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;
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>
#include <stdio.h>
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];
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.
ANSWER:
#include <iostream>
#include <cstdlib>
class Allkinds{
private:
Int num1;
long num2;
float num3;
public:
num1=anum1;
num2=anum2;
num3=anum3;
num1=anum1;
num2=anum2;
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;
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;
First.get_infor();
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:
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;
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.
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;
}
};
{
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();
}