3 Replies - 517 Views - Last Post: 22 April 2016 - 05:09 PM Rate Topic: -----

#1 cboob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-April 16

Recursive function to print a right triangle

Posted 22 April 2016 - 11:04 AM

Hey! Can anyone help me with this code? *** must be in c language ***


Write a recursive function which takes the side of the triangle, and the fillCharacter and displays it. For example, if the side of the triangle is 5 and fillCharacter is ‘@”, it should display as follows:

@

@@

@@@

@@@@

@@@@@

The function header should be: void displayTriangle(int side, char fillCharacter); The main program MUST be as follows (and MUST NOT be changed at all):

#include <stdio.h>


void displayTriangle(int side, char fillCharacter);
int main(void) {
int side;
char theCharacter;

printf("Please enter the character: ");
fflush(stdout);
scanf("%c", &theCharacter);

printf("Please enter the side of the triangle: ");
fflush(stdout);

scanf("%d", &side);


displayTriangle(side, theCharacter);

}

void displayTriangle(int side, char fillCharacter) {}


So the code containing the recursive function would go in the last set of curly brackets { }

Is This A Good Question/Topic? 0
  • +

Replies To: Recursive function to print a right triangle

#2 kaa  Icon User is offline

  • D.I.C Addict

Reputation: 224
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: Recursive function to print a right triangle

Posted 22 April 2016 - 11:21 AM

... pretty much the same answer as the one I gave to your other thread applies here.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,870
  • Joined: 31-December 10

Re: Recursive function to print a right triangle

Posted 22 April 2016 - 11:26 AM

I'll let another mod decide if this thread should be merged with the other, but I can say they both deal with recursion, so it might be a good idea.

To the OP, are you having a problem with recursion? If so, what about it is confusing you?
Was This Post Helpful? 0
  • +
  • -

#4 CordovaCode  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 125
  • Joined: 14-March 16

Re: Recursive function to print a right triangle

Posted 22 April 2016 - 05:09 PM

The following is not C code, but it will show you how recursion works in a very basic way. You should be able to figure out how to use most of the concepts to solve your problem.

#include <iostream>

// function prototypes, show 2 ways to use recursion
void heads_recursive(int, char);
void tails_recursive(int, char);

int main() {
// create and initialize variable
    int width(0);
    char fill('@');

    width = 5;
// call the functions to see the differences
    heads_recursive(width, fill);
    tails_recursive(width, fill);
}

// the actual functions

void heads_recursive(int width, char fill) {
// create a condition that will stop the recursion
    if(width > 0)
    {
// the recursive call
        heads_recursive(width - 1, fill);
        std::cout << width << " : " << fill << std::endl;
    }
}

void tails_recursive(int width, char fill) {
// create a condition that will stop the recursion
    if(width > 0)
    {
        std::cout << width << " : " << fill << std::endl;
// the recursive call
        tails_recursive(width - 1, fill);
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1