Create a Triangle with Asterisks in C: Step-by-Step Guide

  • Thread starter Thread starter RicardoMarques
  • Start date Start date
  • Tags Tags
    Triangle
AI Thread Summary
The discussion focuses on modifying a C program to create a triangle of asterisks instead of a square. The user is guided to create a new function, "escreve_triangulo(int n)," which should call "escreve_linha" with an incrementing value to achieve the desired triangle shape. Participants emphasize understanding the original program's structure and suggest specific changes to the loop parameters. The conversation also touches on creating a reverse triangle and improving code syntax, with encouragement for the user to explore solutions independently. Overall, the thread highlights the importance of grasping programming concepts while providing hints for code modification.
RicardoMarques
Messages
20
Reaction score
2
< Mentor Note -- code tags have been added for better readability. In the future, please use code tags. Thank you. >[/color]

1. Homework Statement

Code:
#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...?

Homework Equations



There are no equations

The Attempt at a Solution


I tried to change the parameters but has been way too difficult
 
Last edited by a moderator:
Physics news on Phys.org
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.
 
  • Like
Likes RicardoMarques
Can you show me how please ? I'm really confused about this
 
RicardoMarques said:
Can you show me how please ? I'm really confused about this
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.
 
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
 
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.
 
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
 
Please answer this very important question: Do you understand how the original program works?
 
RicardoMarques said:
I am studying for a frequency, getting the answer would be ok... I have no time to ask my professor,
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
RicardoMarques said:
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
lewando said:
Please answer this very important question: Do you understand how the original program works?

Yes I do.
berkeman said:
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.

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 haven't understand clearly the hints you are giving me.
 
  • #11
RicardoMarques said:
... i tried a lot of things and nothing seems to work

What have you tried? Please be specific.
 
  • #12
And what output do you get when you run the program that you posted?
 
  • #13
berkeman said:
And what output do you get when you run the program that you posted?

A square
 
  • #14
RicardoMarques said:
A square
Then this is the key hint for how to get a triangle instead...
lewando said:
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.
 
  • Like
Likes RicardoMarques
  • #15
lewando said:
What have you tried? Please be specific.
I tried to change the function escreve_linha , but i need to keep the parameters i and n..
 
  • #16
I got it thank you !

Edit:

Code:
void escreve_linha(int i)
{
   int j ;
    for( j = 0 ;  j <= i ; j++ ) {
        printf("*");
    }
    printf("\n");
}
}
 
  • #17
What would be the hint, if i wanted to put it this way:
****
***
**
*
 
  • #18
RicardoMarques said:
What would be the hint, if i wanted to put it this way:
****
***
**
*
You tell us! Looks like you are getting the hang of it... :smile:
 
  • #19
berkeman said:
You tell us! Looks like you are getting the hang of it... :smile:

If use both parameters in escreve_linha like escreve_linha(int i, int n) is that ok ?
If it is , I am thinking in something like for( n; n>i; n--)
 
  • #20
RicardoMarques said:
for( n; n>i; n--)
That's not quite correct, but you are on the right track...
 
  • #21
I wrote this and it worked, what can I change to make the code better ?
Code:
void escreve_linha(int i, int n)
{
   
    for( n ;  n > i ; n-- ) {
        printf("*");
       ;
    }
    printf("\n");
}

}
 
  • Like
Likes berkeman
  • #22
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. :smile:
 
  • Like
Likes RicardoMarques
  • #23
berkeman said:
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. :smile:

Long night waiting, going for the 20 :)
Thank you guys !
 
  • Like
Likes berkeman

Similar threads

Replies
3
Views
1K
Replies
4
Views
1K
Replies
12
Views
2K
Replies
12
Views
2K
Replies
1
Views
10K
Replies
17
Views
2K
Replies
3
Views
1K
Back
Top