For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template.
Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations in code. However, they require more memory and usually slower than their non-recursive versions in execution, especially for a large amount of input data.
#ifndef H_BINARYTREE
#define H_BINARYTREE
template class BinaryTree{
public:
BinaryTree(); // default constructor
unsigned getSize() const; // returns size of tree
unsigned getHeight() const; // returns height of tree
virtual void Insert(const T&); // inserts node in tree
void Inorder(void (*)(const T&)); // inorder traversal of tree
protected:
Node *root; // root of tree
private:
unsigned _getSize(Node *) const; // private version of getSize()
unsigned _getHeight(Node *) const; // private version of getHeight()
void _Insert(Node *&, const T&); // private version of Insert()
void _Inorder(Node *, void (*)(const T&)); // private version of Inorder()
};
#endif // End of H_BINARYTREE
Because of information hiding, a client is not permitted to access the binary tree directly, so the root of the tree is kept protected (not private because of future implementations of derived classes from the base class of the BinaryTree), so it cannot be passed as an argument to any of the public functions of the tree. It is essential to have private utility functions, which act as interface between a client and the tree. The Insert() function of the BinaryTree class is described as follows:
typedef enum {left_side, right_side } SIDE;
SIDE rnd(){
return rand()%2 ? right_side : left_side;
}// End of rnd()
Put the implementation of your BinaryTree class in the header file binarytree.h. Definition of the class Node, which represents the nodes in a binary tree, can be found in the header file node.h. To use the class Node in your program, include the header file node.h, inserting #include "node.h" at the top of your header file.
The source file binarytreeDriver.cc contains the driver program. In addition to the main() routine, it has the implementations of the following routines (as templates) and the definitions of the two RNGs used in the main() routine.
The unary function print() can be used as an argument to the member functions Inorder() to print the value of its argument x. The function printValues() does the followings:
The class RND1 can be used to generate random integers in the range [LOW1 = –999, HIGH1 = 999] and the class RND2 can be used to generate random floating-point numbers in the range [LOW2 = –999.99, HIGH2 = 999.99]. The function objects RND1() and RND2(), generated from these classes, are used to fill in the random values in vector containers vector A(N1) and vector B(N2) by using the generate() function in the STL, where N1 = 100 and N2 = 50 are the sizes of these two vectors.
The main() routine copies the random values from vectors A and B and inserts them in the binary trees first and second, respectively. At the end, the data values in the binary trees first and second are printed out on stdout with LSIZE = 12 numbers in a single line.
Put the implementation of your BinaryTree class in the header file binarytree.h. Definition of the class Node, which represents the nodes in a binary tree, can be found in the header file node.h. To use the class Node in your program, include the header file node.h, inserting #include "node.h" at the top of your header file.
The source file binarytreeDriver.cc contains the driver program. In addition to the main() routine, it has the implementations of the following routines (as templates) and the definitions of the two RNGs used in the main() routine.
The unary function print() can be used as an argument to the member functions Inorder() to print the value of its argument x. The function printValues() does the followings:
The class RND1 can be used to generate random integers in the range [LOW1 = –999, HIGH1 = 999] and the class RND2 can be used to generate random floating-point numbers in the range [LOW2 = –999.99, HIGH2 = 999.99]. The function objects RND1() and RND2(), generated from these classes, are used to fill in the random values in vector containers vector A(N1) and vector B(N2) by using the generate() function in the STL, where N1 = 100 and N2 = 50 are the sizes of these two vectors.
The main() routine copies the random values from vectors A and B and inserts them in the binary trees first and second, respectively. At the end, the data values in the binary trees first and second are printed out on stdout with LSIZE = 12 numbers in a single line.
BINARYTREE.CC
#include <math.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#include "binarytree.h"
#define SEED 1 // seed for RNGs
#define N1 100 // size of 1st vector container
#define LOW1 -999 // low val for rand integer
#define HIGH1 999 // high val for rand integer
#define N2 50 // size of 2nd vector container
#define LOW2 -99999 // low val for rand float
#define HIGH2 99999 // high val for rand float
#define PREC 2 // no of digits after dec pt
#define LSIZE 12 // no of vals printed on line
#define ITEM_W 7 // no of spaces for each item
// prints single val
template <typename T>
void print(const T&);
// prints data vals in tree
template <typename T>
void print_vals(BinaryTree<T>&, const string&);
// class to generate rand ints
class RND1 {
private:
int low, high;
public:
RND1(const int& l = 0, const int& h = 1) : low(l), high(h) {}
int operator()() const { return rand() % (high - low + 1) + low; }
};
// class to generate rand floats
class RND2 {
private:
int low, high, prec;
public:
RND2(const int& l = 0, const int& h = 1, const int& p = 0) : low(l), high(h), prec(p) {}
float operator()() const { return (static_cast<float>(rand() % (high - low + 1) + low)) / pow(10, prec); }
};
// prints val passed as argument
template <typename T>
void print(const T& x) {
static unsigned cnt = 0;
cout << setw(ITEM_W) << x << ' ';
cnt++;
if (cnt % LSIZE == 0) cout << endl;
}
// prints size and height of bin tree and data val in
// each node in inorder
template <typename T>
void print_vals(BinaryTree<T>& tree, const string& name) {
cout << name << ": "; // print name of tree
// print size and height of tree
cout << "size = " << tree.getSize() << ", ";
cout << "height = " << tree.getHeight() << endl << endl;
// print data values of tree in inorder
cout << "Data values in '" << name << "' inorder:\n\n";
tree.Inorder(print);
cout << endl;
}
// driver program: to test several member functions of BinaryTree class
int main() {
srand(SEED); // set seed for RNGs
// create 1st vector and fill it with rand ints
vector<int> A(N1);
generate(A.begin(), A.end(), RND1(LOW1, HIGH1));
// create binary tree with int vals in vector A,
// and print all vals of tree
BinaryTree<int> first;
for (unsigned i = 0; i < A.size(); i++) first.Insert(A[i]);
print_vals(first, "first");
cout << endl;
// create 2nd vector and fill it with rand floats
vector<float> B(N2);
generate(B.begin(), B.end(), RND2(LOW2, HIGH2, PREC));
// create binary tree with float vals in vector B,
// and print all vals of tree
BinaryTree<float> second;
for (unsigned i = 0; i < B.size(); i++) second.Insert(B[i]);
print_vals(second, "second");
cout << endl;
return 0;
}
In: Computer Science
consider the code;
typedef struct {
int size;
int *nums;
}Data;
Complete the function below that creates and returns a new data type that contains a nums array with size randomly chosen integers. Ensure proper error checking.
Data *getRandomData(int size) {
//PUT CODE HERE
//Create random ints
for(int i=0; i<size; i++)
d->nums[1] = (int)(rand()/(float)RAND_MAX*100);
return d;
In: Computer Science
1. All members may be public for this example
2. You will need three data members:
a. An array of 3 doubles to keep the votes
b. An array of 3 strings to place the candidates names
c. An array of 3 doubles to place the calculates vote
breakdown
3. You will need three methods:
a. A constructor that receives three strings and sets the names of
the candidates to these strings and set all the votes initially to
zero.
b. A "calcPercentages" method that has no arguments or return
values and simply calculates each vote percentage as one vote count
divided by the sum times 100
c. A "display Results" method that has no arguments and displays
each candidate's names and vote percentage.
source:
#include <iostream> //for
cout
#include <string> //for
string
#include <stdlib.h> //for
rand, srand
#include <time.h> //for
time
using namespace std;
//DO NOT EDIT ANY CODE ABOVE THIS LINE EXCEPT FOR LINE 12 (EDITOR'S
NAME)
class CElection
{
public:
};
//DO NOT EDIT ANY CODE BELOW THIS LINE!
int main(void)
{
//Instantiate a CElection object with three
names:
CElection TOOP("Alice","Bob","Other");
//First display a hello message
cout << "\n\n* Welcome to Decision 2019
*\n";
//Randomize the PRN
srand(time(NULL));
int vote;
for (int i=1;i<=10000;i++)
{
vote = rand()%5;
switch (vote)
{
case 0:
case 1:
TOOP.votes[0]++;
break;
case 2:
case 3:
TOOP.votes[1]++;
break;
case 4:
TOOP.votes[2]++;
}
}
TOOP.calcPercentages();
TOOP.displayResults();
}
In: Computer Science
C programming
Rewrite the following function using no loops, and only
tail call recursion
double question5 (int in)
{
int i;
int result;
for (result = rand(), i = 0; i < in; i += 3)
{
result /= i;
result += rand();
}
return result;
}In: Computer Science
Objective
Make a function to serve as a Craps dice game simulator. If you are not familiar Craps is a game that involves rolling two six-sided dice and adding up the total. We are not implementing the full rules of the game, just a function that returns the value of BOTH dice and their total.
Details
In the function you create: Make a function that uses pointers and pass by reference to return THREE separate outputs (arrays are not allowed). Inside the function you will call rand() to represent a random dice roll of a six sided dice. One output will be the first call to rand() and represent the first dice roll. The second output will be a second call to rand() and represents the second dice roll. The third output is the total of the two other outputs added together.
You may choose either options for defining the function:
Option A:
Option B:
In the Main Function: Call srand() to initialize the pseudo-random algorithm. Create three variables to hold your outputs, the two dice rolls and the total. In a for loop call your dice roll function FIVE times, each time it should set the three variables to new values. Also within the for loop print the values of the dice rolls and the total.
In: Computer Science
One needs to be careful when writing formulas for simulation. "=round( rand() * 6, 0)" will take a value uniformly distributed between 0 and 1, multiply it by 6, and round it to the nearest integer. Using that formula, what is the probability of getting 6? (Note: probability is a number between 0 and 1; give your answer to three decimal places.)
Hint: rand() is uniformly distributed from 0 to 1. Hence rand()*6 is uniformly distributed from 0 to 6. Only values above 5.5 will be rounded to 6. We can find the probability from this.
Alternatively, we can simulate a thousand values of "=round(rand()*6,0)", say in the range A1:A1000. Now "=COUNTIF (A1:A1000, 6)" will give us a count of the value 6 in the range A1:A1000. From this we can estimate the probability.
In: Finance
In: Finance
A purely competitive firm finds that the market price for its product is $20.00. It has a fixed cost of $100.00 and a variable cost of $15.00 per unit for the first 50 units and then $25.00 per unit for all successive units.
A. Does price equal or exceed average variable cost for the first 50 units?
What is the average variable cost for the first 50 units?
B. Does price equal or exceed average variable cost for the first 100 units?
What is the average variable cost for the first 100 units?
C. What is the marginal cost per unit for the first 50 units?
What is the marginal cost for units 51 and higher?
D. For each of the first 50 units, does MR exceed MC?
What about for units 51 and higher?
E. What output level will yield the largest possible profit for this purely competitive firm?
In: Economics
Maggie’s aunt died in 2001 leaving her a home which has been the principal residence of Maggie and her husband ever since. Her aunt acquired the property in 1980 at a cost of $90,000. She (the aunt) made capital improvements over the years totaling $25,000. The property had a fair market value at the aunt’s death of $250,000. In 2003, Maggie and her husband added a tennis court (cost $25,000) and paid a city assessment for the installation of sidewalks on their street of $5,000. They sold the home in 2018 for an adjusted sales price of $800,000, and immediately purchased a new home at a cost of $600,000. Maggie and her husband file a joint return. Please identify, discuss, and resolve all tax issues associated with the acquisition and sale of both the home they inherited from Maggie’s aunt, and the purchase of the new home, including an analysis of the basis of their new home.
In: Accounting
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for.
To have your program roll two dice, use the rand() function this way:
die1 = rand() % 6 + 1; die2 = rand() % 6 + 1;
Your program then computes and prints the number of rolls it takes to get the given number. Valid numbers are 2 through 12.
If the user asked for a 2 your program should output:
It took 20 rolls to get a 2.
(your number of rolls will be different of course)
In: Computer Science