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!

Asteriscs triangle

  1. Dec 3, 2016 #1
    < Mentor Note -- code tags have been added for better readability. In the future, please use code tags. Thank you. >

    1. The problem statement, all variables and given/known data

    Code (Text):

    #include <stdio.h>

    void escreve_linha(int n)
    {
        int i;
        for( i = 0 ; i < n ; i++ ) {
            printf("*");
        }
        printf("\n");
    }

    void escreve_quadrado(int n)
    {
        int i;
        for( i = 0 ; i < n ; i++ ) {
            escreve_linha(n);
         
        }
    }

    int main(void)
    {
        int lado;
        printf("Qual o tamanho do lado? ");
        scanf("%d", &lado);
        escreve_quadrado(lado);
        return 0;
    }
    How do I turn this code into a triangle with asteriscs like this:
    *
    **
    ***
    ****

    Going on and on...?

    2. Relevant equations

    There are no equations
    3. The attempt at a solution
    I tried to change the parameters but has been way too difficult
     
    Last edited by a moderator: Dec 3, 2016
  2. jcsd
  3. Dec 3, 2016 #2

    lewando

    User Avatar
    Gold Member

    You would need to create a function escreve_triangulo(int n), where n is the number of layers.

    It would be similar in structure to escreve_quadrado(), except instead of calling escreve_linha(n), where n is not changing, you would need to call that function with an incrementing value. Note: you already have an incrementing value: i.
     
  4. Dec 3, 2016 #3
    Can you show me how please ? I'm really confused about this
     
  5. Dec 3, 2016 #4

    DrClaude

    User Avatar

    Staff: Mentor

    It's going to be hard to help you with this without doing the work for you. Review what escrivo_quadrando does, and you should see what small modification is needed.
     
  6. Dec 3, 2016 #5
    I am studying for a frequency, getting the answer would be ok... I have no time to ask my professor, and I'm really confused
     
  7. Dec 3, 2016 #6

    lewando

    User Avatar
    Gold Member

    Do you understand how the original program works? If not, what parts are confusing?

    If you can provide more details about what is confusing you, we can work on those details.
     
  8. Dec 3, 2016 #7
    I cannot understand what are the changes that I need to do to make a triangle, i tried a lot of things and nothing seems to work
     
  9. Dec 3, 2016 #8

    lewando

    User Avatar
    Gold Member

    Please answer this very important question: Do you understand how the original program works?
     
  10. Dec 3, 2016 #9

    berkeman

    User Avatar

    Staff: Mentor

    No, it is against the Physics Forums rules for us to give you answers to your schoolwork assignments. You will need to do the work yourself, based on our questions and hints. That's how the PF works.
     
  11. Dec 3, 2016 #10
    Yes I do.
    You misunderstood me. I want to get the answer, but by myself.... But I clearly need a little bit of help now, since I'm stuck. I havent understand clearly the hints you are giving me.
     
  12. Dec 3, 2016 #11

    lewando

    User Avatar
    Gold Member

    What have you tried? Please be specific.
     
  13. Dec 3, 2016 #12

    berkeman

    User Avatar

    Staff: Mentor

    And what output do you get when you run the program that you posted?
     
  14. Dec 3, 2016 #13
    A square
     
  15. Dec 3, 2016 #14

    berkeman

    User Avatar

    Staff: Mentor

    Then this is the key hint for how to get a triangle instead...
     
  16. Dec 3, 2016 #15
    I tried to change the function escreve_linha , but i need to keep the parameters i and n..
     
  17. Dec 3, 2016 #16
    I got it thank you !!

    Edit:

    Code (Text):


    void escreve_linha(int i)
    {
       int j ;
        for( j = 0 ;  j <= i ; j++ ) {
            printf("*");
        }
        printf("\n");
    }



    }
     
  18. Dec 3, 2016 #17
    What would be the hint, if i wanted to put it this way:
    ****
    ***
    **
    *
     
  19. Dec 3, 2016 #18

    berkeman

    User Avatar

    Staff: Mentor

    You tell us! Looks like you are getting the hang of it... :smile:
     
  20. Dec 3, 2016 #19
    If use both parameters in escreve_linha like escreve_linha(int i, int n) is that ok ?
    If it is , im thinking in something like for( n; n>i; n--)
     
  21. Dec 3, 2016 #20

    berkeman

    User Avatar

    Staff: Mentor

    That's not quite correct, but you are on the right track...
     
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: Asteriscs triangle
  1. Centroid Of A Triangle (Replies: 3)

Loading...