Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A brave hero is preparing for battle, but this time, s/he will be fighting a par

ID: 3568832 • Letter: A

Question

A brave hero is preparing for battle, but this time, s/he will be fighting a particularly tricky enemy: a slime. What makes the slime so tricky? Every time the hero attacks the slime, it might split into TWO slimes, and the hero then has to kill BOTH slimes - each of which might split AGAIN when s/he tries to kill them! Your task for this question is to write a program that will determine how many slimes the hero will have to kill on average using the power of recursion. Getting Started Once again, this question will make use of random numbers, so you need to make the following preparations: Include the lines "#include " and "#include " at the top of your program. Make sure the first line of your main() function is "srand(time(NULL));"

Write a recursive function called killSlime(int killNumber). The function returns the total number of slimes that the hero actually killed while trying to kill the initial slime (note: a slime that "splits" has *not* been killed!). The function has a single integer parameter called 'killNumber.' This number indicates how hard the slime is to kill, and is used in the procedure below. To determine whether the hero kills a slime, use the following procedure. Generate a random number from 1 to 10 (NOT from 0 to 9!) using the rand() function. If that number is greater than or equal to the slime's killNumber, the slime is dead. Hooray! Otherwise, the slime is not dead (i.e. no slime has been killed) and it splits into two slimes. The hero now must kill BOTH these slimes, which means you must (recursively) determine how many slimes each of these two slimes split into in the course of killing them. Fortunately, the two slimes are weaker than the one that they split from their killNumber is reduced by one

In your main() function, use a loop to call killSlime() 1000 times. Then, calculate and display the following values: The average number of slimes the hero had to kill. The maximum number of slimes the hero had to kill. The output for your program should look something like this A slime appears! To battle, for great justice!! On average, with kill number 10, the hero must kill ???? slimes! The maximum number of slimes the hero had to kill was: ?? Task 3 In a document called a9q2_results.txt, answer the following questions: With an initial killNumber of 10, how many slimes must the hero kill on average? What is the maximum number of slimes killed? With an initial killNumber of 7, how many slimes must the hero kill on average? What is the maximum number of slimes killed?

Explanation / Answer

#include <cstdlib>
#include <time.h>
#include<iostream>
using namespace std;

int killSlime(int killNumber)
{
int slimeKill = (rand() % 10) + 1 ; //random number generate from 1-10
  
if(slimeKill >= killNumber) //base condition if the random number generated is >= killNUmber then it gets killed, so it returns 1
return 1;
else
return killSlime(killNumber-1) + killSlime(killNumber-1) ; // else it divides int otwo with one less killNumber than before, so recursion is done twice
  
}
int main(int argc, char** argv) {
  
srand(time(0)); //seed data
  
  
int total = 0, max = 0;
for(int i=0; i<1000; i++) //loop to call the recursive function 1000 times
{
int kill = killSlime(10);
  
if(max < kill)
max = kill; //calculates max
  
total += kill; //calculates total for average
}
  
float average = (float)(total/1000); //finda average
  
cout<<"A slime appears! To battle, for great justice!! ";
cout<<"On average, with kill number 10, the hero must kill "<<average<<" slimes! ";
cout<<"The maximum number of slimes the hero had to kill was: "<<max;

return 0;
}

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

OUTPUT

A slime appears! To battle, for great justice!!
On average, with kill number 10, the hero must kill 14 slimes!
The maximum number of slimes the hero had to kill was: 46