Question

C++ 9) Write a recursive function printAll that takes an int num argument and prints all...

C++

9) Write a recursive function printAll that takes an int num argument and prints all the numbers in the range [num, 0]. The function returns nothing. e.g. printAll(5) prints 5, 4, 3, 2, 1, 0. Call your function in the main

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

Here is function:

void printAll(int num)
{
   // recursive calls until num reaches to 0
   if(num == 0)
   {
       // print 0 data with dot
       cout << num << "."<< endl;
       return; // return to break the recursive funtion
   }
   else
   {
       // print num with ,
       cout << num << ", ";
   }
   printAll(num - 1);
}

Here is complete code calling from main:

#include <iostream>
using namespace std;
void printAll(int num)
{
   // recursive calls until num reaches to 0
   if(num == 0)
   {
       // print 0 data with dot
       cout << num << "."<< endl;
       return; // return to break the recursive funtion
   }
   else
   {
       // print num with ,
       cout << num << ", ";
   }
   printAll(num - 1);
}
int main ()
{
   printAll(5);
   return 0;
}

Output:

In case if you want different output comment me.

Add a comment
Know the answer?
Add Answer to:
C++ 9) Write a recursive function printAll that takes an int num argument and prints all...
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
  • For Python debugExercise12.py is a recursive function that accepts an integer argument, n, and prints the...

    For Python debugExercise12.py is a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. By debugging these programs, you can gain expertise in program logic in general and the Python programming language in particular. def main(): # Local variable number = 0 # Get number as input from the user. number = int(input('How many numbers to display? ')) # Display the numbers. print_num(number) # The print_num function is a a recursive function #...

  • /*    Write a function that takes as an argument the value x,    and returns...

    /*    Write a function that takes as an argument the value x,    and returns the result of the expression: 20x^4 - 12x^3 - 3x^2 + 15x + 3    Note: No importing; use the power function given to you, and use it    as a reference for writing functions (note also that main itself        if a function!). */ public class Exercise9_1 { public static void main(String[] args) { int num = 5, pow = 2; System.out.println(num...

  • a) You must write a recursive function that takes something of the form: ([(Int, Int, Int),...

    a) You must write a recursive function that takes something of the form: ([(Int, Int, Int), Int, Int) as an argument and returns something of the form: LE(Int, Int, Int)]] which should be interpreted as a list of lists of 3-tuples of RGB values. You may use helper functions if you wish, but your solution must be recursive. You must document the name of your function at the beginning of your code using a comment like -- foo :: ([(Int,...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • Write a function that takes an int as an argument and returns true if the int...

    Write a function that takes an int as an argument and returns true if the int is an even number and false if not. In any case change the value of the original argument to twice its value. Use the integers 0 through 4 as test cases. Do not use any arrays.

  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • Problem H1 (Using C++) In the main function, define four variables of type int, named: first,...

    Problem H1 (Using C++) In the main function, define four variables of type int, named: first, second, third, and total. Write a function named getData that asks the user to input three integers and stores them in the variables first, second, and third which are in the main function. Write a function named computeTotal that computes and returns the total of three integers. Write a function named printAll that prints all the values in the format shown in the following...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Write a python code that takes in an number 0-9 and prints out the word of...

    Write a python code that takes in an number 0-9 and prints out the word of the number. For example 1 would print out one. Below is the skeleton of the code that needs to be filled in. def num2string(num): """ Takes as input a number, num, and returns the corresponding name as a string. Examples: num2string(0) returns "zero", num2string(1)returns "one" Assumes that input is an integer ranging from 0 to 9 """ numString = "" ################################### ### FILL IN...

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