PROGRAM FOR OUTPUTTING ISOSIDE TRIANGLE WITH RIGHT LOWER RIGHT ANGLE

Asked 2 years ago, Updated 2 years ago, 29 views

  • Function void put_stars(int no)
  • that prints no "*"
  • Function that prints no half-width spacesvoid put_spaces(int no)
  • Draw an isosceles triangle with a right lower right corner of height n with a "*" void put_triangle3(intn)

But my program didn't work.
The example execution should be as follows:

$./a.out
What level is it? 2
    *
   **

However, with or without the put_spaces function in my program, the following is the same:

$./a.out
What level is it? 2
**
*

In this way, the put_spaces function didn't seem to work well. How do I write the put_spaces function to make it look like an example?

void put_stars (int no)
{
    inti;
    for(i=no;i>=1;i--) 
        printf("*");
}  

void put_space (int no)
{
    inti;
    for (i=no-1;i>=1;i--) 
        printf("";

}

void put_triangle(intn)
{
   inti;
    for(i=n;i>=1;i--){
        put_stars(i);
        printf("\n");
    }
    return;
}

int main (void)
{
    intln;
    printf("What level? ");
    scanf("%d", & ln);

    put_triangle(ln);

    return 0;
}

c

2022-09-29 20:27

3 Answers

Before writing the program, it is recommended that you identify the necessary steps and explain them in words.

When the height is HEIGHT, the number of characters displayed per line is WIDTH.

nDisplay in step...

  • * remains n
  • (half-width space) n and not , but WIDTH-n


2022-09-29 20:27

I'm creating the function put_space(), but I didn't call it.More to say
The function put_spaces() has not been created (note typo)
The compiler does not take into account typographical errors, nor does it run if you create a function with the correct name.

***If you want to display 3 stars in 2 spaces (full-width for your convenience)

put_space(2);//put_space as typo
put_stars(3);

Otherwise, it won't work out what you expected, so it's almost obvious where this fix is needed.

Additional comments asking for more answers

If I were to add more, it would be almost a complete answer, but if it's homework, it's time out and I think it's enough.

As Mr. @cubick said, it is important to think about the procedure, and this kind of loop is similar to the mathematical inductive thinking.Considering the start or end conditions and the behavior at one point in the middle, it is generally successfully.In , as already mentioned, the procedure is

  • i loops above 0 and below n (0 is the top row)
  • The
  • i line contains n-(i+1) pieces
  • i lines contain * i+1
  • lines

If the behavior of the optional i line cannot be guided at once, assume one specific number.Like Oira's first answer, like two and three. And in the next line, I'll think about what to do.If you train like that, you'll get used to thinking about writing programs and be able to think about specifications quickly.

Show questions tagged with Grammarly write the above steps faithfully.

void put_triangle(intn){
    for(inti=0;i<n;++i){
        put_spaces(n-(i+1));
        put_stars(i+1);
        putchar('\n');
    }
}

style is up to you.

To make these kinds of challenges more generalized
- There is a square canvas of nxn
- * where certain conditions are met where they are not met
And you can display arbitrary figures (text or graphic) on the screen, right?

Create an isosceles triangle with *.


2022-09-29 20:27

*
   **
  ***
 ****
*****

Let's imagine the third stage of the drawing.

***

requires two spaces before *.

I think I can give you an answer soon.


2022-09-29 20:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.