Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

%%%%% c++ assignment %%%%%% ///////// please test your program and check for errors. //////// you should use the file bellow For this assignment you will

%%%%% c++ assignment %%%%%%

///////// please test your program and check for errors.

//////// you should use the file bellow

For this assignment you will write a program that creates a tree of "plants". Each plant is the result of an exeperiment. I have provided the main driver program that is responsible for running each of the experiments but you will need to create a tree for storing the results. The objective of this assignment is to learn how to implement and use a binary tree (not a binary search tree). Since this is the objective, you cannot use a premade classes. Each plant, in this assignment, has three important factors that determine how well the plant can be used to generate enough food for the colony. The three factors are the growth rate, nutritional value, and water requirements. The higher a score for any of these factors, the more desireable the plant is. Each plant has been genetically modified so that when it reproduces it creates exactly two new plants. The new plants have different values for each of the important factors. This assignment runs a couple of experiments with different starting plants. Each experiment starts with a plant and then finds the children for that plant. Then it finds the children for each of those, and so on. As it finds the children it stores them in a binary tree with the starting plant as the root. The experiment has a depth limit and only runs the specified number of generations. When all of the reproduction is done (for the specified number of generations), the program searches the result tree to find the best plant for each of the given factors. The output of the program is the full reproduction tree and a display of the best of each of the factors.

---------------------------------------------------

Program Design

Your program should use good design methodologies so you should have separate classes for each of the following:

- plant -- This class represents a plant. Each plant has an ID as well as a growth rate factor, nutritional value factor, and water requirement factor. The plant class should have these data members. The plant ID follows the format "Plant ??-??-??" where the first two digits are the growth value, the second two are the nutritional value, and the third two are the water requirements value.

- planttree -- This is a tree that holds the plant data. Each non-leaf node in this tree will have exactly two children. The children are not sorted in any way. The planttree class has a method for setting the root of the tree as well as setting the children for a given node. See the experiments.cpp file to see what the method signature is for each of these.

- treenode -- this will be a class (or struct, your choice) to hold a plant object and the two children pointers.

Finally, for your convenience I have provided a "makefile". If you name all of your files the same as I have then you can use the makefile to simplify your building and testing.

---------------------------------------------------------

External Requirements

- The main driver (experiments.cpp) will run a series of experiments with a couple different starting plants. The output from your program must match expected.txt exactly.

----------------------------------------------------------------

Internal Requirements

- The program must use the supplied experiments.cpp file, unmodified, as the main driver. - The program must use a binary tree, that you implement, to store the experiment results. All results should be stored in the tree before it is printed and before the best plants are found. - The plant class should not have any pointers to other plant classes in it. - The planttree nodes should not contain parent pointers. That is, each node in the tree should only have two pointers -- one for left and one for right. - The should be no memory leaks or memory errors (as repoted by valgrind) - All "string" data should be stored as char* variables. DO NOT USE std::string. - Any classes that contain pointers as data members must have a copy constructor, assignment operator, and destructor.

----------------------------------------------------------------

experiments.cpp

#include

#include

#include "plant.h"

#include "planttree.h"

using namespace std;

void showResults(planttree pt)

{

pt.display();

const plant* bestPlant;

cout << endl;

cout << "Best growth rate: " << endl;

bestPlant = pt.findBestGrowth();

if (bestPlant != nullptr)

cout << (*bestPlant) << endl;

else

cout << "ERROR: null best plant" << endl;

cout << "Best nutritional value: " << endl;

bestPlant = pt.findBestNutrition();

if (bestPlant != nullptr)

cout << (*bestPlant) << endl;

else

cout << "ERROR: null best plant" << endl;

cout << "Best water requirement: " << endl;

bestPlant = pt.findBestWater();

if (bestPlant != nullptr)

cout << (*bestPlant) << endl;

else

cout << "ERROR: null best plant" << endl;

}

plant getChildPlant(plant parent,int co[])

{

int gx = parent.getGrowth() - 50;

int nx = parent.getNutrition() - 50;

int wx = parent.getWater() - 50;

gx = abs(co[0] * (gx * gx * gx) + co[1] * ( gx * gx ) + co[2] * gx + co[3]);

gx = gx % 100;

nx = abs(co[4] * (nx * nx * nx) + co[5] * ( nx * nx ) + co[6] * nx + co[7]);

nx = nx % 100;

wx = abs(co[8] * (wx * wx * wx) + co[9] * ( wx * wx ) + co[10] * wx + co[11]);

wx = wx % 100;

char* plantId = new char[15]; // max size ==> Plant ??-??-?? == 15 chars

sprintf(plantId,"Plant %d-%d-%d",gx,nx,wx);

plant plant(plantId,gx,nx,wx);

delete [] plantId;

return(plant);

}

void runSingleExperiment(planttree& pt,int depth,plant& parentPlant)

{

if (depth > 0)

{

int leftCoeffs[] = {13,3,11,7,2,23,5,29,3,37,11,13};

int rightCoeffs[] = {128,16,64,2,32,8,2,128,256,16,16,64};

plant leftPlant = getChildPlant(parentPlant,leftCoeffs);

plant rightPlant = getChildPlant(parentPlant,rightCoeffs);

pt.addChildren(parentPlant,leftPlant,rightPlant);

runSingleExperiment(pt,depth-1,leftPlant);

runSingleExperiment(pt,depth-1,rightPlant);

}

}

void runExperiment(const char* title, plant startingPlant, int depth)

{

planttree pt;

pt.setRoot(startingPlant);

runSingleExperiment(pt,depth,startingPlant);

cout << "===================================" << endl;

cout << title << endl;

cout << "===================================" << endl;

showResults(pt);

cout << endl;

cout << endl;

}

int main()

{

runExperiment("Experiment 1",plant("Plant 1-1-1",1,1,1),5);

runExperiment("Experiment 2",plant("Plant 11-17-33",11,17,33),5);

return(0);

}

--------------------------------------------------------------------

makefile

CC=g++

CPPFLAGS = -std=c++11 -I. -g

DEPS = plant.h planttree.h

OBJ = plant.o planttree.o experiments.o

%.o: %c $(DEPS)

$(CC) $(CPPFLAGS) -c -o $@

experiments: $(OBJ)

$(CC) $(CPPFLAGS) -o $@ $^

valgrind --leak-check=yes ./$@

testtree: plant.o planttree.o testtree.o

$(CC) $(CPPFLAGS) -o $@ $^

valgrind --leak-check=yes ./$@

testplant: plant.o testplant.o

$(CC) $(CPPFLAGS) -o $@ $^

valgrind --leak-check=yes ./$@

---------------------------------------------------

Expected.txt

// some data of the output

=================================== Experiment 1 =================================== Plant ID: Plant 1-1-1 (G: 1 N: 1 W: 1) Plant ID: Plant 66-91-36 (G: 66 N: 91 W: 36) Plant ID: Plant 99-39-21 (G: 99 N: 39 W: 21) Plant ID: Plant 86-95-56 (G: 86 N: 95 W: 56) Plant ID: Plant 19-79-59 (G: 19 N: 79 W: 59) Plant ID: Plant 58-0-8 (G: 58 N: 0 W: 8)

..

..

Plant ID: Plant 82-51-8 (G: 82 N: 51 W: 8) Plant ID: Plant 66-50-16 (G: 66 N: 50 W: 16) Plant ID: Plant 74-72-12 (G: 74 N: 72 W: 12) Plant ID: Plant 11-67-93 (G: 11 N: 67 W: 93) Plant ID: Plant 26-80-72 (G: 26 N: 80 W: 72)

Best growth rate: Plant ID: Plant 99-39-21 (G: 99 N: 39 W: 21) Best nutritional value: Plant ID: Plant 82-99-88 (G: 82 N: 99 W: 88) Best water requirement: Plant ID: Plant 34-95-96 (G: 34 N: 95 W: 96)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions