Professional Documents
Culture Documents
#include <iostream>
#include <string>
using namespace std;
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
2) (14 points) Suppose the class Diagne has been properly implemented in file diagne.h.
You use Diagne class in the following program.
#include <iostream>
#include "diagne.h"
using namespace std;
int main ()
{
Diagne * dptr = new Diagne(5);
Diagne d1(7);
Diagne d2(8);
camasirMakinesi (d1, dptr);
d2 = denbirhaltolmaz (d1);
return 0;
}
Mark the appropriate entries in the table below with "YES" to reflect the facts about
"construction" and "destruction" of the objects mentioned at the beginning of each row. For
"Construction" mark only one cell. Similarly, for "Destruction" mark only one cell. Each
correct mark is 1 point; each wrong mark is -1 point. No mark is no points for that item. No
negative grade from this question.
Construction Destruction
Created Created Destructed
Not Not
using using copy using
created destructed
constructor constructor Destructor
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
3)
a) (4 points) Write -19 in binary and in 2's complement format using 8 bits.
11101101
b) (2 points) What is the smallest number that you can represent in 1's complement format
and using 5 bits? Give this value in decimal, not in binary.
-(24-1) = -15
c) (5 points) Suppose you use a computer system in which signed numbers are represented in
4-bits and using 2's complement format. What would be the value in the box? Give your
answer in binary and show your work.
1010
+ 0010
------------
1100
What is the value of x+y in binary? Did you encounter carry overflow while calculating
x+y ? Show your work in detail and explain your reasoning.
11111111
+ 11111111
Since there is carry out of
-----------------
the most significant bit,
111111110
there is carry overflow.
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
4)
a) (6 points) Suppose you can/cannot do the followings in your program:
In class Piglet implementation, you can access the private data members of class
Eeyore and also of class Tigger.
In class Eeyore implementation, you cannot access the private data members of class
Piglet, but you can access the private data members of class Tigger.
In class Tigger implementation, you cannot access the private data members of class
Piglet and of class Eeyore.
What are the friend relationships among these three classes? Give your answer by filling the
appropriate cells in the following table with true or false. Each correct cell is 1 point; each wrong
cell is -1 point; empty cell is 0 point (minimum grade for this question is 0).
b) (2 points) Consider the following instantiations of templated class Brand. How many different
version(s) of class Brand is/are compiled in the object file?
Brand<int> michaelKors(22);
Brand<string> gucci("dollar");
Brand<int> louisVuitton(19);
2 versions are compiled (one for int and the other is for string)
c) (8 points) The following function takes an integer vector as parameter and copies its content
into a newly generated dynamic integer array. The dynamic array is returned as the function's
return value and its size is returned as a reference parameter. Rewrite the templated version of this
function so that it works with any vector/array element type.
int * convertVectorToDynamic (const vector<int> & v, int & size)
{
int i; In part (c) of this question, you may prefer not to attempt to solve
size = v.size(); it by signing the “not attempted” box below and secure 2 points.
int * darr = new int[size]; If you sign the “not attempted” box below, you accept that you
for (i=0; i<size; i++) did not answer this question and you will receive 2 points. In this
case, your answer will not be graded even if you write something
darr[i]=v[i]; as solution.
return darr; Not attempted
}
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
5) In this question you will write the implementation of = operator and * operator of a
Matrix class that stores real numbers. This class, called Matrix, has the following private
data members:
private:
double ** myMatrix;
int numRows, numCols;
Here, myMatrix is dynamic matrix of real numbers with numRows rows and numCols
columns. The constructor of the Matrix class creates such a dynamic matrix if the number
of rows and columns are given as positive values; otherwise, an empty matrix is created
(where myMatrix is set to NULL). You can assume existence of such a constructor; you will
not implement it. There are also some other member functions to fill the content of the matrix
(not to be implemented in this question). You can assume the existence of properly
implemented deep copy constructor and destructor for the Matrix class in this question. You
do not need to implement those functions.
In part (a) this question, you may prefer not to attempt to solve it by signing the “not
attempted” box below and secure 2 points. If you sign the “not attempted” box below, you
accept that you did not answer this question and you will receive 2 points. In this case, your
answer will not be graded even if you write something as solution.
Not attempted
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
5) Continued
b) (9 points) Write the implementation of deep assignment operator, operator = for
Matrix class as a member function. This operator must allow cascaded assignments, and
your implementation must provide protection against self-assignment and empty matrix case.
These are actually the classical approaches in all assignment operator implementations that
we have seen in class. Thus, there is no particular trick in this question.
In part (b) this question, you may prefer not to attempt to solve it by signing the “not
attempted” box below and secure 2 points. If you sign the “not attempted” box below, you
accept that you did not answer this question and you will receive 2 points. In this case, your
answer will not be graded even if you write something as solution.
Not attempted
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
6) (15 points) The function below is a partial solution for the following problem:
Write a function that takes two integer LinkedList parameters, say list1
and list2. This function returns true if list1 and list2 have the same
amount of elements and the corresponding elements in both of the lists are all
the same. Otherwise, the function returns false. If both lists are empty, the
function returns true, but if only one of them is empty, it returns false.
Implementation must utilize LinkedListIterator. The member functions of the
LinkedList class will not be used.
However, the function is incomplete. Complete this function by filling in the boxes with
appropriate expressions and function calls.
You are not allowed to delete or update anything in the code. Moreover, you cannot add
anything other than the code that you are going to write in the boxes.
For your convenience the member functions of the LinkedListIterator class are given as
follows: void Init() void Next() bool HasMore() int Current()
bool isSame (const LinkedList & list1, const LinkedList & list2)
{
LinkedListIterator iter1 (list1);
LinkedListIterator iter2 (list2);
iter1. Init() ;
if ( iter1.Current() != iter2.Current() )
{
return false;
}
iter1. Next() ;
iter2. Next() ;
}
return true;
else
return false;
}
In this question, you may prefer not to attempt to solve it by signing the “not attempted” box below
and secure 3 points. If you sign the “not attempted” box below, you accept that you did not answer this
question and you will receive 3 points. In this case, your answer will not be graded even if you write
something as solution.
Not attempted
This study source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
NAME:
7) (12 points) The following main function gives the output shown below.
int main()
{ 5 9 in Coder
int a=5, b=9;
Coder CS204(a,b);
5 9 in Debugger
Debugger HW6(a,b); 5 9 in main
CS204.Print();
HW6.Print(); 6 10 in Coder
cout << a << " " << b << " in main" << endl << endl;
5 10 in Debugger
CS204.Update(); 5 10 in main
CS204.Print();
HW6.Print();
cout << a << " " << b << " in main" << endl << endl; 6 11 in Coder
HW6.Update(); 6 11 in Debugger
CS204.Print(); 6 11 in main
HW6.Print();
cout << a << " " << b << " in main" << endl << endl;
return 0;
}
Class declarations, member function and constructor implementations of the Coder and Debugger
classes are partially given below. Complete the class declarations and constructor implementations by
filing in the boxes below such that the main function gives the corresponding output.
You are not allowed to delete or update anything in the code. Moreover, you cannot add anything other
than the code that you are going to write in the boxes.
class Coder const int & x is OK
{
public:
Coder ( int x, int & y ):
myC(x), myS(y) {}
void Print () {
cout << myC << " " << myS << " in Coder" << endl;
}
void Update () {
myC++;
myS++;
}
private:
int myC;
int & myS;
};
class Debugger
{
public:
Debugger ( int & x, int & y ):
myC(x), myS(y) {}
void Print () {
cout << myC << " " << myS << " in Debugger" << endl;
}
void Update () {
myC++;
myS++;
In this question, you may prefer not to attempt to solve it by
}
signing the “not attempted” box below and secure 3 points. If you
private: sign the “not attempted” box below, you accept that you did not
answer this question and you will receive 3 points. In this case,
int & myC; your answer will not be graded even if you write something as
int & myS; solution.
Not attempted
};source was downloaded by 100000882496880 from CourseHero.com on 03-28-2024 06:15:02 GMT -05:00
This study
https://www.coursehero.com/file/65385652/CS204-Spring2019-midterm2-Solutionspdf/
Powered by TCPDF (www.tcpdf.org)