SlideShare a Scribd company logo
Fill in the missing code for C++.
Code from part B is below to make copy-pasting easier:
*-------------------------------------------------------------------------*/
/* * $ g++ -lpthread -g buyTheCandy.cpp -o buyTheCandy */
//-- Standard inclusions: --//
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>
//-- Constants: --//
// PURPOSE: To the names of the children
const char* CHILD_NAME_ARRAY[] = {"Alice", "Bob", "Cathy", "David" };
// PURPOSE: To tell the number of children.
const int NUM_CHILDREN = sizeof(CHILD_NAME_ARRAY)/sizeof(const char*);
// PURPOSE: To tell the possible denominations of the Coin instances.
const int COIN_DENOMINATION_ARRAY[] = {1,5,10,25};
// PURPOSE: To tell the number of coins in each new Purse.
const int NUM_COINS_PER_NEW_PURSE = 4;
// PURPOSE: To tell the number of denominations.
const int NUM_DENOMINATIONS = sizeof(COIN_DENOMINATION_ARRAY)/sizeof(int);
// PURPOSE: To tell how much money needs to be collected to buy the candy.
const int CANDY_COST = 100;
//-- Classes: --//
// PURPOSE: To represent coins.
class Coin {
// I. Member vars:
// PURPOSE: To tell the denomination of '*this' Coin instance.
int denomination_;
// PURPOSE: To hold the address of the next Coin after '*this' one,
// or 'NULL' if there is no such Coin instance.
Coin* nextPtr_;
// II. Disallowed auto-generated methods:
// No default constructor:
Coin ();
// No copy constructor:
Coin (const Coin&);
// No copy assignment op:
Coin operator= (const Coin&);
protected :
// III. Protected methods:
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To make a coin of denomination 'newDom'. No return value.
Coin (int newDom ) : denomination_(newDom), nextPtr_(NULL) { }
// PURPOSE: To release the resources of '*this'. No parameters.
// No return value.
~Coin () { }
// V. Accessors:
// PURPOSE: To return the denomination of '*this' Coin instance.
// No parameters.
int getDenomination () const { return(denomination_); }
// PURPOSE: To return the address of the next Coin after '*this' one,
// or 'NULL' if there is no such Coin instance. No parameters.
Coin* getNextPtr () const { return(nextPtr_); }
// VI. Mutators:
// PURPOSE: To note that the Coin instance with address 'coinPtr' comes
// after '*this' one. No return value.
void setNextPtr (Coin* coinPtr ) { nextPtr_ = coinPtr; }
// VII. Methods that do main and misc work of class:
// PURPOSE: Try creating and return the address of a random coin.
// No parameters.
static Coin* makeRandom () {
// I. Application validity check:
// II. Try creating and return a coin:
return(new Coin(COIN_DENOMINATION_ARRAY[rand() % NUM_DENOMINATIONS]));
} };
// PURPOSE: To implement a list of Coin instances.
class Purse {
// 0. Constants:
enum { NO_OWNER_INDEX = -1 };
// I. Member var:
// PURPOSE: To hold the index of the name of the owner of '*this' Purse
// instance, or 'NO_OWNER_INDEX' if '*this' has no owner. int index_;
// ADD YOUR VARIABLES HERE
// II. Disallowed auto-generated methods:
// No copy constructor:
Purse (const Purse&);
// No copy-assignment op:
Purse operator= (const Purse&);
protected :
// III. Protected methods: public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To initialize '*this' to an empty purse
Purse () :
index_(NO_OWNER_INDEX) {
// INITIALIZE YOUR VARS HERE
}
// PURPOSE: To initialize '*this' Purse instance to have
// NUM_COINS_PER_NEW_PURSE random Coin instances owned by the child with
// index 'newIndex'. No return value.
Purse (int newIndex ) : index_(newIndex) {
// INITIALIZE YOUR VARS HERE
for (int i = 0; i < NUM_COINS_PER_NEW_PURSE; i++) {
addToBack(Coin::makeRandom());
}
}
// PURPOSE: To release the resources of '*this'. No parameters.
// No return value.
~Purse () {
Coin* run;
Coin* nextPtr;
// GET RID OF YOUR VARS HERE
}
// V. Accessors:
// PURPOSE: To return the index of the name of the owner of '*this' Purse
// instance, or 'NO_OWNER_INDEX' if '*this' has no owner.
int getIndex () const { return(index_); }
// PURPOSE: To return the name of the owner of '*this' Purse instance.
// No parameters.
const char* getOwnerNameCPtr() const { return( (getIndex() == NO_OWNER_INDEX) ?
"common" : CHILD_NAME_ARRAY[getIndex()] ); }
// PURPOSE: To return the value of the money in '*this' wallet.
// No parameters.
int getValue () const {
const Coin* run;
int sum = 0;
// YOUR CODE HERE
return(sum);
}
// PURPOSE: To tell the number of Coin instances in '*this'.
int getNumCoins () const { return(numCoins_); }
// VI. Mutators:
// PURPOSE: To add the Coin with address 'coinPtr' to '*this' Purse.
// No return value.
void addToBack (Coin* coinPtr ) {
// YOUR CODE HERE
}
// PURPOSE: To remove the Coin at the beginning of '*this' and return
// its address. No parameters.
Coin* removeFromFront () {
Coin* returnMe = NULL;
// YOUR CODE HERE
return(returnMe);
}
// VII. Methods that do main and misc. work of class:
// PURPOSE: To print the status of '*this' Purse to 'stdout'.
// No parameters. No return value.
void print () const {
printf ("%s has %d coins worth %d cents.n", getOwnerNameCPtr(),getNumCoins(),getValue()
); }
};
// PURPOSE: To implement a thread-safe version of Purse.
class CommonPurse : public Purse {
// I. Member vars:
// ADD YOUR VARIABLES HERE
// PURPOSE: To tell the current turn.
int turn_;
// II. Disallowed auto-generated methods:
// No copy constructor:
CommonPurse (const CommonPurse&);
// No copy assignment op:
CommonPurse operator= (const CommonPurse&);
protected :
// III. Protected methods:
public :
// IV. Constructor(s), assignment op(s), factory(s) and destructor:
// PURPOSE: To initialize '*this' to an empty shared Purse instance.
CommonPurse () : turn_(NUM_CHILDREN-1) {
// INITIALIZE YOUR VARS HERE
}
// PURPOSE: To release the resources of '*this'. No parameters.
// No return value.
~CommonPurse () {
// GET RID OF YOUR VARS HERE
}
// V. Accessors:
// PURPOSE: To tell the current turn.
int getTurn () const { return(turn_); }
// VI. Mutators:
// VII. Methods that do the main and misc. work of class:
// PURPOSE: To consolidate the Coin instances of '*donorPursePtr' into
// '*this' one.
// (Note to the professional C++ coders: Yeah, I know it is better to use
// call-by-reference. I use pointers for consistency.)
// PUT CODE 3 PLACES IN HERE . . . I WONDER WHERE?
void consolidate (Purse* donorPursePtr ) {
// I. Application validity check:
// II. Transfer the wealth:
while (getTurn() != donorPursePtr->getIndex()) {
printf ("%s: "I want candy! Let's put our money together!"n", donorPursePtr-
>getOwnerNameCPtr() );
}
while (donorPursePtr->getNumCoins() > 0) {
addToBack(donorPursePtr->removeFromFront());
}
printf ("%s: "I added my money."n", donorPursePtr->getOwnerNameCPtr() );
turn_--;
// III. Finished: } };
//-- Global vars: --//
// PURPOSE: To represent the shared, thread-safe Purse instance.
CommonPurse sharedPurse;
//-- Main functions: --//
// PURPOSE: To add the Coin instances in '*(Purse*)vPtr' to 'sharedPurse'.
// Returns 'NULL'.
void* consolidate (void* vPtr ) {
Purse* pursePtr = NULL; // CHANGE THAT NULL
// YOUR CODE HERE
return(NULL); }
// PURPOSE: To have the children add their money to 'sharedPurse', and
// then attempt to buy the candy. Ignores parameters. Returns /
/ 'EXIT_SUCCESS' to OS.
int main () {
// I. Application validity check:
// II. Try to buy candy:
// II.A. Initialize data-structures:
Purse* pursePtrArray[NUM_CHILDREN];
srand(getpid());
for (int i = 0; i < NUM_CHILDREN; i++) {
pursePtrArray[i] = new Purse(i);
pursePtrArray[i]->print();
}
// II.B. Consolidate the money:
// YOUR CODE HERE TO START THREADS
// II.C. Close program down:
// YOUR CODE HERE TO WAIT FOR THREADS
for (int i = 0; i < NUM_CHILDREN; i++) {
delete(pursePtrArray[i]);
}
printf("All: "We have %d cents. ",sharedPurse.getValue());
if (sharedPurse.getValue() >= CANDY_COST) {
printf("Yay! We can afford the candy!"n");
} else {
printf("Time to beg mom for more money!"n");
}
// III. Finished:
return(EXIT_SUCCESS);
}
A package of 4 candy candies cost 100 cents. (It can be 1 USD, 1 Euro, 1 whatever). Four
siblings (brothers or sisters) want a candy, and each has some money. Unfortunately, probably
none of the siblings has enough money to buy the candies by themself. So they will all pool their
money and jointly buy the candy. There are 3 classes: - Coin: Represents some money - Purse:
Implements a container (a linked list) of Coin instances. - Commonpurse: a thread-safe version
of purse Each sibling has their money in a purse instance. Additionally, there is a Commonpurse
called sharedpurse to which all siblings will add their money. Because each sibling is
implemented with its own thread, the individual Purse instances do not need to be thread safe.
However, sharedpurse needs to be made thread-safe because all four threads can access it. B.
Cut-and-paste the following: // PURPOSE: To have the children add their money to
'sharedPurse', and // then attempt to buy the candy. Ignores parameters. Returns //
'EXIT_SUCCESS' to OS. int main () { I/ I. Application validity check: // II. Try to buy candy: //
II.A. Initialize data-structures: Purse* pursePtrarray[NUM_CHILDREN]; srand (getpid()); for (
int i = 0 ; i < NUM_CHILDREN; i + + ) { purseptrarray[i] [ i ] new Purse ( i ) ; pursePtrarray
[i]->print (); } // II.B. Consolidate the money: I/ YOUR CODE HERE TO START THREADS
// II.C. Close program down: I/ YOUR CODE HERE TO WAIT FOR THREADS for (int i = 0 ;
i < NUM_CHILDREN; i + + ) { delete (purseptrarray [i]); } printf("All: "We have ofd cents. ",
sharedPurse.getValue()); if (sharedPurse.getValue ( ) >= CANDY_COST) { printf("Yay! We
can afford the candy!  "  "); } else { printf("Time to beg mom for more money! "n"); } // III.
Finished: return(EXIT_SUCCESS); } Purse must implement a linked list of Coin instances
using the coin methods getNext.ptr() and setNextPtr ( ). (No cheating and using C++ containers
like std: 1 ist, std x vector, etc.) 1. Give class Purse 3 member variables: - A coin to point to the
beginning of the list. - A Coin* to point to the end of the list. - An int that keeps track of the
length of the list. 2. Initialize your variables in the constructor. 3. Get rid of your list in the
destructor method: -Purse (). In C one gets memory with malloc ( ) and gives it back with free (
). However, in C++ one gets memory and builds an instance of an object with new, and one
dismantles the instance with delete(). Please have a local variable like coinPtr and say delete
(coinPtr) for cach coin in the list. 4. Make getvalue() loop thru the coin instances. It should sum
all the getDenomination () values of the coin instances, and return() the sum. 5. Make
getNumcoins () returns the how many coin instances are in the list. 6. Make addToBack () add
coinPtr to the back of the list. It should also increment your list-length variable. 7. Make
removefromfront () remove the first coin instance from the linked list. It should also decrement
your list-length variable, and return () the address of the first coin. D. Make it multi-threaded: 1.
In main () you will need an array of NUN_CHILDREN pthread_t instances for the bee hive
threads. a. In section II . declare your array variable. Have a loop that starts all
NUN_CHILDREN threads. Each thread should run consolidate (), and pass the address of the
corresponding purse as an argument. b. In section II . C wait for all child threads to finish. 2. In
consolidate (), argument vPtr comes in pointing to a Purse. Set pursePtr cqual to vPtr (you will
need to cast it). The only thing the function needs to do is run sharedpurse, consolidate
(purseptr). 3. Now run it! E. Make it thread-safe: Congratulations! If you got this far you have
made it multi-threaded, but not thread-safe. To make it thread-safe you will have to add some
mutex(es) and condition(s). You need to protect access to the linked list and turn_in
Commonpurse method consolidate( ) . a. It needs one pthread_mutex_t variable and an array of
NUM_CHILDREN + 1 pthread_cond_t instances. b. Initialize those variables in CommonPurse
() . c. Destroy those variables in CommonPurse ( ). d. Use them in consolidate ( ) in
Commonpurse. Where does the critical section begin? Where does the critical section end? Note:
all threads should wait on the pthread_cond_t with index donorpursePtr->getindex () + 1 . Also,
all threads should signal the pthread_cond_t with index donorPursePtr->getindex ( ). F.
Questions: How well did your program work before making it thread safe? How well did your
program work after making it thread safe? G. Sample output: $ ./buyTheCandy Alice has 4 coins
worth 41 cents. Bob has 4 coins worth 50 cents. Cathy has 4 coins worth 16 cents. David has 4
coins worth 50 cents. Bob: "I want candy! Let's put our money together!" Cathy: "I want candy!
Let's put our money together!" David: "I added my money." Alice: "I want candy! Let's put our
money together!" Cathy: "I added my money." Bob: "I added my money." Alice: "I added my
money." All: "We have 157 cents. Yay! We can afford the candy!" $ . /buyTheCandy Alice has 4
coins worth 12 cents. Bob has 4 coins worth 36 cents. Cathy has 4 coins worth 8 cents. David has
4 coins worth 16 cents. Alice: "I want candy! Let's put our money together!" Cathy: "I want
candy! Let's put our money together!" Bob: "I want candy! Let's put our money together!"
David: "I added my money." Cathy: "I added my money." Bob: "I added my money." Alice: "I
added my money." All: "We have 72 cents. Time to beg mom for more money!" $
./buyTheCandy Alice has 4 coins worth 37 cents. Bob has 4 coins worth 22 cents. Cathy has 4
coins worth 80 cents. David has 4 coins worth 21 cents. Alice: "I want candy! Let's put our
money together!" Bob: "I want candy! Let's put our money together!" David: "I added my
money." Cathy: "I added my money." Bob: "I added my money." Alice: "I added my money."
All: "We have 160 cents. Yay! We can afford the candy!"

More Related Content

Similar to Fill in the missing code for C++- Code from part B is below to make co.pdf

20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
eugeniadean34240
 
Sadi service
Sadi serviceSadi service
Sadi service
Mark Wilkinson
 
Example code for the SADI BMI Calculator Web Service
Example code for the SADI BMI Calculator Web ServiceExample code for the SADI BMI Calculator Web Service
Example code for the SADI BMI Calculator Web Service
Mark Wilkinson
 
public class Point {   Insert your name here    private dou.pdf
public class Point {    Insert your name here    private dou.pdfpublic class Point {    Insert your name here    private dou.pdf
public class Point {   Insert your name here    private dou.pdf
anandshingavi23
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
Anıl Sözeri
 
00-intro-to-classes.pdf
00-intro-to-classes.pdf00-intro-to-classes.pdf
00-intro-to-classes.pdf
TamiratDejene1
 
C++ Core Guidelines
C++ Core GuidelinesC++ Core Guidelines
C++ Core Guidelines
Thomas Pollak
 
import java.util.Scanner;import java.text.DecimalFormat;import j.pdf
import java.util.Scanner;import java.text.DecimalFormat;import j.pdfimport java.util.Scanner;import java.text.DecimalFormat;import j.pdf
import java.util.Scanner;import java.text.DecimalFormat;import j.pdf
KUNALHARCHANDANI1
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
FashionBoutiquedelhi
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdf
fashiongallery1
 
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docxSinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
jennifer822
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
Sandino Núñez
 
operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
 operating system Ubunut,Linux,Mac filename messageService.cpp.pdf operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
annethafashion
 
Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2
Aldo Chiecchia
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
Mohammad Shaker
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
Yashpatel821746
 
Php 5.6
Php 5.6Php 5.6
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
I keep getting an error about m_pHead in printStoresInfo(); function.pdf
I keep getting an error about m_pHead in printStoresInfo(); function.pdfI keep getting an error about m_pHead in printStoresInfo(); function.pdf
I keep getting an error about m_pHead in printStoresInfo(); function.pdf
flashfashioncasualwe
 

Similar to Fill in the missing code for C++- Code from part B is below to make co.pdf (20)

20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
20145-5SumII_CSC374-407_assign3.htmlCSC 374407 Computer Sy.docx
 
Sadi service
Sadi serviceSadi service
Sadi service
 
Example code for the SADI BMI Calculator Web Service
Example code for the SADI BMI Calculator Web ServiceExample code for the SADI BMI Calculator Web Service
Example code for the SADI BMI Calculator Web Service
 
public class Point {   Insert your name here    private dou.pdf
public class Point {    Insert your name here    private dou.pdfpublic class Point {    Insert your name here    private dou.pdf
public class Point {   Insert your name here    private dou.pdf
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
00-intro-to-classes.pdf
00-intro-to-classes.pdf00-intro-to-classes.pdf
00-intro-to-classes.pdf
 
C++ Core Guidelines
C++ Core GuidelinesC++ Core Guidelines
C++ Core Guidelines
 
import java.util.Scanner;import java.text.DecimalFormat;import j.pdf
import java.util.Scanner;import java.text.DecimalFormat;import j.pdfimport java.util.Scanner;import java.text.DecimalFormat;import j.pdf
import java.util.Scanner;import java.text.DecimalFormat;import j.pdf
 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
 
Write a class that implements the BagInterface. BagInterface should .pdf
Write a class that implements the BagInterface.  BagInterface should .pdfWrite a class that implements the BagInterface.  BagInterface should .pdf
Write a class that implements the BagInterface. BagInterface should .pdf
 
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docxSinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList    se.docx
SinglyLinkedListPseudoCode.txtCLASS SinglyLinkedList se.docx
 
Intro to Ember.JS 2016
Intro to Ember.JS 2016Intro to Ember.JS 2016
Intro to Ember.JS 2016
 
operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
 operating system Ubunut,Linux,Mac filename messageService.cpp.pdf operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
operating system Ubunut,Linux,Mac filename messageService.cpp.pdf
 
Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2Meet Elcodi, the flexible e-commerce components built on Symfony2
Meet Elcodi, the flexible e-commerce components built on Symfony2
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
I keep getting an error about m_pHead in printStoresInfo(); function.pdf
I keep getting an error about m_pHead in printStoresInfo(); function.pdfI keep getting an error about m_pHead in printStoresInfo(); function.pdf
I keep getting an error about m_pHead in printStoresInfo(); function.pdf
 

More from AKPLAYZONEKOTA255

Find the proportion of scores that are greater than the following Z-sc.pdf
Find the proportion of scores that are greater than the following Z-sc.pdfFind the proportion of scores that are greater than the following Z-sc.pdf
Find the proportion of scores that are greater than the following Z-sc.pdf
AKPLAYZONEKOTA255
 
Find the running time equation- T(n)- of this program- Note- list's po.pdf
Find the running time equation- T(n)- of this program- Note- list's po.pdfFind the running time equation- T(n)- of this program- Note- list's po.pdf
Find the running time equation- T(n)- of this program- Note- list's po.pdf
AKPLAYZONEKOTA255
 
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdfFind the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
AKPLAYZONEKOTA255
 
Find the following in the thin section of a lime stone sample below (1).pdf
Find the following in the thin section of a lime stone sample below (1).pdfFind the following in the thin section of a lime stone sample below (1).pdf
Find the following in the thin section of a lime stone sample below (1).pdf
AKPLAYZONEKOTA255
 
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdfFind the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
AKPLAYZONEKOTA255
 
Find the mean of the data summarized in the given frequency distributi (1).pdf
Find the mean of the data summarized in the given frequency distributi (1).pdfFind the mean of the data summarized in the given frequency distributi (1).pdf
Find the mean of the data summarized in the given frequency distributi (1).pdf
AKPLAYZONEKOTA255
 
Find the average annual growth rate of the dividends for each firm lis.pdf
Find the average annual growth rate of the dividends for each firm lis.pdfFind the average annual growth rate of the dividends for each firm lis.pdf
Find the average annual growth rate of the dividends for each firm lis.pdf
AKPLAYZONEKOTA255
 
Find the ares of the shaded repon- The grach depicts the standard norm.pdf
Find the ares of the shaded repon- The grach depicts the standard norm.pdfFind the ares of the shaded repon- The grach depicts the standard norm.pdf
Find the ares of the shaded repon- The grach depicts the standard norm.pdf
AKPLAYZONEKOTA255
 
Find the area of the shaded region- The graph depicts the standard nor (1).pdf
Find the area of the shaded region- The graph depicts the standard nor (1).pdfFind the area of the shaded region- The graph depicts the standard nor (1).pdf
Find the area of the shaded region- The graph depicts the standard nor (1).pdf
AKPLAYZONEKOTA255
 
Find the area of the shaded region- The graph to the right depicts IQ (1).pdf
Find the area of the shaded region- The graph to the right depicts IQ (1).pdfFind the area of the shaded region- The graph to the right depicts IQ (1).pdf
Find the area of the shaded region- The graph to the right depicts IQ (1).pdf
AKPLAYZONEKOTA255
 
Find f(4) if f(x)-7x42x.pdf
Find f(4) if f(x)-7x42x.pdfFind f(4) if f(x)-7x42x.pdf
Find f(4) if f(x)-7x42x.pdf
AKPLAYZONEKOTA255
 
Find L(G) for SaS-bS-a.pdf
Find L(G) for SaS-bS-a.pdfFind L(G) for SaS-bS-a.pdf
Find L(G) for SaS-bS-a.pdf
AKPLAYZONEKOTA255
 
Find L(G)SaSa-bSb-.pdf
Find L(G)SaSa-bSb-.pdfFind L(G)SaSa-bSb-.pdf
Find L(G)SaSa-bSb-.pdf
AKPLAYZONEKOTA255
 
Find and install a Wi-Fi analyzer application on your computer or mobi.pdf
Find and install a Wi-Fi analyzer application on your computer or mobi.pdfFind and install a Wi-Fi analyzer application on your computer or mobi.pdf
Find and install a Wi-Fi analyzer application on your computer or mobi.pdf
AKPLAYZONEKOTA255
 
Find a current event tbqt relates to the case and write the connection.pdf
Find a current event tbqt relates to the case and write the connection.pdfFind a current event tbqt relates to the case and write the connection.pdf
Find a current event tbqt relates to the case and write the connection.pdf
AKPLAYZONEKOTA255
 
Fill in the missing information on the cost of goods manufactured sche.pdf
Fill in the missing information on the cost of goods manufactured sche.pdfFill in the missing information on the cost of goods manufactured sche.pdf
Fill in the missing information on the cost of goods manufactured sche.pdf
AKPLAYZONEKOTA255
 
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdfFind (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
AKPLAYZONEKOTA255
 
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdfFinancial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
AKPLAYZONEKOTA255
 
Finance RatesDetermine the monthly payment for the installment loan- C.pdf
Finance RatesDetermine the monthly payment for the installment loan- C.pdfFinance RatesDetermine the monthly payment for the installment loan- C.pdf
Finance RatesDetermine the monthly payment for the installment loan- C.pdf
AKPLAYZONEKOTA255
 
Fill in the P(X-x) values to give a legitimate probability distributio.pdf
Fill in the P(X-x) values to give a legitimate probability distributio.pdfFill in the P(X-x) values to give a legitimate probability distributio.pdf
Fill in the P(X-x) values to give a legitimate probability distributio.pdf
AKPLAYZONEKOTA255
 

More from AKPLAYZONEKOTA255 (20)

Find the proportion of scores that are greater than the following Z-sc.pdf
Find the proportion of scores that are greater than the following Z-sc.pdfFind the proportion of scores that are greater than the following Z-sc.pdf
Find the proportion of scores that are greater than the following Z-sc.pdf
 
Find the running time equation- T(n)- of this program- Note- list's po.pdf
Find the running time equation- T(n)- of this program- Note- list's po.pdfFind the running time equation- T(n)- of this program- Note- list's po.pdf
Find the running time equation- T(n)- of this program- Note- list's po.pdf
 
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdfFind the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
Find the percentile for the data value- Data set- 44-14-51-85-51-51-7-.pdf
 
Find the following in the thin section of a lime stone sample below (1).pdf
Find the following in the thin section of a lime stone sample below (1).pdfFind the following in the thin section of a lime stone sample below (1).pdf
Find the following in the thin section of a lime stone sample below (1).pdf
 
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdfFind the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
Find the median of these numbers- 4- 2- 7- 5- 3- 5.pdf
 
Find the mean of the data summarized in the given frequency distributi (1).pdf
Find the mean of the data summarized in the given frequency distributi (1).pdfFind the mean of the data summarized in the given frequency distributi (1).pdf
Find the mean of the data summarized in the given frequency distributi (1).pdf
 
Find the average annual growth rate of the dividends for each firm lis.pdf
Find the average annual growth rate of the dividends for each firm lis.pdfFind the average annual growth rate of the dividends for each firm lis.pdf
Find the average annual growth rate of the dividends for each firm lis.pdf
 
Find the ares of the shaded repon- The grach depicts the standard norm.pdf
Find the ares of the shaded repon- The grach depicts the standard norm.pdfFind the ares of the shaded repon- The grach depicts the standard norm.pdf
Find the ares of the shaded repon- The grach depicts the standard norm.pdf
 
Find the area of the shaded region- The graph depicts the standard nor (1).pdf
Find the area of the shaded region- The graph depicts the standard nor (1).pdfFind the area of the shaded region- The graph depicts the standard nor (1).pdf
Find the area of the shaded region- The graph depicts the standard nor (1).pdf
 
Find the area of the shaded region- The graph to the right depicts IQ (1).pdf
Find the area of the shaded region- The graph to the right depicts IQ (1).pdfFind the area of the shaded region- The graph to the right depicts IQ (1).pdf
Find the area of the shaded region- The graph to the right depicts IQ (1).pdf
 
Find f(4) if f(x)-7x42x.pdf
Find f(4) if f(x)-7x42x.pdfFind f(4) if f(x)-7x42x.pdf
Find f(4) if f(x)-7x42x.pdf
 
Find L(G) for SaS-bS-a.pdf
Find L(G) for SaS-bS-a.pdfFind L(G) for SaS-bS-a.pdf
Find L(G) for SaS-bS-a.pdf
 
Find L(G)SaSa-bSb-.pdf
Find L(G)SaSa-bSb-.pdfFind L(G)SaSa-bSb-.pdf
Find L(G)SaSa-bSb-.pdf
 
Find and install a Wi-Fi analyzer application on your computer or mobi.pdf
Find and install a Wi-Fi analyzer application on your computer or mobi.pdfFind and install a Wi-Fi analyzer application on your computer or mobi.pdf
Find and install a Wi-Fi analyzer application on your computer or mobi.pdf
 
Find a current event tbqt relates to the case and write the connection.pdf
Find a current event tbqt relates to the case and write the connection.pdfFind a current event tbqt relates to the case and write the connection.pdf
Find a current event tbqt relates to the case and write the connection.pdf
 
Fill in the missing information on the cost of goods manufactured sche.pdf
Fill in the missing information on the cost of goods manufactured sche.pdfFill in the missing information on the cost of goods manufactured sche.pdf
Fill in the missing information on the cost of goods manufactured sche.pdf
 
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdfFind (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
Find (a) marginal distributions f(x) and g(y)- (b) E(X) and E(Y)- (c).pdf
 
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdfFinancial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
Financial analysts forecast Safeco Corp's (SAF) growth rate for the fu.pdf
 
Finance RatesDetermine the monthly payment for the installment loan- C.pdf
Finance RatesDetermine the monthly payment for the installment loan- C.pdfFinance RatesDetermine the monthly payment for the installment loan- C.pdf
Finance RatesDetermine the monthly payment for the installment loan- C.pdf
 
Fill in the P(X-x) values to give a legitimate probability distributio.pdf
Fill in the P(X-x) values to give a legitimate probability distributio.pdfFill in the P(X-x) values to give a legitimate probability distributio.pdf
Fill in the P(X-x) values to give a legitimate probability distributio.pdf
 

Recently uploaded

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 

Recently uploaded (20)

Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

Fill in the missing code for C++- Code from part B is below to make co.pdf

  • 1. Fill in the missing code for C++. Code from part B is below to make copy-pasting easier: *-------------------------------------------------------------------------*/ /* * $ g++ -lpthread -g buyTheCandy.cpp -o buyTheCandy */ //-- Standard inclusions: --// #include <cstdlib> #include <cstdio> #include <unistd.h> #include <pthread.h> //-- Constants: --// // PURPOSE: To the names of the children const char* CHILD_NAME_ARRAY[] = {"Alice", "Bob", "Cathy", "David" }; // PURPOSE: To tell the number of children. const int NUM_CHILDREN = sizeof(CHILD_NAME_ARRAY)/sizeof(const char*); // PURPOSE: To tell the possible denominations of the Coin instances. const int COIN_DENOMINATION_ARRAY[] = {1,5,10,25}; // PURPOSE: To tell the number of coins in each new Purse. const int NUM_COINS_PER_NEW_PURSE = 4; // PURPOSE: To tell the number of denominations. const int NUM_DENOMINATIONS = sizeof(COIN_DENOMINATION_ARRAY)/sizeof(int); // PURPOSE: To tell how much money needs to be collected to buy the candy. const int CANDY_COST = 100; //-- Classes: --//
  • 2. // PURPOSE: To represent coins. class Coin { // I. Member vars: // PURPOSE: To tell the denomination of '*this' Coin instance. int denomination_; // PURPOSE: To hold the address of the next Coin after '*this' one, // or 'NULL' if there is no such Coin instance. Coin* nextPtr_; // II. Disallowed auto-generated methods: // No default constructor: Coin (); // No copy constructor: Coin (const Coin&); // No copy assignment op: Coin operator= (const Coin&); protected : // III. Protected methods: public : // IV. Constructor(s), assignment op(s), factory(s) and destructor: // PURPOSE: To make a coin of denomination 'newDom'. No return value. Coin (int newDom ) : denomination_(newDom), nextPtr_(NULL) { } // PURPOSE: To release the resources of '*this'. No parameters. // No return value.
  • 3. ~Coin () { } // V. Accessors: // PURPOSE: To return the denomination of '*this' Coin instance. // No parameters. int getDenomination () const { return(denomination_); } // PURPOSE: To return the address of the next Coin after '*this' one, // or 'NULL' if there is no such Coin instance. No parameters. Coin* getNextPtr () const { return(nextPtr_); } // VI. Mutators: // PURPOSE: To note that the Coin instance with address 'coinPtr' comes // after '*this' one. No return value. void setNextPtr (Coin* coinPtr ) { nextPtr_ = coinPtr; } // VII. Methods that do main and misc work of class: // PURPOSE: Try creating and return the address of a random coin. // No parameters. static Coin* makeRandom () { // I. Application validity check: // II. Try creating and return a coin: return(new Coin(COIN_DENOMINATION_ARRAY[rand() % NUM_DENOMINATIONS])); } }; // PURPOSE: To implement a list of Coin instances. class Purse { // 0. Constants: enum { NO_OWNER_INDEX = -1 };
  • 4. // I. Member var: // PURPOSE: To hold the index of the name of the owner of '*this' Purse // instance, or 'NO_OWNER_INDEX' if '*this' has no owner. int index_; // ADD YOUR VARIABLES HERE // II. Disallowed auto-generated methods: // No copy constructor: Purse (const Purse&); // No copy-assignment op: Purse operator= (const Purse&); protected : // III. Protected methods: public : // IV. Constructor(s), assignment op(s), factory(s) and destructor: // PURPOSE: To initialize '*this' to an empty purse Purse () : index_(NO_OWNER_INDEX) { // INITIALIZE YOUR VARS HERE } // PURPOSE: To initialize '*this' Purse instance to have // NUM_COINS_PER_NEW_PURSE random Coin instances owned by the child with // index 'newIndex'. No return value. Purse (int newIndex ) : index_(newIndex) { // INITIALIZE YOUR VARS HERE for (int i = 0; i < NUM_COINS_PER_NEW_PURSE; i++) {
  • 5. addToBack(Coin::makeRandom()); } } // PURPOSE: To release the resources of '*this'. No parameters. // No return value. ~Purse () { Coin* run; Coin* nextPtr; // GET RID OF YOUR VARS HERE } // V. Accessors: // PURPOSE: To return the index of the name of the owner of '*this' Purse // instance, or 'NO_OWNER_INDEX' if '*this' has no owner. int getIndex () const { return(index_); } // PURPOSE: To return the name of the owner of '*this' Purse instance. // No parameters. const char* getOwnerNameCPtr() const { return( (getIndex() == NO_OWNER_INDEX) ? "common" : CHILD_NAME_ARRAY[getIndex()] ); } // PURPOSE: To return the value of the money in '*this' wallet. // No parameters. int getValue () const { const Coin* run; int sum = 0; // YOUR CODE HERE
  • 6. return(sum); } // PURPOSE: To tell the number of Coin instances in '*this'. int getNumCoins () const { return(numCoins_); } // VI. Mutators: // PURPOSE: To add the Coin with address 'coinPtr' to '*this' Purse. // No return value. void addToBack (Coin* coinPtr ) { // YOUR CODE HERE } // PURPOSE: To remove the Coin at the beginning of '*this' and return // its address. No parameters. Coin* removeFromFront () { Coin* returnMe = NULL; // YOUR CODE HERE return(returnMe); } // VII. Methods that do main and misc. work of class: // PURPOSE: To print the status of '*this' Purse to 'stdout'. // No parameters. No return value. void print () const { printf ("%s has %d coins worth %d cents.n", getOwnerNameCPtr(),getNumCoins(),getValue() ); } };
  • 7. // PURPOSE: To implement a thread-safe version of Purse. class CommonPurse : public Purse { // I. Member vars: // ADD YOUR VARIABLES HERE // PURPOSE: To tell the current turn. int turn_; // II. Disallowed auto-generated methods: // No copy constructor: CommonPurse (const CommonPurse&); // No copy assignment op: CommonPurse operator= (const CommonPurse&); protected : // III. Protected methods: public : // IV. Constructor(s), assignment op(s), factory(s) and destructor: // PURPOSE: To initialize '*this' to an empty shared Purse instance. CommonPurse () : turn_(NUM_CHILDREN-1) { // INITIALIZE YOUR VARS HERE } // PURPOSE: To release the resources of '*this'. No parameters. // No return value. ~CommonPurse () { // GET RID OF YOUR VARS HERE
  • 8. } // V. Accessors: // PURPOSE: To tell the current turn. int getTurn () const { return(turn_); } // VI. Mutators: // VII. Methods that do the main and misc. work of class: // PURPOSE: To consolidate the Coin instances of '*donorPursePtr' into // '*this' one. // (Note to the professional C++ coders: Yeah, I know it is better to use // call-by-reference. I use pointers for consistency.) // PUT CODE 3 PLACES IN HERE . . . I WONDER WHERE? void consolidate (Purse* donorPursePtr ) { // I. Application validity check: // II. Transfer the wealth: while (getTurn() != donorPursePtr->getIndex()) { printf ("%s: "I want candy! Let's put our money together!"n", donorPursePtr- >getOwnerNameCPtr() ); } while (donorPursePtr->getNumCoins() > 0) { addToBack(donorPursePtr->removeFromFront()); } printf ("%s: "I added my money."n", donorPursePtr->getOwnerNameCPtr() ); turn_--; // III. Finished: } };
  • 9. //-- Global vars: --// // PURPOSE: To represent the shared, thread-safe Purse instance. CommonPurse sharedPurse; //-- Main functions: --// // PURPOSE: To add the Coin instances in '*(Purse*)vPtr' to 'sharedPurse'. // Returns 'NULL'. void* consolidate (void* vPtr ) { Purse* pursePtr = NULL; // CHANGE THAT NULL // YOUR CODE HERE return(NULL); } // PURPOSE: To have the children add their money to 'sharedPurse', and // then attempt to buy the candy. Ignores parameters. Returns / / 'EXIT_SUCCESS' to OS. int main () { // I. Application validity check: // II. Try to buy candy: // II.A. Initialize data-structures: Purse* pursePtrArray[NUM_CHILDREN]; srand(getpid()); for (int i = 0; i < NUM_CHILDREN; i++) { pursePtrArray[i] = new Purse(i); pursePtrArray[i]->print(); }
  • 10. // II.B. Consolidate the money: // YOUR CODE HERE TO START THREADS // II.C. Close program down: // YOUR CODE HERE TO WAIT FOR THREADS for (int i = 0; i < NUM_CHILDREN; i++) { delete(pursePtrArray[i]); } printf("All: "We have %d cents. ",sharedPurse.getValue()); if (sharedPurse.getValue() >= CANDY_COST) { printf("Yay! We can afford the candy!"n"); } else { printf("Time to beg mom for more money!"n"); } // III. Finished: return(EXIT_SUCCESS); } A package of 4 candy candies cost 100 cents. (It can be 1 USD, 1 Euro, 1 whatever). Four siblings (brothers or sisters) want a candy, and each has some money. Unfortunately, probably none of the siblings has enough money to buy the candies by themself. So they will all pool their money and jointly buy the candy. There are 3 classes: - Coin: Represents some money - Purse: Implements a container (a linked list) of Coin instances. - Commonpurse: a thread-safe version of purse Each sibling has their money in a purse instance. Additionally, there is a Commonpurse called sharedpurse to which all siblings will add their money. Because each sibling is implemented with its own thread, the individual Purse instances do not need to be thread safe. However, sharedpurse needs to be made thread-safe because all four threads can access it. B. Cut-and-paste the following: // PURPOSE: To have the children add their money to 'sharedPurse', and // then attempt to buy the candy. Ignores parameters. Returns // 'EXIT_SUCCESS' to OS. int main () { I/ I. Application validity check: // II. Try to buy candy: // II.A. Initialize data-structures: Purse* pursePtrarray[NUM_CHILDREN]; srand (getpid()); for ( int i = 0 ; i < NUM_CHILDREN; i + + ) { purseptrarray[i] [ i ] new Purse ( i ) ; pursePtrarray
  • 11. [i]->print (); } // II.B. Consolidate the money: I/ YOUR CODE HERE TO START THREADS // II.C. Close program down: I/ YOUR CODE HERE TO WAIT FOR THREADS for (int i = 0 ; i < NUM_CHILDREN; i + + ) { delete (purseptrarray [i]); } printf("All: "We have ofd cents. ", sharedPurse.getValue()); if (sharedPurse.getValue ( ) >= CANDY_COST) { printf("Yay! We can afford the candy! " "); } else { printf("Time to beg mom for more money! "n"); } // III. Finished: return(EXIT_SUCCESS); } Purse must implement a linked list of Coin instances using the coin methods getNext.ptr() and setNextPtr ( ). (No cheating and using C++ containers like std: 1 ist, std x vector, etc.) 1. Give class Purse 3 member variables: - A coin to point to the beginning of the list. - A Coin* to point to the end of the list. - An int that keeps track of the length of the list. 2. Initialize your variables in the constructor. 3. Get rid of your list in the destructor method: -Purse (). In C one gets memory with malloc ( ) and gives it back with free ( ). However, in C++ one gets memory and builds an instance of an object with new, and one dismantles the instance with delete(). Please have a local variable like coinPtr and say delete (coinPtr) for cach coin in the list. 4. Make getvalue() loop thru the coin instances. It should sum all the getDenomination () values of the coin instances, and return() the sum. 5. Make getNumcoins () returns the how many coin instances are in the list. 6. Make addToBack () add coinPtr to the back of the list. It should also increment your list-length variable. 7. Make removefromfront () remove the first coin instance from the linked list. It should also decrement your list-length variable, and return () the address of the first coin. D. Make it multi-threaded: 1. In main () you will need an array of NUN_CHILDREN pthread_t instances for the bee hive threads. a. In section II . declare your array variable. Have a loop that starts all NUN_CHILDREN threads. Each thread should run consolidate (), and pass the address of the corresponding purse as an argument. b. In section II . C wait for all child threads to finish. 2. In consolidate (), argument vPtr comes in pointing to a Purse. Set pursePtr cqual to vPtr (you will need to cast it). The only thing the function needs to do is run sharedpurse, consolidate (purseptr). 3. Now run it! E. Make it thread-safe: Congratulations! If you got this far you have made it multi-threaded, but not thread-safe. To make it thread-safe you will have to add some mutex(es) and condition(s). You need to protect access to the linked list and turn_in Commonpurse method consolidate( ) . a. It needs one pthread_mutex_t variable and an array of NUM_CHILDREN + 1 pthread_cond_t instances. b. Initialize those variables in CommonPurse () . c. Destroy those variables in CommonPurse ( ). d. Use them in consolidate ( ) in Commonpurse. Where does the critical section begin? Where does the critical section end? Note: all threads should wait on the pthread_cond_t with index donorpursePtr->getindex () + 1 . Also, all threads should signal the pthread_cond_t with index donorPursePtr->getindex ( ). F. Questions: How well did your program work before making it thread safe? How well did your program work after making it thread safe? G. Sample output: $ ./buyTheCandy Alice has 4 coins worth 41 cents. Bob has 4 coins worth 50 cents. Cathy has 4 coins worth 16 cents. David has 4 coins worth 50 cents. Bob: "I want candy! Let's put our money together!" Cathy: "I want candy! Let's put our money together!" David: "I added my money." Alice: "I want candy! Let's put our money together!" Cathy: "I added my money." Bob: "I added my money." Alice: "I added my money." All: "We have 157 cents. Yay! We can afford the candy!" $ . /buyTheCandy Alice has 4 coins worth 12 cents. Bob has 4 coins worth 36 cents. Cathy has 4 coins worth 8 cents. David has 4 coins worth 16 cents. Alice: "I want candy! Let's put our money together!" Cathy: "I want candy! Let's put our money together!" Bob: "I want candy! Let's put our money together!" David: "I added my money." Cathy: "I added my money." Bob: "I added my money." Alice: "I added my money." All: "We have 72 cents. Time to beg mom for more money!" $
  • 12. ./buyTheCandy Alice has 4 coins worth 37 cents. Bob has 4 coins worth 22 cents. Cathy has 4 coins worth 80 cents. David has 4 coins worth 21 cents. Alice: "I want candy! Let's put our money together!" Bob: "I want candy! Let's put our money together!" David: "I added my money." Cathy: "I added my money." Bob: "I added my money." Alice: "I added my money." All: "We have 160 cents. Yay! We can afford the candy!"