Home > Software design >  how do you make a isosceles triangle using for loops?
how do you make a isosceles triangle using for loops?

Time:11-14

#include <stdio.h>

#define HEIGHT 5
#define WIDTH 7

int main () {
    int roof;
    roof=WIDTH/2;
     for (int rows = 0; rows < roof; rows  ) {
      for(int col=0; col < WIDTH; col  )
      {
      
      
      if(col == 0 ||col == WIDTH - 1) 
      putchar('/');
  
      else
      {
      
     
      
          if((col == roof )&&rows==0)
          printf("X");
      
          for(int col=0; col < WIDTH; col  )
          {
          
          }

       }
      }
     putchar ('\n');

     }

  for (int rows = 0; rows < HEIGHT; rows  ) {
      for(int col=0; col < WIDTH; col  )
      {
      if(rows==0 || rows==HEIGHT-1 )
      {
          putchar('X');
      }
      else{

     if(col==0 || col==WIDTH-1)
     {
     putchar('X');
     }
     else
     putchar(' ');
      }
      }
     putchar ('\n');

      }
  return 0;
}

ok so I have made some progress, not much though. I am stuck immediately after making a top of my roof. I can pinpoint the exact position of the top of my roof by simply dividing "WIDTH" by 2 while "WIDTH" is odd.

however I was not able to figure out how to make the rest of my roof

its supposed to look like this enter image description here

I thought about and -- "roof" for every row so it would switch coordinates, but I have no clue how, I would appreciate further asistance if possible.

CodePudding user response:

That should do it:

#include <stdio.h>

#define HEIGHT 5
#define WIDTH 7

int main() {
    int roof = WIDTH / 2;
    int offset = (WIDTH % 2 == 0) ? 1 : 0;

    for(int col_start = roof; col_start > 0; col_start--) {
        if(col_start != roof || offset != 1) {
            
            for(int col = 0; col < col_start; col  ) {
                putchar(' ');
            }
            putchar('X');
        

            if(col_start != roof) {
                for(int col = 0; col < (roof - col_start) * 2 - 1 - offset; col  ) {
                    putchar(' ');
                }
                putchar('X');
            }
        
            putchar('\n');
        }
    }
    

    for (int rows = 0; rows < HEIGHT; rows  ) {
        for (int col = 0; col < WIDTH; col  ) {
        if (rows == 0 || rows == HEIGHT - 1) {
            putchar('X');
        } else {

            if (col == 0 || col == WIDTH - 1) {
            putchar('X');
            } else
            putchar(' ');
        }
    }
    putchar('\n');

    }
    return 0;
}

Outputs:

   X
  X X
 X   X
XXXXXXX
X     X
X     X
X     X
XXXXXXX

And with an even width (8):

   XX
  X  X
 X    X
XXXXXXXX
X      X
X      X
X      X
XXXXXXXX

CodePudding user response:

You can do it in multiple loops if your like (one set for roof, another set for walls), but I still prefer a single loop that handles all rows from the top of the roof to the floor where you simply keep track of which row you are printing.

You can calculate the number of rows needed for the roof, to handle odd/even number of rows with:

  int roofrows = (WIDTH   1) / 2 - 1,     /* int division intentional */

Above you simply add one to the width and divide by two (with integer division) and then subtract one from the result. This ensures, e.g. a width of 7 or 8 both have a 3-row roof. The former with one X at the top-center, the latter with XX marking the top to handle the even number of columns.

(note: you should add checks to ensure WIDTH > 2, otherwise there really isn't any place to put a roof. Those checks I leave to you)

You get the total height of the house by adding roofrows HEIGHT, e.g.

      total = HEIGHT   roofrows;          /* total height of building */

Now you will loop over all rows in the building from 0 <= rows < total. To print the building just keep track of which row you are printing. If rows < roofrows you are printing the roof. From then on, you are simply printing the walls -- which can be done exactly like the box question.

Putting it altogether, you would have:

#include <stdio.h>

#define  HEIGHT  5
#define  WIDTH   7

int main (void) {
  
  int roofrows = (WIDTH   1) / 2 - 1,     /* int division intentional */
      total = HEIGHT   roofrows;          /* total height of building */
  
  for (int rows = 0; rows < total; rows  ) {      /* loop over each row */
    for (int cols = 0; cols < WIDTH; cols  ) {    /* loop over each col */
      if (rows < roofrows) {                      /* printing roof? */
        int rafter = roofrows - rows;             /* get rafter offset */
        /* if current col is rafter - print X */
        if (cols == rafter || cols == WIDTH - rafter - 1) {
          putchar ('X');
        }
        else {              /* otherwise print space */
          putchar (' ');
        }
      }
      /* working on the walls, is row top or bottom row? */
      else if (rows == roofrows || rows == total - 1) {
        putchar ('X');      /* if so, printf all X */
      }
      else {  /* otherwise, normal wall row */
        if (cols == 0 || cols == WIDTH - 1) {   /* 1st or last col gets X */
          putchar ('X');
        }
        else {  /* otherwise, middle is space */
          putchar (' ');
        }
      }
    }
    putchar ('\n');         /* tidy up with \n after each row */
  }
}

Example Use/Output

$ ./bin/xboxroof
   X
  X X
 X   X
XXXXXXX
X     X
X     X
X     X
XXXXXXX

There are many ways to put this together as with the last question. Let me know if you have questions here and I'm happy to help further.

  •  Tags:  
  • c
  • Related