1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Inverted triangle homework question

  1. Sep 7, 2014 #1

    FOIWATER

    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    Write a recursive function void recurTriangle ( int n, char ch ) which prints out an upside-down triangle. The parameter ch is the character to be used for drawing the triangle, and n is the number of characters on the first row. For example, if n is 7 and ch is ’+’, then the output of the function should be:

    +++++++
    ++++++
    +++++
    ++++
    +++
    ++
    +

    NOTE: I didn't actually use n or ch but it's the same idea


    2. Relevant equations



    3. The attempt at a solution
    #include <stdio.h>
    void triangle(int,char);
    char b;
    int main()
    {
    int a;
    printf("how many rows in the triangle?\n");
    scanf("%d",&a);
    printf("what symbol?\n");
    scanf("%c",&b);
    triangle(a,b);
    }
    void triangle(int x,char y)
    {
    int c,d;
    for (c=1;c<=x;c++)
    {
    for (d=x;d>=c;d--)
    {
    printf("%c",y);
    }
    printf("\n");
    }
    }

    PROBLEM: I am having issues with scanf for a character. If I make the triangle function only have one input and remove the user's ability to edit the character and just input my own and not use scanf, it works fine. It seems to be somehow messing it up

    any help appreciated.
     
  2. jcsd
  3. Sep 7, 2014 #2

    FOIWATER

    User Avatar
    Gold Member

    Actually solved it already sry...

    the problem was scanf(" %c" <--- needs a space there.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Inverted triangle homework question
  1. Homework question! (Replies: 5)

Loading...