16.0k views
5 votes
Write this in a C# code

This is another problem solving project. Since I want you to focus on the algorithm part of the problem, you should write this as a Console Program. Once you have figured out the steps required to solve the problem, writing the code should be pretty easy. You should be pretty familiar with doing these steps by now:

Create an Activity Diagram and Pseudo-code.
Then use the activity diagram and the pseudo-code as the basis for writing the code that you submit.
Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one pair of adult rabbits (a male and a female). At the end of each month, a pair of rabbits produces one pair of offspring (a male and a female). However, these new offspring will not be able to reproduce until they are a month old, and won't have babies of their own until the following month. To illustrate this, consider the first two months:

At the beginning of month one, the scientist just has the original pair of adult rabbits. The table for month one will look something like

Month Adults Babies Total
1 1 0 1

At the end of month one this pair of adults produces one pair of offspring. Thus, at the beginning of month two the table will look like this:

Month Adults Babies Total
1 1 0 1
2 1 1 2

At the end of month two the adults have another pair of baby rabbits. The first pair of babies, born at the end of last month are not old enough to have babies yet, but we will categorize them as adults. So, at the beginning of month three the table looks like this:

Month Adults Babies Total
1 1 0 1
2 1 1 2
3 2 1 3

The scientist has 500 cages in which to hold her rabbits. Each cage holds one pair of rabbits. Assuming that no rabbits ever die, when will she run out of cages?

Your program must do the following:

Print out your student information.
Print a table that contains the following information for the beginning of each month. Stop printing when you run out of cages.
The number of months that have passed.
The number adult rabbits (those over 1 month old).
The number of baby rabbits produced this month.
The total number of rabbit pairs in the lab.
Calculate and print out how many months it will take until the number of rabbits exceeds the number of available cages

User LordGrim
by
8.2k points

1 Answer

3 votes

Below is a simple C# console program that implements the described rabbit reproduction scenario.

using System;

class Program

{

static void Main()

{

// Print student information

Console.WriteLine("Student Information:");

Console.WriteLine("Name: [Your Name]");

Console.WriteLine("ID: [Your ID]\\");

// Initialize variables

int months = 1;

int adults = 1;

int babies = 0;

int totalPairs = 1;

int cages = 500;

// Print table header

Console.WriteLine("Month\tAdults\tBabies\tTotal");

// Loop until the number of rabbit pairs exceeds the number of cages

while (totalPairs <= cages)

{

// Print current month's information

Console.WriteLine($"{months}\t{adults}\t{babies}\t{totalPairs}");

// Calculate new adults and babies for the next month

int newAdults = babies; // Babies from the previous month become adults

int newBabies = adults; // Each adult produces one pair of babies

// Print the result

Console.WriteLine($"\\It will take {months - 1} months until the number of rabbits exceeds the number of available cages.");

User Mwilkerson
by
8.0k points