CENG Programming Language Concepts Programming Assignment 3 Solution

$35.00 $29.00

• Objectives This homework aims to help you get familiar with the fundamental C++ programming concepts. Keywords: Constructor/Copy Constructor/Destructor, Assignment/Move, Operator Overloading, Mem-ory Management • Problem De nition TL;DR: Implement the methods in the given 3 classes (Koin, Blockchain, Miner). Blockchains have Koins, Miners have Blockchains. The details are on their way. …. Searching for…

Rate this product

You’ll get a: zip file solution

 

Description

Rate this product

• Objectives

This homework aims to help you get familiar with the fundamental C++ programming concepts.

Keywords: Constructor/Copy Constructor/Destructor, Assignment/Move, Operator Overloading, Mem-ory Management

• Problem De nition

TL;DR: Implement the methods in the given 3 classes (Koin, Blockchain, Miner). Blockchains have Koins, Miners have Blockchains. The details are on their way.

….

Searching for a hyped keyword to construct the homework upon…

Found 1 result(s): Blockchain.

Initiating environment…

Kicking in Google-fu…

Browsing StackOver ow…

Watching cute cat videos… Oh, is that a “don’t laugh challenge”? I’m interested!

Populating classes… Wait a second, is this a linked list? Ugh.

Brawling with C++ Memory Manager… (wish I didn’t say “I would like to speak with your manager”)

Preparing test cases…

Ready.

As an aspiring Computer Engineering student, your next task -if you accept- is to create a simpli ed version of a Blockchain. So, you are going to overload some operators, keep track of the memory, ght with faulty segments (it’s not you, it’s the segment.), calculate your Blockchain’s and your Miner’s values, and some housekeeping.

A standard C++ homework, with the addition of being rich locally. Because why would anyone pay at money for some bytes in another person’s computer? I wouldn’t.

(I actually did, but that’s not in the scope of this homework.)

1

• Class De nitions

3.1 Koin

Koin is the most basic class in this homework. Basically it’s the node in a linked-list, but with the addition of Blockchain, they would create the Voltron. No? Okay, linked list it is.

c l a s s Koin f

p r i v a t e :

d o u b l e value ;
Koin ∗next ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PRIVATE METHODS/PROPERTIES BELOW

p u b l i c :
• ∗ ∗
∗ C o n s t r u c t o r .

∗ @param v a l u e The Koin ‘ s v a l u e .
∗/
Koin( d o u b l e value) ;
• ∗ ∗
∗ Copy C o n s t r u c t o r .

∗ @param r h s The k o i n t o be c o p i e d .
∗/
Koin( c o n s t Koin& rhs) ;
• ∗ ∗
∗ Assignment .

∗ @param r h s The k o i n t o a s s i g n i n t o t h i s k o i n .
∗ @return The a s s i g n e d k o i n .
∗/
Koin& o p e r a t o r =( c o n s t Koin& rhs) ;

~Koin ( ) ;

d o u b l e getValue ( ) c o n s t ;
Koin∗ getNext ( ) c o n s t ;
• ∗ ∗
∗ S e t s t h e next c h a i n f o r t h i s Koin .

∗ Important : Koin d o e s NOT “own” next .

∗ @param next The next Koin .
∗/
v o i d setNext(Koin ∗next) ;
• ∗ ∗
∗ E q u a l i t y o v e r l o a d .

 

 

 

 

 

 

 

 

 


The Koins
a r e e q u a l
i f
t h e i r
v a l u e s
a r e
equal , and
t h e i r next
Koins , and


t h e next
Koins ‘
next
k o i n s . . .
a r e
a l s o
e q u a l .

 

 

 

 

 

 

 


Important :
S i n c e
t h e
v a l u e s
a r e
double ,
you s h o u l d
r e f r a i n t o
u s e e q u a l i t y

between two d o u b l e s .

∗ I n s t e a d , they s h o u l d be compared w i t h i n a s e n s i t i v i t y , g i v e n i n U t i l i t i e s . – h .

2

∗ @param r h s The Koin t o compare .
∗ @return True i f they ‘ r e equal , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r==(c o n s t Koin& rhs) c o n s t ;
• ∗ ∗
I n e q u a l i t y o v e r l o a d .


∗ E x p l a i n e d i n upper p a r t .

