Question

The question in C. 5. [5 marks] Consider a special type of animal named Animal-X that...

The question in C.

5. [5 marks] Consider a special type of animal named Animal-X that reproduces following the rules below: • An Animal-X dies six months after it is born. That is, if an Animal-X is born in month 0, it will die in month 6. • An Animal-X gives birth to two Animal-Xes every two months until it dies. That is, if an Animal-X is born in month 0, it will reproduce in months 2 and 4. It does not reproduce in month 6 or beyond. • In each month, among all the Animal-Xes that have been alive for exactly three months, there is one that dies. That is, if there are n (n > 0) Animal-Xes that are born in month 0, one of them will die in month 3. Given an integer initial representing the number of Animal-Xes born in month 0, and an integer m representing a month, your task in this question is to write a function int calculate_animal_x(int initial, int m) that calculates and returns the number of Animal-Xes alive in month m. For example, if there is 1 Animal-X born in month 0 (initial = 1):
• In month 1, there is 1 Animal-X.

• In month 2, there are 3 Animal-Xes (2 are born by the initial Animal-X).

• In month 3, there are 2 Animal-Xes (the initial Animal-X dies).

• In month 4, there are 6 Animal-Xes (4 are born).

• In month 5, there are 5 Animal-Xes (1 of the Animal-Xes born in month 2 dies).

• In month 6, there are 15 Animal-Xes (10 are born).

• In month 7, there are 14 Animal-Xes (1 of the Animal-Xes born in month 4 dies).

• In month 8, there are 39 Animal-Xes (the remaining Animal-X born in month 2 dies; 26 are born).

• ...

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


//time complexity will be O(m)
int calculate_animal_x(int initial, int m)
{
//varibles a0-a5 store the number of animals
//with a age 0-5.
int a0 = initial;
int a1=0, a2=0, a3=0, a4 =0, a5=0;

//for every month we update the number of animals
//with particular ages
for(int i=1; i<=m; i++)
{
//age of animals increase by 1
a5 = a4;
a4 = a3;
a3 = a2;
a2 = a1;
a1 = a0;

//1 animal of age 3 month dies
if(a3!=0)a3--;

//new animals are born from a0 and a1
a0 = 2*(a2+a4);
}

//number of remaining animals
return a0+a1+a2+a3+a4+a5;
}

// I have provided the C code without main. Please, add a main function if needed

Add a comment
Know the answer?
Add Answer to:
The question in C. 5. [5 marks] Consider a special type of animal named Animal-X that...
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
  • 5. Mortality for two independent lives is modeled as a Markov chain wth four states: (1) both ali...

    5. Mortality for two independent lives is modeled as a Markov chain wth four states: (1) both alive (2) (x) alive (3) (y) alive (4) neither alive The transition probability matrix is 0.85 0.1 0.04 0.01 0 0.95 0 0.05 0 0 0.9 0.1 A 3-year term insurance has the following provisions withi- 0.05 (i) Premiums are payable at the beginning of the year while both are alive (ii) Benefits are paid at the first death. ii I (y) dies...

  • Task: Simulate the growth of a biological population. The Game of Life, invented by John H....

    Task: Simulate the growth of a biological population. The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. Our implementation of the game will use a 2-dimensional array with 12 rows and 12 columns. Each cell of the array will hold either a 1 (an organism is present) or a 0 (no organism there). You will calculate 4 generations of the organisms’ birth, survival, and death. An organism’s neighbors...

  • (C++ program) Can someone help me with this homework? Write a function named selectPositive that given...

    (C++ program) Can someone help me with this homework? Write a function named selectPositive that given a table of n x m integers, copies the elements that are greater than zero ( > 0 ) to a 1D array, column by column. For instance: int table[MAX_ROW][MAX_COL] = { {10, -2, -3, -4, 0, 85}, {-5, -6, 0, 80, -5, 10}, {90, 11, -1, 31, -3, -4}, {41, 51, 6, 71, -7, 19} }; int n = 4, m = 6;  ...

  • The integer-valued random variable X(t) denotes the number of individuals alive at time t in a...

    The integer-valued random variable X(t) denotes the number of individuals alive at time t in a simple birth process {X (t);t 2 0). A partial differential equation for II(s, t), the probability generating function of X (t), is 하 =-88(1-8) on Os Ot In Activity 4.1 of Book 4, you showed that this partial differential equation has the general solution Suppose that X(0), the number of individuals alive at time 0, is a random variable: X(0) has the negative binomial...

  • 5. Consider the following C code: int main() int x = 1; switch (x) case 1:...

    5. Consider the following C code: int main() int x = 1; switch (x) case 1: i=1; case 2: i=5; return 0; Based on this code, answer the following: (a) Convert the above C code to MIPS assembly. (b) The condition you are testing is met at Case 1. You do not want the Case 2 to be tested. Add appropriate instructions in MIPS for this implementation. (c) If int x is any integer other than 1 or 2, you...

  • Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

    Question 14; Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks) Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and...

  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

  • 1.The following statement gets an element from position 4 in an array named myArray: x =...

    1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...

  • Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1;...

    Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...

  • Please answer both questions thank you. Question 39 (1 point) Translate the following conditions to Java,...

    Please answer both questions thank you. Question 39 (1 point) Translate the following conditions to Java, where x is an integer and s is a string. 1. x >= 6 2. x < 0 s is "Day" 3. S = "Day" x is at least 6 4. X == -7 DODO x is at most 6 5. x = -7 x is - 7 6. s.equals("Day") 7. x <= 6 x is not 27 8. X >O x is positive...

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