Lab Assignment # 3, CSE 1320-501, Fall 2003

 

Topic objectives:                              Recursion

                              Multi-dimensional arrays

                              File I/O

Due date:                                November 11, 2003

                             

Your friend Chris Green was so pleased with the last program you wrote that there’s a new task for you.  The Green Shop has been a huge success so now Chris is going to start breeding different types of animals to stock in the shop.  Chris wants you to write a program to predict how many animals there will be after a given number of generations.  Each type of animal will have different initial populations, different birth rates, different average life spans, and different breeding characteristics.  Chris has a recursive algorithm to generate the next generation of animals from the current generation of animals given the four pieces of information above.  You get to the write the program that Chris will use to make breeding plans.

 

Your task has several parts.  First, Chris wants to make a table of the animals of interest along with the four pieces of data given above.  All of this data will be entered first from a file.  Each line in the file will be in the following format:

 

Animal_name    Init_females   Init_males   Births_per_female   Life_span_in_years   Percent_female_babies

 

The Animal_name will be a single string, i.e. “green_snake” (all one word with the underscore) not “green snake” (two separate words).  The Init_females and Init_males are integers which indicate the starting number of adult females and males in Chris’s breeding program.  Births, Life_span and Percent_female are floating point numbers.   Births_per_female is the average number of babies an adult female has in each pregnancy.  The program assumes one pregnancy per generation.  Life_span is the average number of years that the animal lives (remember, Chris has a small shop and small animals so this number may be fractions of a year).  Percent tells what percent of the babies born will be female  (represented as a value between 0 and 1, ex. 25% is .25, 82% is .82) - this number with Births number and the total number of breeding females is used to figure out the number of females in the newly born generation.   The last line of the file will have “GGG” for the Animal and that will indicate that there is no more data in the file.  There will be no more than 25 animals in the file.

 

The data read from the file should be stored in two arrays.  The first array should be an array to hold the animal names in the form of a pointer to the string of the animal’s name.  The rest of the data should be stored in a multi-dimensional array with one row for each animal and a column for each piece of data to be stored about the animal, with the first column being the initial number of females, the second column being the initial number of males, the third Births, the fourth Life_span, etc.  Thus the name for animal [x] is in the name array and the breeding information for animal [x] is in the multi-dimensional data array.  You may want to store other data in the array that you create in the program so keep this in mind when designing your array.

 

After you have read in all the animals and stored all their data, you now need to run the breeding program part.  For each animal in the array you must do the following:

 

1)     Print the animal’s name and the data that the user entered for the animal.

2)     Ask the user how many generations of breeding (g) they would like to predict population for.  The user’s answer should be a non-negative integer value.

3)     To calculate the number of females in generation g the algorithm is:

Fem (g) = Initial_females         for g = 0

Fem (g) = Fem(g - 1) *  (1 + Births * Percent)         for g > 0

4)     To calculate the number of males in generation g the algorithm is:

Mal (g) = Initial_males         for g = 0

Mal (g) = Mal(g - 1)   + Fem (g - 1) *  (1 + Births * (1 - Percent))         for g > 0

5)     If the user asks for the population after g generations, print the number of females and males and the sum of the current number of males and females and then return the sum as the population.

6)     After the user has had the chance to predict once for each animal, then ask them if they wish to stop or to make new breeding population predictions for the existing animals.

 

 

Implementation requirements:

The program must use the following data structures:

An array of pointers to the strings containing the names of the animals

A multi-dimensional array of data for each animal

An input file containing the names and data for all the animals

 

The program must use the following control structures:

A loop to read the lines of input from the file and store them in the arrays

A loop to go through the list of animals and make population predictions

Two recursive functions (one for females and one for males) to calculate population

 

The program should perform the following actions in the given order:

Declare and initialize the variables and data structures

In a loop (until the last line of input data is seen)

Read in the data from the file

Allocate space for a string for the animals name and copy the input to that string

Store the pointers and data in the appropriate arrays

In a loop (for each animal)

Print the appropriate outputs

Ask the user the number of generations to calculate

Call the recursive functions to calculate the data

After processing each animal once, let the user repeat the population calculations until the user indicates that they are finished.

 

As in the previous labs, the program should have a program header which gives, at least, your name, the number of the lab assignment, your class and section, the assignment date, the due date, and a description of the program.  If multiple program files are used, each file should contain a similar header. 

Each programmer-defined function, i.e. each function you write, should have a function header similar to those used in the examples in the textbook.  This header should include at least the function name, the purpose of the function, and its inputs and outputs.

 

This program must be run using the sample data file that will be given.  For testing run your program with the values below your own sample data as well.  The sample data set that you create must meet the guidelines given in the problem definition.

 

The program output must be recorded in a script file from OMEGA using the gcc compiler.  If you do not know how to create a script file, it is your responsibility to read the instructions on  my website and to  ask the TA or OIT how to use this function.  

 

Sample input values for use in testing:

 

         Animal_name        Init_female  Init_males Births  Life_span        Percent_female  

 

        Tree_frogs        10              10        40.0         2.0        .75

        Green_snakes                   12        8             40.0        4.5         .75

        Praying_mantis                40        15          10.0        0.25       .25