Question

C++ Language 1. Why would you prefer to use recursion? (Multiple answers or one answer) a...

C++ Language

1. Why would you prefer to use recursion? (Multiple answers or one answer)

a

When the problem to be solved is naturally recursive, a recursive algorithm provides a clear and simple solution.

b

Since stack memory is faster than heap memory, and recursion uses stack memory, it is preferable to use recursion.

c

If a language, like LISP, or a subset of a language, like C++ templates, provides recursion but does not provide looping, then use recursion.

d

Since recursive functions call themselves, they avoid redundant code, and generate LESS code than functions that use looping.

2. What code below would use up all the memory on the heap (also known as the "free store")?

a

void function() { function(); }
function();

b

int i;
while (true) ++i;

c

while (true) new int;

d

ofstream out_file("big_file.txt");
while (true) {out_file<<"junk and more junk "};

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

1. Why would you prefer to use recursion? (Multiple answers or one answer)
- a: When the problem to be solved is naturally recursive, a recursive algorithm provides a clear and simple solution.
- b: Since stack memory is faster than heap memory, and recursion uses stack memory, it is preferable to use recursion.
- d: Since recursive functions call themselves, they avoid redundant code, and generate LESS code than functions that use looping.

2. What code below would use up all the memory on the heap (also known as the "free store")?
c: while (true) new int;

This would tke up all the memory on heap since we are allocating new space dynamically infinitely.

Add a comment
Know the answer?
Add Answer to:
C++ Language 1. Why would you prefer to use recursion? (Multiple answers or one answer) a...
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
  • C++ Language 1. Match the description / meaning with the C++ syntax.       -   ...

    C++ Language 1. Match the description / meaning with the C++ syntax.       -       A.       B.       C.       D.       E.    external class or method that can access private members       -       A.       B.       C.       D.       E.    scope resolution operator       -       A.       B.       C.       D.       E....

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

  • C++ Using Recursion We are going to create a (looping) menu that accesses functions that use...

    C++ Using Recursion We are going to create a (looping) menu that accesses functions that use recursion. The function headers and a description will be provided. You are responsible for defining the functions. Ensure that they each contain a base case they reach that doesn’t have a recursive function call, otherwise it’s an infinite loop! Functions must be implemented with recursion. int Factorial(int arg) Returns arg! (4! Is 4 * 3 * 2 = 24) Base case is Factorial(1) returning...

  • Call stack question! Long one... The call stack is part of main memory that is reserved...

    Call stack question! Long one... The call stack is part of main memory that is reserved for function calling. Like all r memory it is finite, so can be exhausted resulting in a stack overflow. Recursive functions allocate space on the stack for each recursive call: if there are many such recursive calls a stack overflow can result. The questions that follow ask you to investigate recursive functions and stack overflows. Note that when running programs in the Linux terminal...

  • C++ C++ Language 1. True/False: You can use pass by reference when passing a C++ unique...

    C++ C++ Language 1. True/False: You can use pass by reference when passing a C++ unique pointer to a function. a)True b)False 2. True/False: A temporary value in a program can be referred to by at most one lvalue reference. a)True b)False 3. A unique pointer is a pointer a that is declared in a block of code in which it is the only pointer. b that is declared in a function in which it is the only pointer. c...

  • C++ Language 1. True/False: You can use pass by reference when passing a C++ unique pointer...

    C++ Language 1. True/False: You can use pass by reference when passing a C++ unique pointer to a function. a)True b)False 2. True/False: A temporary value in a program can be referred to by at most one lvalue reference. a)True b)False 3. A unique pointer is a pointer a that is declared in a block of code in which it is the only pointer. b that is declared in a function in which it is the only pointer. c that...

  • In this lab, you may use MATLAB to implement the Recursion problems. Do not use a...

    In this lab, you may use MATLAB to implement the Recursion problems. Do not use a for or a while loop to implement a recursive function. 3. Recursion: Sort Game (One of the reasons why we sort) (25 points) Write a recursive function that guesses a number between 0 and x (x being an input number to the function). If you guess too high, it tells you that and continues. If you guess too low, it tells you that and...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise...

    Need help about C PROGRAMMING,, pls use only C LANGUAGE.., yea so im doing this exercise as a practice for c programming, could someone do this as well so i could compare if my code makes makes sense and to see and help correct the errors im getting right now.. Any helpful help would be appreciated.. Pointers &Pointer arithmetic Memory allocation and freeing Main topics: Exercise This lab is designed to give you practice working with pointers and memory allocation...

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