Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume you are working for a company that is creating a virtual reality game set

ID: 3574270 • Letter: A

Question

Assume you are working for a company that is creating a virtual reality game set in a heavily forested environment. As such you need to create a randomly variable class tree. Each tree is to be placed on a terrain in an integer X-Y space. Each tree is to be assigned a species and a height based on that value. No tree can exist in the same space as another. All trees should be stored in a linked list. Accept from user input how many trees they want, generate them and then display them to the screen. Height range Species 12 to 25 feet Elm. 8 to 50 feet Oak 4 to 40 feet Pine

Explanation / Answer

This is a sample C++ program to implement the above functionality.

#include <iostream>
#include <string>
#include <vector>

// Tree class containning height and species.
class Tree{
int height;
string species;

Tree(int height, string species){
this.height = height;
this.species = species;
}
};

// List been defined to contain the Tree.
vector<Tree> lst;

int main(){
int numberOfTrees = 0;

// Reading number of Trees required by user.
cout << "Enter the number of trees: ";
cin >> numberOfTrees;

// static variables to generate height based on initial height.
static int j = 0;
static int height1 = 12;
static int height2 = 8;
static int height3 = 4;

// Loop to generate the Height
for(int i =0 ;i<numberOfTrees; i++){

// j is used to access one of the trees.
if(j>4)
j = 0;
else
j++;

// Here we calculate the height and create a Tree object and store in list.
switch(j){
case 1: {
if(height1 > 25)
height1 = 12;
else
height1 += 2;

Tree t = new Tree(height1, "Elm");
lst.push_back(t);
}
break;
case 2: {
if(height2 > 50)
height2 = 8;
else
height2 += 4;

Tree t = new Tree(height2, "Oak");
lst.push_back(t);
}
break;
case 3: {
if(height3 > 40)
height3 = 4;
else
height3 += 6;

Tree t = new Tree(height3, "Pine");
lst.push_back(t);
}
break;
default:
break;
}
}   

  
// access values from the vector and displaying them.
for(i = 0; i < numberOfTrees; i++){
cout << "Tree [" << i << "] = " << lst[i] << endl;
}

  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote