Question

Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

Assignment

Predator / Prey

Objectives

Reading from and writing to text files

Implementing mathematical formulas in C++

Implementing classes

Using vectors

Using command line arguments

Modifying previously written code

Tasks

For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food supply, and the increase of the predator population in the presence of a food source, as well as the initial populations of the prey and their predators, we can predict future population sizes, using these formulas:

preyn+1= preyn × (1+A−B × predn)

predn+1 = predn ×(1 −C +D  × preyn )

Where:

preyn is the population of the prey at time step n

A is the rate at which prey birth exceed natural deaths

B is the rate of predation

predatorn is the population of the predators at time step n

C is the rate at which predator deaths exceeds births without a food supply

D is the increase of the predator population in the presence of a food source

Therefore, given a set of initial conditions and the population at time step 0, the size of the populations can be predicted ‘x’ time steps, or periods into the future.

To implement this simulation, your program will read in the values for A, B, C, and D from a file along with the initial population sizes for the prey and predators. The file will contain data for one or more simulations. For each simulation, you will collect data on both population sizes until one of the populations goes to zero. Finally, you print to a file a simple graph representing this data, one for each simulation.

Files

You will be provided with a file called sim.dat, which will contain the data for all of the simulations.

Each line of the file will contain data for one simulation in the following order:

              A B C D initialPreyPopulation initialPredatorPopulation  

Therefore, a file with data for two simulations would read as follows:

0.1 0.01 0.01 0.00002 1000 20

0.5 0.02 0.01 0.0003 2000 50

For each run of your simulation, you will have to read in a line of data.

As you run each simulation, you will have to collect data on the number of time steps and the populations of the predators and prey at these time steps. This information will be printed as a graph to an output file. To print the graph, you will be provided with a function called printGraph, which you will find in the file, ‘PredatorPrey.cpp’. The printGraph function takes as parameters the output file stream, the simulation number, a vector of integers, the maximum size of the predator or prey population, and the number of generations for this simulation (the number of times the simulation looped until one of the populations went to 0 or below).

The integers in the vector will represent information about a given time step and the population of the prey and predators at that time step in an ordered triplet. For instance, if at time step 0, the population of the prey is 1000 and the predators’ population is 20; at time step 1, the population of the prey is 950 and the predators’ is 19; and at time step 2, the prey population 900 and the predators’ is 18, the first entries in the vector would look

0

1000

20

1

950

19

2

900

18

As part of your implementation of the simulation, you will be required to create two new classes, one called Predator and one called Prey. You are given the header file for Prey.h. You are given a ‘skelton’ header file for Predator.h. You need to create definitions for the mutators/accessors in Predator.h. You need to implement all the mutators and accessors in these classes (the ‘.cpp’ files). Your main function is defined in a file called PredatorPrey.cpp. Add the appropriate code where you see:

/* Your solution goes here */

Sample Output

For your program to work properly, it must execute using the following syntax:

PredatorPrey [input file] [output file]
 
where
·   ‘input file’ is any file in the correct format, including the provided sim.dat file
·   ‘output file’ is where the graphs will be written to
 
For example, the project should execute without errors with these arguments:
 
PredatorPrey sim.dat sim.out
 
For comparison, a graph for the simulations listed above is provided in the file PredPrey.txt.  
 

Please Remember

Name your project PredPrey.

You are given the ‘complete’ header file, Prey.h. There are no modifications necessary to this file.

You are given the ‘incomplete’ header file, Predator.h. There ARE modifications necessary to this file.

You need to implement the class files Prey.cpp and Predator.cpp.

You are given the ‘incomplete’ ‘main’ program, PredatorPrey.cpp. There ARE modifications necessary to this file.

Read the comments for clues about what is required.

Both classes you will create have a mutator named ‘getNextPopulation’. If the ‘next’ population goes below 0 (zero), this mutator shall return 0 (zero).

If your code is working correctly, your output should be identical to PredPrey.txt

To learn about command line arguments and how to use them in Visual Studio, review the video titled ‘CommandLineArguements’

Extra Credit

You can receive 10 points of extra credit by modifying the code as described below:

The ‘printHeader’ function in ‘PredatorPrey.cpp’ prints the scale of the ‘x’ and ‘y’ values.  One possible outcome is shown below:
 
Y scale: 1 row = 100 animals (or less).
X scale: 1 col = 1 generations (or less).
 
It is improper English to say ‘1 generations’.  To get the full amount of extra credit you will need to modify the code to print the word ‘generation’ if 1 column = 1 generation (or less) and to print the word ‘animal’ if 1 row = 1 animal (or less).

Required Files

All the files should be well documented, to include file headers and headers for each function, as well as appropriate in­line comments.

Submit the following files:

PredatorPrey.cpp

Prey.cpp

Predator.h

Predator.cpp

PREDATORYPREY.CPP
//
// PredatorPrey.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include "Predator.h"
#include "Prey.h"

using namespace std;


/*
Function Name:                  
                                                printHeader
Function Description:   
                                                print standard header to screen and output file prior to graph
inputs:
                                                outfile         - output file stream
                                                simNum          - simulation #
                                                preyStart       - # of prey at start of simulation
                                                preyEnd         - # of prey at end of simulation
                                                preyMax         - max # of pry in simulation
                                                predStart       - # of pred at start of simulation
                                                predEnd         - # of pred at end of simulation
                                                predMax         - max # of predators in simulation
                                                genNum          - # of generations in simulation
                                                xScale          - scale of X axis
                                                yScale          - scale of Y axis
returns:
                                                None
Notes/Assumptions:
                                                This routine should only be called from printGraph
*/
void printHeader(ofstream& outfile, int simNum, int preyStart, int preyEnd, int preyMax,
        int predStart, int predEnd, int predMax, int genNum, int xScale, int yScale)
{
        /*
        Print header to screen...
        */
        cout << endl << "Simulation: " << simNum << endl;
        cout << "Prey start count     = " << preyStart << endl;
        cout << "Predator start count = " << predStart << endl;
        cout << "Prey end count       = " << preyEnd << endl;
        cout << "Predator end count   = " << predEnd << endl;
        cout << "Max Prey count       = " << preyMax << endl;
        cout << "Max Predator count   = " << predMax << endl;
        cout << "# of Generations     = " << genNum << endl;
        cout << "Y scale: 1 row = " << yScale << " animals (or less)." << endl;
        cout << "X scale: 1 col = " << xScale << " generations (or less)." << endl << endl;
        /*
        Now print header to file, also...
        */
        outfile << endl << "Simulation: " << simNum << endl;
        outfile << "Prey start count     = " << preyStart << endl;
        outfile << "Predator start count = " << predStart << endl;
        outfile << "Prey end count       = " << preyEnd << endl;
        outfile << "Predator end count   = " << predEnd << endl;
        outfile << "Max Prey count       = " << preyMax << endl;
        outfile << "Max Predator count   = " << predMax << endl;
        outfile << "# of Generations     = " << genNum << endl << endl;
        outfile << "Y scale: 1 row = " << yScale << " animals (or less)." << endl;
        outfile << "X scale: 1 col = " << xScale << " generations (or less)." << endl;
        outfile << "Predator = o, Prey = *" << endl << endl;
        return;
}
/*
Function Name:
                                                printGraph 
Function Description:
                                                prints are crude graph representing the predator and 
                                                prey populations until one species dies off...
inputs:
                                                outfile   - output file stream
                                                simNumber - simulation number
                                                data      - vector of ints (contains population data at time 'period' -
                                                                        period, prey population, predator population)
                                                maxvalue  - greater value on prey or predator population
                                                period    - time period (max generation)
returns:
                                                none.
Notes/Assumptions:
                                                The graph will only show a close approximation of the animal populations.
                                                However, this is enough to display the trend.
*/
void printGraph(ofstream& outfile, int simNumber, vector<int> data, int maxValue, int period)
{
        const int GRAPH_SIZE = 101;                     
        bool toScreen = false;                          // set to true if you want graphs to go to screen; otherwise, false
        /*
        We will scale the x,y graph based on a size of 100 X 100.  This means we need to look at maxValue
        and periods.  We will scale according to the code below.

        Scale the y axis by using the max population
        */
        int scalePop = 1;
        if (maxValue <= 0)
                scalePop = 1;
        else if (maxValue > 0 && maxValue <= 100)
                scalePop = 1;
        else if (maxValue > 100 && maxValue <= 1000)
                scalePop = 10;
        else if (maxValue > 1000 && maxValue <= 10000)
                scalePop = 100;
        else if (maxValue > 10000 && maxValue <= 100000)
                scalePop = 1000;
        /*
        We can limit the y axis height by looking at the max
        population
        */
        int maxY = maxValue / scalePop + 5;
        if (maxY > GRAPH_SIZE)
                maxY = GRAPH_SIZE - 1;          // avoids out of bounds errors (see loops, below)
        /*
        Scale the x axis by using the (# of) periods
        */
        int scalePeriod = 1;
        if (period <= 0)
                scalePeriod = 1;
        else if (period > 0 && period <= 100)
                scalePeriod = 1;
        else if (period > 100 && period <= 1000)
                scalePeriod = 10;
        else if (period > 1000 && period <= 10000)
                scalePeriod = 100;
        else if (period > 10000 && period <= 100000)
                scalePeriod = 1000;
        /*
        We can limit the x axis length by looking at the max
        number of periods
        */
        int maxX = period / scalePeriod + 5;
        if (maxX > GRAPH_SIZE)
                maxX = GRAPH_SIZE - 1;          // avoids out of bounds errors (see loops, below)

        vector <vector<int>> graph(GRAPH_SIZE, vector<int>(GRAPH_SIZE)); // 100X100 array
        //
        // zero out the graph data - a zero means nothing will be printed.
        //
        for (int i = 0; i < GRAPH_SIZE; i++)
        {
                for (int j = 0; j < GRAPH_SIZE; j++)
                {
                        graph[i][j] = 0;
                }
        }
        //
        // each triplet of generation is stored in the graph vector to be output by this routine.
        // so, for every period, generation (X axis); we store a mark (for pred or prey) in a 
        // row (Y axis) - this mark is scaled by a factor of 10.
        //
        // since the data is stored as a triplet in the vector; go by 3's
        //
        int maxPreyCount = 0;
        int maxPredCount = 0;
        int thisPreyCount = 0;
        int thisPredCount = 0;
        for (unsigned int i = 0; i < data.size(); i = i + 3)
        {
                thisPreyCount = data[i + 1];
                thisPredCount = data[i + 2];
                if (thisPredCount > maxPredCount)
                        maxPredCount = thisPredCount;
                if (thisPreyCount > maxPreyCount)
                        maxPreyCount = thisPreyCount;
                //
                // graph[period][predator_population] = 1;
                // graph[period][prey_population] = 2;
                // graph[period][all_others] = 0; remember 0 == nothing is printed
                //
                graph[data[i] / scalePeriod][thisPreyCount / scalePop] = 1; // indicates prey  
                graph[data[i] / scalePeriod][thisPredCount / scalePop] = 2; // indicates predator
        }
        /*
        Print out a nice header
                        data[1] and data[2] are the initial populations of the prey and predator
                        data[period *3 + 1] and data[period * 3 + 2] are the ending populations
        */
        printHeader(outfile, simNumber, data[1], data[period * 3 + 1],
                maxPreyCount, data[2], data[period * 3 + 2], maxPredCount,
                period + 1, scalePeriod, scalePop);
        /*
        Data is stored in the 2D array for the populations for each generation, scaled
        accordingly. Now, construct the graph so that it correctly represents the data
        */
        int k;
        //
        // print vertical axis with labels and data points
        //
        for (int i = maxY; i >= 0; i--)
        {
                //
                // had to 'tweek' the vertcal axis creation (setw)
                //
                if ((i % 10 == 0) || (i % 5 == 0))
                {
                        if (toScreen)
                        {
                                cout << setw(10) << i * scalePop;
                                cout << setw(2) << "|";
                        }
                        outfile << setw(10) << i * scalePop;
                        outfile << setw(2) << "|";
                }
                else
                {
                        if (toScreen)
                        {
                                cout << setw(12) << "|";
                        }
                        outfile << setw(12) << "|";
                }
                //
                // we have started to print out a row (with a # which represents a population amount)
                // in the above code.
                // now, print out the symbols that represent predator / prey for this population amount
                // (Note that most rows will not print out data that is predator or prey).
                //
                for (int j = 0; j < maxX; j++)
                {
                        k = graph[j][i];
                        // if prey...
                        if (k == 1)
                        {
                                if (toScreen)
                                        cout << "*";
                                outfile << "*";
                        }
                        // else if predator...
                        else if (k == 2)
                        {
                                if (toScreen)
                                        cout << "o";
                                outfile  << "o";
                        }
                        // else print a blank space (nothing)
                        else
                        {
                                if (toScreen)
                                        cout  << " ";
                                outfile  << " ";
                        }
                }
                if (toScreen)
                        cout << endl << endl;
                outfile << endl; // << endl;
        }
        //
        // print 'X' axis info
        //
        if (toScreen)
                cout << setw(12) << " ";
        outfile << setw(12) << " ";
        for (int i = 0; i < maxX; i++)
        {
                if (toScreen)
                        cout << "-";
                outfile  << "-";
        }
        if (toScreen)
        {
                cout << endl; cout << setw(12) << " "; cout << "0";
        }
        outfile << endl; outfile << setw(12) << " "; outfile << "0";
        for (int i = 0; i < maxX; i++)
        {
                if (i % 10 == 0 && i > 0)
                {
                        if (toScreen)
                                cout << setw(10) << i * scalePeriod;
                        outfile << setw(10) << i * scalePeriod;
                }
        }
        if (toScreen)
                cout << endl;
        outfile << endl;
}

int main(int argc, char* argv[])
{
        /* 
        One variable has been created.  You will need to create many more variables.
        You should declare / initialize your variables here...
        */
        int simulationNumber;                                           // store the simulation #; i.e., each row is 1 sim.
        
        
        /*
        The following code reads from the command line...
        Watch the video on CommandLineArguements for more information.
        */
        if (argc != 3) {
                cout << "Incorrect number of arguments:: argv[0] [input_file] [output_file]" << endl;
                return 0;
        }

        /*
        Get input and output file
        */
        string inputFile = argv[1];
        string outputFile = argv[2];
        ifstream infile(inputFile);                     // input file stream
        ofstream outfile(outputFile);           // output file stream
        //
        // verify input and output file can be opened...
        //
        if (infile.fail())
        {
                cout << "Cannot read from input file: " << inputFile << ". Exiting..." << endl;
                return 1;
        }
        if (outfile.fail())
        {
                cout << "Cannot open output file: " << outputFile << ". Exiting..." << endl;
                infile.close();
                return 1;
        }
        cout << "Predator Vs Prey" << endl << endl;
        outfile << "Predator Vs Prey" << endl << endl;
        cout << "\tOutput file == " << outputFile << endl << endl;

        simulationNumber = 0; // keep track of the # of simulations
        //
        // read the input file until there are no more data, each row should have 6 elements
        // if not, the results will be unpredictable.
        //
        while (infile >> preyBirthRate >> preyPredationRate >> predDeathRate >> predPopRate >> initPreyPop >> initPredPop)
        {
                /* Your solution goes here  */
        }
        return 0;
}
PREDATORY VS PREY TEXT (OUTPUT)
Predator Vs Prey


Simulation: 1
Prey start count     = 980
Predator start count = 17
Prey end count       = 0
Predator end count   = 91
Max Prey count       = 7495
Max Predator count   = 103
# of Generations     = 52

Y scale: 1 row = 100 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

           |                                                        
           |                                                        
           |                                                        
           |                                                        
      7500 |                                                        
           |                      **                                
           |                     *                                  
           |                                                        
           |                        *                               
      7000 |                    *                                   
           |                                                        
           |                                                        
           |                         *                              
           |                   *                                    
      6500 |                                                        
           |                                                        
           |                                                        
           |                  *                                     
           |                                                        
      6000 |                          *                             
           |                                                        
           |                                                        
           |                 *                                      
           |                                                        
      5500 |                                                        
           |                                                        
           |                *                                       
           |                           *                            
           |                                                        
      5000 |                                                        
           |                                                        
           |               *                                        
           |                                                        
           |                                                        
      4500 |                                                        
           |              *                                         
           |                            *                           
           |                                                        
           |                                                        
      4000 |                                                        
           |             *                                          
           |                                                        
           |                                                        
           |            *                                           
      3500 |                                                        
           |                             *                          
           |                                                        
           |           *                                            
           |                                                        
      3000 |                                                        
           |                                                        
           |          *                                             
           |                                                        
           |         *                    *                         
      2500 |                                                        
           |                                                        
           |        *                                               
           |                                                        
           |                                                        
      2000 |       *                                                
           |                               *                        
           |      *                                                 
           |                                                        
           |     *                                                  
      1500 |    *                                                   
           |                                *                       
           |   *                                                    
           |  *                                                     
           |                                                        
      1000 | *                                                      
           |*                                *                      
           |                                                        
           |                                                        
           |                                  *                     
       500 |                                                        
           |                                   *                    
           |                                    *                   
           |                                     *                  
           |                                 oooooooooo             
         0 |ooooooooooooooooooooooooooooooooo       ***ooooooooo    
            --------------------------------------------------------
            0        10        20        30        40        50

Simulation: 2
Prey start count     = 2000
Predator start count = 50
Prey end count       = 0
Predator end count   = 101
Max Prey count       = 2000
Max Predator count   = 101
# of Generations     = 3

Y scale: 1 row = 100 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

      2500 |       
           |       
           |       
           |       
           |       
      2000 |*      
           |       
           |       
           |       
           |       
      1500 |       
           |       
           |       
           |       
           |       
      1000 | *     
           |       
           |       
           |       
           |       
       500 |       
           |       
           |       
           |       
           |  o    
         0 |oo*    
            -------
            0

Simulation: 3
Prey start count     = 500
Predator start count = 5
Prey end count       = 0
Predator end count   = 25
Max Prey count       = 12297
Max Predator count   = 43
# of Generations     = 101

Y scale: 1 row = 1000 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

           |                                                                                                    
           |                                                                                                    
     15000 |                                                                                                    
           |                                                                                                    
           |                                                                                                    
           |                                                                   ***                              
           |                                                                ***   **                            
     10000 |                                                              **        *                           
           |                                                            **                                      
           |                                                          **             *                          
           |                                                       ***                *                         
           |                                                    ***                                             
      5000 |                                                ****                       *                        
           |                                           *****                            *                       
           |                                      *****                                  *                      
           |                             *********                                        *                     
           |               **************                                                  *                    
         0 |oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
            ----------------------------------------------------------------------------------------------------
            0        10        20        30        40        50        60        70        80        90

Simulation: 4
Prey start count     = 500
Predator start count = 50
Prey end count       = 0
Predator end count   = 38
Max Prey count       = 500
Max Predator count   = 50
# of Generations     = 14

Y scale: 1 row = 10 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

       550 |                  
           |                  
           |                  
           |                  
           |                  
       500 |*                 
           |                  
           |                  
           |                  
           |                  
       450 |                  
           |                  
           |                  
           |                  
           |                  
       400 |                  
           |                  
           |                  
           |                  
           |                  
       350 |                  
           |                  
           |                  
           |                  
           |                  
       300 | *                
           |                  
           |                  
           |                  
           |                  
       250 |                  
           |                  
           |                  
           |                  
           |                  
       200 |                  
           |                  
           |  *               
           |                  
           |                  
       150 |                  
           |                  
           |                  
           |                  
           |                  
       100 |   *              
           |                  
           |                  
           |                  
           |    *             
        50 |oo                
           |  oooooooooo      
           |            oo    
           |      *           
           |       **         
         0 |         *****    
            ------------------
            0        10

Simulation: 5
Prey start count     = 980
Predator start count = 5
Prey end count       = 52
Predator end count   = 0
Max Prey count       = 17071
Max Predator count   = 16
# of Generations     = 56

Y scale: 1 row = 1000 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

           |                                                            
           |                                                            
     20000 |                                                            
           |                                                            
           |                                                            
           |                               **                           
           |                              *  *                          
     15000 |                                                            
           |                             *                              
           |                            *     *                         
           |                           *                                
           |                          *                                 
     10000 |                         *         *                        
           |                        *                                   
           |                       *                                    
           |                      *                                     
           |                    **              *                       
      5000 |                  **                                        
           |                **                                          
           |             ***                     *                      
           |        *****                                               
           | *******                              *                     
         0 |oooooooooooooooooooooooooooooooooooooooooooooooooooooooo    
            ------------------------------------------------------------
            0        10        20        30        40        50

Simulation: 6
Prey start count     = 1000
Predator start count = 20
Prey end count       = 406
Predator end count   = 0
Max Prey count       = 1000
Max Predator count   = 20
# of Generations     = 28

Y scale: 1 row = 10 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

      1000 |*                               
           |                                
           |                                
           |                                
           |                                
       950 |                                
           |                                
           |                                
           |                                
           |                                
       900 | *                              
           |                                
           |                                
           |                                
           |                                
       850 |                                
           |                                
           |                                
           |                                
           |  *                             
       800 |                                
           |                                
           |                                
           |                                
           |                                
       750 |                                
           |                                
           |                                
           |   *                            
           |                                
       700 |                                
           |                                
           |                                
           |                                
           |                                
       650 |    *                           
           |                                
           |                                
           |                                
           |                                
       600 |                                
           |     *                          
           |                                
           |                                
           |                                
       550 |                                
           |                                
           |      *                         
           |                                
           |                                
       500 |                                
           |                                
           |                                
           |       *                        
           |                                
       450 |                                
           |                                
           |                                
           |        *                       
           |                                
       400 |                           *    
           |         *                      
           |                                
           |                          *     
           |                                
       350 |          *                     
           |                         *      
           |           *                    
           |                        *       
           |            *                   
       300 |                       *        
           |             *        *         
           |              *      *          
           |               *    *           
           |                ****            
       250 |                                
           |                                
           |                                
           |                                
           |                                
       200 |                                
           |                                
           |                                
           |                                
           |                                
       150 |                                
           |                                
           |                                
           |                                
           |                                
       100 |                                
           |                                
           |                                
           |                                
           |                                
        50 |                                
           |                                
           |                                
           |oooooooo                        
           |        oooooooooo              
         0 |                  oooooooooo    
            --------------------------------
            0        10        20        30

Simulation: 7
Prey start count     = 300
Predator start count = 25
Prey end count       = 177
Predator end count   = 0
Max Prey count       = 300
Max Predator count   = 26
# of Generations     = 30

Y scale: 1 row = 10 animals (or less).
X scale: 1 col = 1 generations (or less).
Predator = o, Prey = *

       350 |                                  
           |                                  
           |                                  
           |                                  
           |                                  
       300 |*                                 
           |                                  
           |                                  
           |                                  
           |                                  
       250 |                                  
           | *                                
           |                                  
           |                                  
           |                                  
       200 |                                  
           |                                  
           |  *                               
           |                             *    
           |                                  
       150 |                                  
           |   *                              
           |                            *     
           |                                  
           |    *                      *      
       100 |                                  
           |     *                    *       
           |                                  
           |      *                  *        
           |       *                *         
        50 |        *              *          
           |         **          **           
           |           **********             
           |oooooooooo                        
           |          oooooooooo              
         0 |                    oooooooooo    
            ----------------------------------
            0        10        20        30
PREDATOR.CPP
class Predator
{
public:
        /*
                Default constructor, sets all data members to 0/null.
        */
        Predator();
        /*
                Constructor to set data members to initial values.
        */
        /* Your solution goes here  */
        /*
                Mutators to:
                        setPopulation
                        setDeathRate
                        setPopulationRate
                to specific values
        */

        /* Your solution goes here  */
        
        
        /*
        Accessors to:
                getCurrentPopulation
                getNextGenPopulation
                getCurrentDeathRate
                getCurrentPopulationRate
        */

        /* Your solution goes here  */
        
        
        /*
        Destructor
        */
        ~Predator();

private:
        int currentPopulation;
        double deathRate;
        double populationRate;
};

PREY.CPP

class Prey
{
public:
        /*
                Set the default constructor to zero all values.
        */
        Prey();
        /*
                Constructor to set all data members to specific values. 
        */
        Prey(int population, double birthRate, double predRate);
        /*
                Mutators to:
                        setPopulation
                        setBirthRate
                        setPredRate
                to specific values
        */
        void setPopulation(int population);
        void setBirthRate(double rate);
        void setPredRate(double rate);
        /*
                Accessors to:
                        getCurrentPopulation
                        getNextGenPopulation
                        getCurrentBirthRate
                        getCurrentPredation
        */
        int getCurrentPopulation() const;
        int getNextGenPopulation(int predPopulation) const;
        double getCurrentBirthRate() const;
        double getCurrentPredation() const;
        /*
                Destructor
        */
        ~Prey();

private:
        int currentPopulation;
        double birthRate;
        double predRate;
};

SIM.DAT

0.2 0.005 0.003 0.00002 980 17
0.5 0.02 0.01 0.0003 2000 50
0.1 0.01 0.01 0.00002 500 5
0.1 0.01 0.01 0.00002 500 50
0.3 0.05 0.02 0.00002 980 5
0.1 0.01 0.01 0.00002 1000 20
0.3 0.02 0.06 0.0004 300 25

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

void printGrid(char grid[][20], int w, int h);

int main() {

    // Create a 20 by 20 grid array of chars

    char grid[20][20];

    // Now loop through each row...

    for (int i = 0; i < 20; i++) {

        // ... then each column and set each space to '.'

        for (int j = 0; j < 20; j++) {

            grid[i][j] = '.';

        }

    }

  

    // Print the grid of 20 by 20 dots

    printGrid(grid,20,20);

  

    return 0;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

   std::cin.get();

}

  

  

// Call this function to print your grid. w = width, h = height

void printGrid(char grid[][20], int w, int h) {

    for (int i = 0; i < w; i++) {

        for (int j = 0; j < h; j++) {

            cout << grid[i][j] << " ";

        }

        cout << endl;

    }

}

Add a comment
Know the answer?
Add Answer to:
Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After...

    Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to:  implement member functions  convert a member function into a standalone function  convert a standalone function into a member function  call member functions  implement constructors  use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...

  • Overload the output stream operator << and the assignment operator =. Any time cout << task;...

    Overload the output stream operator << and the assignment operator =. Any time cout << task; is used the code should output a task, any time task1 = task2 is copied there must be a deep copy if there are pointers. Use the operators in the following code as suggested above. DO NOT simply overload them! Do so on the following code: //Project 4 Main function #include "functions.h" #include <stdlib.h> #include <stdio.h> //main int main(){    TaskList column("tasks.txt");    char...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  •    moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring>...

       moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • I'm not getting out put what should I do I have 3 text files which is...

    I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT