Download the files dicelist.h and diceapp.cpp from the Homework 7 area of Blackb
ID: 3765210 • Letter: D
Question
Download the files dicelist.h and diceapp.cpp from the Homework 7 area of Blackboard. The dicelist.h implementation file starts as follows:
/*
dicelist header file
*/
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
// a node consists of data (two individual dice and the total) and a
// pointer to the next node in the list
struct dice_node
{
int dice1, dice2, total;
dice_node *next;
};
// a dice_ptr is a pointer to a dice node
typedef dice_node* dice_ptr;
// dicelist class represents a list of nodes
class dicelist
{
private:
dice_ptr front; // pointer to the front of the list
public:
dicelist ();
void roll ();
bool remove ();
void nuke ();
void print ();
int sum ();
int count ();
int count (int t);
double percent (int t);
};
You are not allowed to add any new private data to the class. The public functions that you need to define are:
dice_list (): Constructor that initializes the list to be empty. It also calls srand to set a random number seed.
void roll (): Adds a new random dice roll to the rear of the list.
bool remove (): Removes the front node from the list. Adds the node to free storage. Returns true if there is a node to remove, otherwise, returns false.
void nuke (): Makes the list empty. Adds the nodes in the list back to free store.
void print (): Prints the elements in the list, one per line, in the following format:
5 … 4 ==> 9
int sum (): Returns a sum of the dice totals in the list.
int count (): Returns the number of nodes in the list.
int count (int t): Returns the number of occurrences of parameter t as a total in the list.
double percent (int t): Returns the percentage of the occurrences of parameter t in the list.
The diceapp.cpp application file is coded except for the calls to the public functions. Add the calls as directed by the comments in the file.
There is a sample run posted in the assignment on Blackboard. Your functions should work for all cases (for example when the list is empty, nodes occurring at the ends of lists, etc.).
DICEAPP FILE
____________________________________________________________________________________
DICELIST.H FILE
________________________________________________________________________________-
Explanation / Answer
dicelist::dicelist(){
front = NULL;
srand(time(NULL));
}
void dicelist::roll(){
dice_node *dc = new dice_node();
dc->dice1 = rand() % 6 + 1;
dc->dice2 = rand() % 6 + 1;
dc->total = dc->dice1 + dc->dice2;
// Adding to the End
if (temp == NULL){
dc->next = front;
dice_ptr = dc;
return;
}
dice_node* temp = front;
while (temp->next != NULL)
temp = temp->next;
temp->next = dc;
}
bool dicelist::remove(){
if (front == NULL)
return fasle;
front = front->next;
return true;
}
void dicelist::nuke(){
front = NULL;
}
void dicelist::print(){
dice_node* temp = front;
while (temp != NULL){
cout << temp->dice1 << " ... " << temp->dice2 << " ==> " << temp->total << endl;
temp = temp->next;
}
}
int dicelist::sum(){
dice_node* temp = front;
int res = 0;
while (temp != NULL){
res += temp->total;
temp = temp->next;
}
return res;
}
int dicelist::count(){
dice_node* temp = front;
int res = 0;
while (temp != NULL){
res += 1;
temp = temp->next;
}
return res;
}
int dicelist::count(int t){
dice_node* temp = front;
int res = 0;
while (temp != NULL){
if (temp->total == t)
res += 1;
temp = temp->next;
}
return res;
}
double dicelist::percent(int t){
int n = count(t);
int total = count();
return (n*100.0)/total;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.