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

  • Thread starter RicardoMarques
  • Start date
  • Tags
    Triangle
In summary, the code uses an incrementing value to keep track of how many times the function escreve_linha() has been called.
  • #1
RicardoMarques
20
2
< Mentor Note -- code tags have been added for better readability. In the future, please use code tags. Thank you. >

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
  • #2
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
  • #3
Can you show me how please ? I'm really confused about this
 
  • #4
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.
 
  • #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
 
  • #6
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
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
Please answer this very important question: Do you understand how the original program works?
 
  • #9
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

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

1. How do I create a triangle with asterisks in C?

To create a triangle with asterisks in C, you will need to use nested for loops. The outer loop will control the number of rows in the triangle, while the inner loop will control the number of asterisks in each row.

2. What is the syntax for creating a triangle with asterisks in C?

The general syntax for creating a triangle with asterisks in C is:

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

Where "n" is the number of rows in the triangle.

3. Can I create an inverted triangle with asterisks using this method?

Yes, you can create an inverted triangle with asterisks in C using a similar method. Instead of starting the outer loop at 1 and incrementing, you will start at "n" and decrement. This will create an inverted triangle with the desired number of rows.

4. How do I add spaces between the asterisks in each row?

To add spaces between the asterisks in each row, you can use the "printf" function with a space character in between the asterisks. For example:

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

5. Can I create a hollow triangle with asterisks using this method?

Yes, you can create a hollow triangle with asterisks in C by modifying the inner loop. Instead of printing an asterisk every time, you can check if the current iteration is the first or last column and print an asterisk or a space accordingly. This will create a hollow triangle with the desired number of rows.

Similar threads

Replies
3
Views
970
Replies
4
Views
1K
Replies
12
Views
2K
Replies
12
Views
1K
Replies
1
Views
9K
Replies
17
Views
2K
Replies
3
Views
1K
Back
Top