C program- Please Help! Once upon a time, there was a child who couldnt sleep, s
ID: 666481 • Letter: C
Question
C program- Please Help!
Once upon a time, there was a child who couldnt sleep,
so the child's mother told her a story about
a mouse who couldnt sleep,
so the mouse's mother told her a story about
a turtle who couldnt sleep,
so the turtle's mother told her a story about
a frog who couldnt sleep,
so the frog's mother told her a story about
a bear who went to sleep.
Then the frog went to sleep.
Then the turtle went to sleep.
Then the mouse went to sleep.
Then the child went to sleep.
You need to code the story() function. Here is the rest of the program:
#include <stdio.h>
#include <stdlib.h>
//PUT CODE HERE
int main()
{
char* animals[] = {"bear", "frog", "turtle", "mouse", "child"};
int start = 4;
printf("Once upon a time, there was ");
story(animals, start);
return 0;
}
Need to use Recursion ONLY please, thanks!
Explanation / Answer
This is the solution :
}
Hope it helps
#include <stdio.h> #include <stdlib.h> #include <time.h> char* chooseAnAnimal(char **animals, int size); void printStoryR (char **animals, char *animal, int size, int indent); void printIndent(int indent); void printStory(char **animals,int size); int main(){ char *animals[] = {"frog", "cat", "dog", "horse", "snake", "eagle", "bear", "weasel"}; srand(time(NULL)); int i; int j = 0; printStory((char **)animals, 8); /* while(j < 9){ */ /* i = rand()%8; */ /* //temp = i; */ /* printf("%s, %d ", animals[i], i); */ /* j++; */ /* } */ } // end of main // randomly choose an animal from the array char* chooseAnAnimal(char **animals, int size) { int i = rand() % size; char *temp; // swtich the first animal and the chosen animal in the array. temp = animals[i]; animals[i] = animals[0]; animals[0] = temp; return animals[0]; } void printStoryR (char **animals, char *animal, int size, int indent) { char *a = chooseAnAnimal(animals, size); printIndent(indent); // print the indent printf("A %s couldn't sleep, so her mothers told a story about a little %s ", animal, a); // print the first half statement in the current level if (size > 1) { // check the termination condition for recursive calling. printStoryR(animals + 1, a, size - 1, indent + 1); } else { printIndent(indent + 1); printf("... who fell asleep "); } printIndent(indent); printf("... and the %s fell asleep. ", a); // print the second half statement of the current level } // print indentation based on the level. void printIndent(int indent) { int i; for (i = 0; i < indent; i++) { printf(" "); } } // print the outter most statement about child and make a call to recursive function call. void printStory(char **animals,int size){ char *a = chooseAnAnimal(animals, size); printf("A child couldn't sleep, so her mothers told a story about a little %s ", a); printStoryR(animals, a, size, 1); printf("... and the child fell asleep. ");}
Hope it helps
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.