∗ @param r h s The Koin t o compare .
∗ @return True i f they ‘ r e not equal , f a l s e o t h e r w i s e .
∗/
b o o l o p e r a t o r !=( c o n s t Koin& rhs) c o n s t ;
• ∗ ∗
∗ M u l t i p l y t h e v a l u e o f t h e Koin with g i v e n m u l t i p l i e r .

∗ @param m u l t i p l i e r The v a l u e t o m u l t i p l y t h e Koin ‘ s v a l u e .
∗ @return The c u r r e n t Koin .
∗/
Koin& o p e r a t o r ∗=( i n t multiplier) ;
• ∗ ∗
∗ D i v i d e t h e v a l u e o f t h e Koin with g i v e n d i v i s o r .

∗ @param d i v i s o r The v a l u e t o d i v i d e t h e Koin ‘ s v a l u e .
∗ @return The c u r r e n t Koin .
∗/
Koin& o p e r a t o r /=( i n t divisor) ;
• ∗ ∗
∗ Stream o v e r l o a d .

∗ What t o stream :

∗ koinValue nextKoin ‘ s value nextKoins ‘ nextKoin ‘ s value … j ( p i p e a t – t h e end )


∗ Example :

0.439
0.439
0.439j

 

 


Important : The
p r e c i s i o n r e q u i r e d i s g i v e n t o you i n U t i l i z e r . h .

For
t h i s example ,
i t ‘ s
3 .

 

 


@param o s Stream t o be used .

@param k o i n Koin
t o be
streamed .
∗ @return The c u r r e n t Stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Koin& koin) ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PUBLIC METHODS/PROPERTIES BELOW

• ;

 

 

 

 

 

3

3.2 Blockchain

Blockchain consists of an id (which is unique in a Miner) and a head Koin. You are going to be traversing the Koin chain, mostly. Also, in order to deal with the memory, it’s important to decide that which Koins are under the ownership of a Blockchain, which are not.

c l a s s Blockchain f

p r i v a t e :
Koin ∗head ;
c o n s t i n t id ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PRIVATE METHODS/PROPERTIES BELOW

p u b l i c :
• ∗ ∗
∗ C o n s t r u c t o r .

∗ @param i d The ID o f t h e B l o c k c h a i n .
∗/
Blockchain( i n t id) ;
• ∗ ∗
∗ Secondary C o n s t r u c t o r .

 

 


Important :
B l o c k c h a i n “owns” t h i s
Koin .

 

 


@param
i d The ID o f t h e
B l o c k c h a i n .

@param
head
The s t a r t i n g
p o i n t o f
t h e B l o c k c h a i n .
∗/
Blockchain( i n t id , Koin ∗head) ;
• ∗ ∗
∗ Copy C o n s t r u c t o r .

∗ The head s h o u l d be deep c o p i e d .

∗ @param r h s The b l o c k c h a i n t o be c o p i e d .
∗/
Blockchain( c o n s t Blockchain& rhs) ;
• ∗ ∗
∗ Move Operator .

 

 


RHS w i l l
r e l i n q u i s h t h e r i g h t s
o f t h e
headKoin
t o LHS .

LHS ‘ s ID
w i l l
not ( cannot ) be
changed .

 

 


C a r e f u l
about
memory l e a k s !

 

 

 


@param r h s The
b l o c k c h a i n t o be moved
i n t o t h i s
b l o c k c h a i n .
∗ @return The m o d i f i e d b l o c k c h a i n .
∗/
Blockchain& o p e r a t o r =(Blockchain&& rhs) noexcept ;
• ∗ ∗
∗ No a s s i g n m e n t with c o n s t r h s .

∗ @param r h s noNeed ∗ @return noNeed
∗/
Blockchain& o p e r a t o r =( c o n s t Blockchain& rhs) = d e l e t e ;

4

~Blockchain ( ) ;

i n t getID ( ) c o n s t ;
Koin∗ getHead ( ) c o n s t ;
• ∗ ∗
C a l c u l a t e t h e v a l u e o f t h e b l o c k c h a i n .


∗ @return T o t a l v a l u e o f t h e Koins i n t h e b l o c k c h a i n .
∗/
d o u b l e getTotalValue ( ) c o n s t ;
• ∗ ∗
C a l c u l a t e t h e l e n g t h o f t h e k o i n s end to end .


∗ @return The l e n g t h o f t h e b l o c k c h a i n .
∗/
l o n g getChainLength ( ) c o n s t ;
• ∗ ∗
∗ P r e f i x a d d i t i o n .

∗ Mine and i n s e r t t h e mined Koin a t t h e end o f t h e c h a i n .
∗/
v o i d o p e r a t o r ++() ;
• ∗ ∗
∗ P r e f i x decrement .

∗ Remove/ d e s t r o y t h e l a s t i n s e r t e d Koin t o t h e c h a i n . ∗ no op i f t h e head i s n u l l p t r . ∗

∗ Important n o t e :

I f a b l o c k c h a i n i s c r e a t e d by a s o f t f o r k , i t s head cannot be d e l e t e d .

∗/
v o i d o p e r a t o r () ;
• ∗ ∗
∗ M u l t i p l i c a t i o n o v e r l o a d .

 

 


M u l t i p l y t h e v a l u e
o f e v e r y
Koin
i n t h e b l o c k c h a i n .

 

 


@param
m u l t i p l i e r
The v a l u e
t o be
m u l t i p l i e d with t h e v a l u e s o f t h e Koins .

@return
The c u r r e n t b l o c k c h a i n .

∗/
Blockchain& o p e r a t o r ∗=( i n t multiplier) ;
• ∗ ∗
∗ D i v i s i o n .


D i v i d e
t h e
v a l u e o f
e v e r y
Koin i n
t h e b l o c k c h a i n .

 

 


@param
d i v i s o r The
v a l u e
t o d i v i d e
t h e v a l u e s o f t h e Koins .

@return
The
c u r r e n t
b l o c k c h a i n .

∗/
Blockchain& o p e r a t o r /=( i n t divisor) ;
• ∗ ∗
∗ Stream o v e r l o a d .

∗ What t o stream :

∗ Block b l o c k c h a i n I D : headKoinStream ( s e e Koin f o r stream example ) ( –
t o t a l V a l u e )

5


∗ Example :


Block
6 :
0.707
0.390
0.984 j
(2.080)

 

 


Edge
c a s e
: B l o c k c h a i n
w i t h o u t
head

 

 


Block
b l o c k c h a i n I D : Empty .

 

 


@param o s Stream t o be used .


@param b l o c k c h a i n B l o c k c h a i n t o
be streamed .
∗ @return The c u r r e n t stream .
∗/

f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Blockchain& – blockchain) ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PUBLIC METHODS/PROPERTIES BELOW

• ;

3.3 Miner

Miner is the owner of the blockchains in its arsenal. Also, it’s able to fork the blockchains as requested.

The details are in the code itself below.

c l a s s Miner f

p r i v a t e :
• ∗ ∗
∗ Find t h e next ID f o r a newly c r e a t e d / f o r k e d b l o c k c h a i n .
∗ B l o c k c h a i n IDs a r e unique b e l o n g i n g t o Miner , and s t r i c t l y i n c r e a s i n g ( –
s t a r t e d from 0 ) .

∗ @return The next a v a i l a b l e ID f o r a new b l o c k c h a i n .
∗/
i n t getNextAvailableBlockchainID ( ) c o n s t ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PRIVATE METHODS/PROPERTIES BELOW

p u b l i c :
• ∗ ∗
∗ C o n s t r u c t o r .

∗ @param name Name o f t h e miner .
∗/
Miner(std : : string name) ;

~Miner ( ) ;
• ∗ ∗
∗ C r e a t e a new b l o c k c h a i n with t h e next a v a i l a b l e ID .
∗/

v o i d createNewBlockchain ( ) ;
• ∗ ∗

S t a r t mining with
t h e g i v e n count .


For e v e r y
count ,
a
s i n g l e Koin w i l l
be
produced i n each b l o c k c h a i n .

 

 


O p e r a t i o n s
must
be
o r d e r e d a c c o r d i n g
t o
t h e b l o c k c h a i n I D s .

 

 

 

6

∗ @param c y c l e C o u n t The count which t h e b l o c k c h a i n s w i l l be run f o r .
∗/
v o i d mineUntil( i n t cycleCount) ;
• ∗ ∗

S t a r t de mining with
t h e
g i v e n count .

 

 


For e v e r y count , t h e
l a s t
Koin i n t h e
b l o c k c h a i n
must
be
d e s t r o y e d .

no op f o r
B l o c k c h a i n
i f
i t
doesn ‘ t have
any
( l e f t ) .

 

 

 

 

 


O p e r a t i o n s
must be
o r d e r e d
a c c o r d i n g
t o
t h e
b l o c k c h a i n I D s .

 

 

 

 

 


@param c y c l e C o u n t
The
count which t h e
b l o c k c h a i n s
w i l l
be
run f o r .
∗/
v o i d demineUntil( i n t cycleCount) ;
• ∗ ∗

C a l c u l a t e
t h e
v a l u e
o f
t h e
miner ,
which i s
t h e sum
o f t h e b l o c k c h a i n s ‘ –

v a l u e .

 

 

 


S o f t f o r k s
DO NOT c o n s t i t u t e f o r
t h e t o t a l
v a l u e o f
t h e miner .

 

 

 

 


@return T o t a l
v a l u e
o f
t h e
miner .

 

∗/
d o u b l e getTotalValue ( ) c o n s t ;
• ∗ ∗
∗ Return t h e count o f t h e b l o c k c h a i n s .

∗ @return T o t a l count o f t h e b l o c k c h a i n s .
∗/
l o n g getBlockchainCount ( ) c o n s t ;
• ∗ ∗
∗ I n d e x i n g .

∗ Find t h e b l o c k c h a i n with t h e g i v e n i d .

∗ @return The b l o c k c h a i n with t h e g i v e n i d . n u l l p t r i f not e x i s t s .
∗/
Blockchain∗ o p e r a t o r [ ] ( i n t id) c o n s t ;
• ∗ ∗
Shallow copy t h e b l o c k c h a i n with g i v e n ID .


∗ How To :

1 )
Fetch
t h e
b l o c k c h a i n
i n
t h e
miner with
g i v e n
ID .

 


2 )
I f
not
e x i s t s ,
no op .

 

 

 

 

 


3 )
Fetch
t h e
next
a v a i l a b l e
ID
f o r
t h e
b l o c k c h a i n .

 


4 )
C r e a t e a
new
b l o c k c h a i n
with
t h e new ID ,
and
with
t h e
head
a s t h e –

o r i g i n a l
o f
t h e
l a s t Koin o f t h e b l o c k c h a i n .

 

 


5 )
Hence ,
a
m o d i f i c a t i o n
t o
t h e
newly
c r e a t e d b l o c k c h a i n
w i l l
a l s o a f f e c t –

t h e
o l d
b l o c k c h a i n , but
o n l y . . .

 

 

 


6 )
. . . a f t e r
t h e head ( head
i n c l u d e d ) .
And v i c e
v e r s a .


7 )
Save
t h e
newly
c r e a t e d b l o c k c h a i n i n t h e
miner .

 

 

 

 

 

 

 

 

 

 


@param
b l o c k c h a i n I D The
b l o c k c h a i n
ID
t o
be
f o r k e d .

 

 

 

 

 

 

 

 

 

 


@return
t r u e
i f
t h e b l o c k c h a i n
with
g i v e n
ID
e x i s t s ,
o t h e r w i s e
f a l s e .
∗/
b o o l softFork( i n t blockchainID) ;
• ∗ ∗
Deep copy t h e b l o c k c h a i n with g i v e n ID .

7

∗ How To :

1 )
Fetch
t h e
b l o c k c h a i n
i n t h e miner
with
g i v e n ID .

 


2 )
I f not
e x i s t s ,
no
op .

 

 

 


3 )
El se ,
f e t c h
t h e
next
a v a i l a b l e ID
f o r
t h e b l o c k c h a i n .

 


4 )
C r e a t e a
new b l o c k c h a i n with t h e new ID ,
and with
t h e
head
a s
a copy –

o f
t h e
l a s t
Koin o f
t h e b l o c k c h a i n .

 

 

 


5 )
Any c ha n ge s made
i n t h e new b l o c k c h a i n
w i l l NOT a f f e c t
t h e
o l d

b l o c k c h a i n .
And v i c e
v e r s a .

 

 

 


6 )
Save
t h e
newly
c r e a t e d b l o c k c h a i n
i n t h e
miner .

 

 

 

 

 

 

 

 


@param
b l o c k c h a i n I D The
b l o c k c h a i n ID t o
be
f o r k e d .

 

 

 

 

 

 

 

 


@return
t r u e
i f
t h e
b l o c k c h a i n with
g i v e n
ID
e x i s t s ,
o t h e r w i s e
f a l s e .
∗/
b o o l hardFork( i n t blockchainID) ;
• ∗ ∗
∗ Stream o v e r l o a d .

∗ What t o stream :

∗BEGIN MINER
∗ Miner name :minerName
∗ B l o c k c h a i n count :g e t B l o c k c h a i n C o u n t ( )
∗ T o t a l v a l u e : g e t T o t a l V a l u e ( )

∗ Block b l o c k c h a i n I D : headKoin ( s e e B l o c k c h a i n f o r stream example )

∗ . ∗ . ∗ .
∗ Block l a s t B l o c k c h a i n I D : headKoin ( s e e B l o c k c h a i n f o r stream example ) ∗

∗END MINER

∗ Example :

∗BEGIN MINER
∗ Miner name : BTCMiner
∗ B l o c k c h a i n count : 5
∗ T o t a l v a l u e : 2 . 5 1 9

0.529 j (1.123)

Block 0 : 0.491
0.103

∗ Block 1 : Empty .
∗ Block 2 : Empty .
∗ Block 3 : Empty .

Block 4 : 0.400
0.924
0.072 j (1.396)

 

∗END MINER

∗ @param o s Stream t o be used .
∗ @param miner Miner t o be streamed . ∗ @return The c u r r e n t stream .
∗/
f r i e n d std : : ostream& o p e r a t o r <<(std : : ostream& os , c o n s t Miner& miner) ;

• DO NOT MODIFY THE UPPER PART

• ADD OWN PUBLIC METHODS/PROPERTIES BELOW

• ;

8

• Extras

While producing Koins, you need to produce a random double value for its worth. To do this, you MUST use Utilizer class. It’ll seed a Random Number Generator and give you the values accordingly. This RNG is OS-Agnostic, but the grading will only be done in Ubuntu (ineks to be exact).

The implementation of the Mersenne and the Utilizer is already provided to you. Hence, you just need to do this:

Koin∗ newKoin = new Koin(Utilizer : : fetchRandomValue ( ) ) ;

While comparing/printing the Koins, you MUST use the Utilizer class again.

To compare values, use Utilizer::doubleSensitivity.

To print, use Utilizer::printPrecision.

The summary of the memory ownership:

• A Koin WILL NOT own its nextKoin.

• A Blockchain WILL own the headKoin when constructed with.

• A Blockchain WILL own the COPY of the headKoin when copy constructed.

• A Blockchain WILL own the headKoin when moved from RHS. RHS WILL relinquish (TR: feragat etmek/vazgecmek) its ownership from the headKoin.

• A Miner WILL own the blockchain when createNewBlockchain, softFork:, hardFork: is called and produced a blockchain.

• Owning a koin also means owning the nextKoin, and the nextKoin of the nextKoin, …

• Grading

For your convenience, there are multiple partial tests already given to you in StudentPack. The rest of the points can be earned with proper memory management and proper outputs. The example I/O will be shared on COW, and a simple one is in StudentPack.

 

 

 

 

 

 

 

 

 

 

 

9

• Regulations

◦ Programming Language: You must code your program in C++ (11). Your submission will be compiled with g++ with -std=c++11 ag on department lab machines.

◦ Allowed Libraries: You may include and use C++ Standard Library. Use of any other library (especially the external libraries found on the internet) is forbidden.

◦ Memory Management: When an instance of a class is destructed, the instance must free all of its owned/used heap memory. Class-wise memory management is explained to you in source-code. Any heap block, which is not freed at the end of the program will result in grade deduction. Please check your codes using valgrind {leak-check=full for memory-leaks.

◦ Late Submission: You have a total of 10 days for late submission. You can spend this credit for any of the assignments or distribute it for all. For each assignment, you can use at most 3 days-late.

◦ Cheating: In case of cheating, the university regulations will be applied.

◦ Newsgroup: It’s your responsibility to follow the newsgroup (news.ceng.metu.edu.tr) for discus-sions and possible updates on a daily basis.

• Submission

Submission will be done via CengClass. Create a zip le named hw3.zip that contains:

• Koin.h

• Koin.cpp

• Blockchain.h

• Blockchain.cpp

• Miner.h

• Miner.cpp

Do not submit a le that contains a main function. Such a le will be provided and your code will be compiled with it. Also, do not submit a Make le.

Note: The submitted zip le should not contain any directories! The following command sequence is expected to run your program on a Linux system:

• unzip hw3.zip

• make clean

• make all

• make run

• -optional- make valgrind

Good luck, have fun.

 

– engin

 

10

CENG Programming Language Concepts Programming Assignment 3 Solution
$35.00 $29.00