Question

(Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to...

(Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to solve the problem.

void Triangle(int);

Write a main function to test it. For example, a function call Triangle(6) will displays the following 6 rows of a triangle pattern.

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

1 2 3 4 5 6 5 4 3 2 1

And a function call Triangle(4) will displays the 4 rows of the pattern as below.

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

A function call Triangle(9) will displays the 9 rows of the pattern as below.

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

1 2 3 4 5 6 5 4 3 2 1

1 2 3 4 5 6 7 6 5 4 3 2 1

1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

(Hint: You may need to write another recursive functions to display one single row in the triangle pattern. And let your Triangle function invoke the second recursive function if necessary. E.g.,

void printRow(int m, int n)

When called with two actual parameters, the function call printRow(2,5) will display a sequence 2 3 4 5 4 3 2; The function call printRow(1,5)will display 1 2 3 4 5 4 3 2 1. )

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

// C implementation to print the given number sequences recursively

#include <stdio.h>
#include <stdlib.h>

void printRowExtend(int n)
{
    if (n < 1)
return;
  
   // print the remaining numbers of the nth row recursively in increasing order
   printf("%d ",n);
   printRowExtend(n-1);
}

void printRow(int n,int k)
{
   if (n < 1)
{
printRowExtend(k-1);
return;
}  
   // print the remaining numbers of the nth row recursively in decreasing order
printf("%d ",k-n+1);
   printRow(n-1,k);
}

// to recursively print the numbers in row number wise
void printRecur(int n,int k)
{
   if(n>k)
return;
  
   printRow(n,n); // pass n & n because we have to go first from 1 to n & then from n to 1
printf("\n"); // after each row, there should be new line
   printRecur(n+1,k);
}

// Triangle function as described in the question
void Triangle(int n)
{
printRecur(1,n);
}

// Driver program for the question given in description
int main()
{
   int n = 9;
   Triangle(n);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
(Recursive Function) Display the triangle pattern without using loop. Write a recursive function named Triangle to...
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
  • Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a...

    Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a 2D array): (2) int columns; (3) int rows Public: (1) A default constructor that creates a 2D array, 8 6 4 (2) A constructor that takes three values for the three private members, and creates a 2D array accordingly. (3) The "get" and "set" methods for the three private members. (4) A method that displays a 2D array, stored in the private member matrix,...

  • C++ Assignment 4 - For example 2, write a non recursive version of this function... using...

    C++ Assignment 4 - For example 2, write a non recursive version of this function... using a regular loop. Let's consider writing a function to find the factorial of an integer, N!. For example 7! equals 7*6*5*4*3*2*1. int myFactorial( int integer) if( integer == 1) return 1; else return (integer * (myFactorial (integer-1))); // action performed on call - pass into function "integer - 1" // action performed on return *

  • use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the...

    use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. (pascals 0 0) → 1 (pascals 2 0) → 1 (pascals 2 1) → 2 (pascals 4 2) → 6 (printTriangle 5) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 [5 marks] Write a procedure...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • Hello, I need help writing the two methods to print a triangle shape in java. An...

    Hello, I need help writing the two methods to print a triangle shape in java. An example of the shape is as follows: 9 8 7 6 5 4 3 8 7 6 5 4 3 7 6 5 4 3 6 5 4 3 5 4 3 4 3 3 One method should be defined as "public static void printPattern(int num1, int num2, Boolean ascending)". This method will print a upper-triangle matrix-like layout filled will a sequence of integers...

  • 1. Write a function that converts a string into an int. Assume the int is between...

    1. Write a function that converts a string into an int. Assume the int is between 10 and 99. Do not use the atoi() or the stoi() function. 2. Write a function prototype for problem 1. 3. Write a function call for the function you defined in problem 1. 4. Write a function that converts an int between 10 and 99 into a string. 5. Write a function prototype for problem 4. 6. Write a function call for function you...

  • use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse...

    use C++            Project 6 1. Using vectors or arrays, write a function named word_rev() that reverse any given word. 2. Write a program that will: Ask the user to enter a word. Save the entry as word_entered. Call word_rev on the entry Display the reversed entry (word_entered) 3. Write a function named prime() that determine whether or not a given number n (greater than one) is prime. The algorithm: If n is even then n is not a prime number...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • 28. Implement an overloaded member function of the IntVector class named assign. This version of assign passes in an ar...

    28. Implement an overloaded member function of the IntVector class named assign. This version of assign passes in an array of integers, along with 2 unsigned index values. This function may assume the indices are valid (i.e., does no bounds error checking on the array passed in). For example, if you have an array of integers named iarr with a size of 10, you can call assign on an IntVector named v like this: int iarr[] 1, 2, 3, 4,...

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