# Homework Help: Asteriscs triangle

1. Dec 3, 2016

### RicardoMarques

< 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");
}

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

}
}

int main(void)
{
printf("Qual o tamanho do 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. Dec 3, 2016

### lewando

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.

3. Dec 3, 2016

4. Dec 3, 2016

### 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.

5. Dec 3, 2016

### RicardoMarques

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

6. Dec 3, 2016

### lewando

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.

7. Dec 3, 2016

### RicardoMarques

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

8. Dec 3, 2016

### lewando

Please answer this very important question: Do you understand how the original program works?

9. Dec 3, 2016

### 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.

10. Dec 3, 2016

### RicardoMarques

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.

11. Dec 3, 2016

### lewando

What have you tried? Please be specific.

12. Dec 3, 2016

### Staff: Mentor

And what output do you get when you run the program that you posted?

13. Dec 3, 2016

### RicardoMarques

A square

14. Dec 3, 2016

### Staff: Mentor

Then this is the key hint for how to get a triangle instead...

15. Dec 3, 2016

### RicardoMarques

I tried to change the function escreve_linha , but i need to keep the parameters i and n..

16. Dec 3, 2016

### RicardoMarques

I got it thank you !!

Edit:

Code (Text):

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

}

17. Dec 3, 2016

### RicardoMarques

What would be the hint, if i wanted to put it this way:
****
***
**
*

18. Dec 3, 2016

### Staff: Mentor

You tell us! Looks like you are getting the hang of it...

19. Dec 3, 2016

### RicardoMarques

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--)

20. Dec 3, 2016

### Staff: Mentor

That's not quite correct, but you are on the right track...

21. Dec 3, 2016

### RicardoMarques

I wrote this and it worked, what can I change to make the code better ?
Code (Text):

void escreve_linha(int i, int n)
{

for( n ;  n > i ; n-- ) {
printf("*");
;
}
printf("\n");
}

}

22. Dec 3, 2016

### Staff: Mentor

If the syntax was accepted and it worked, then it's fine. I thought the initializer in the for() statement looked wrong, but if the compiler took it and it worked, then I think you're good.

23. Dec 3, 2016

### RicardoMarques

Long night waiting, going for the 20 :)
Thank you guys !!