Arthur Collé
Arthur Collé

Reputation: 2607

Output that looks like a triangle in C

I wrote this program,

include <stdio.h>

int main(){
int size = 5;

int row;
int col;

  for (col=0; col<size; col++){
    for (row=0; row<col;row++){
      printf(" ");
    }

    for (row=0; row <(size-col) ; row++){
      printf("*");
    if(col<=size){
      printf("*");
      }
    }
    printf("\n");
  }
  return 0;
}

It should make a triangle like

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

But instead there is one extra * on every line. What is the problem?

Thanks a lot!

Upvotes: 0

Views: 579

Answers (5)

Rob Latham
Rob Latham

Reputation: 5223

No one brought up recursion? It's not quite pure because you have to track depth. I called it a pyramid (pyr_) instead of a triangle:

#include <stdio.h> 
#include <stdlib.h>

static void pyr_line(int depth, int nrows) {
    int i;

    if (nrows == 0) return;

    for (i=0; i<depth; i++)
            printf(" ");

    for(i=0; i<(2*(nrows-1)+1); i++)
            printf("*");

    printf("\n");
    pyr_line(depth+1, nrows-1);
}

int main(int argc, char ** argv)
{
    pyr_line(0, atoi(argv[1]));
    return 0;
}

Upvotes: 0

makes
makes

Reputation: 6548

Factor your problem into relevant variables and give them meaningful names. This will improve code readability and make bugs easier to fix, e.g.:

#include <stdio.h>

void triangle(int height)
{
    int row, col;
    int width = (2 * height) - 1;

    for (row = 0 ; row < height ; row++)
    {
        for (col = 0 ; col < width ; col++)
        {
            if (row > col)
            {
                putchar(' ');
            }
            else if (col < width - row)
            {
                putchar('*');
            }
        }
        puts("\r");
    }
}

int main(void)
{
    triangle(5);
    return 0;
}

Upvotes: 0

John Gordon
John Gordon

Reputation: 2704

How about printing the *'s like this

for (row=0; row<(9-2*col); row++)
  printf("*");
printf("\n");

This will print nine of the first row, seven on the second, etc.

Upvotes: 0

septical
septical

Reputation: 440

Changing

if(col<=size){

to

if((row % size) > 0){

will have the same effect too.

Upvotes: 1

AusCBloke
AusCBloke

Reputation: 18522

Mystical has a solution to the way you're printing two asterisks an iteration. Using the identifiers row and col in your example also makes things more confusing than just i and j, especially since the outer loop is actually your current row.

An alternative to your mess is (I'm hoping this isn't homework since it's not tagged as such):

int main(void)
{
   int size = 5;
   int i, j;

   for (i = size; i > 0; i--) {

      for (j = i; j < size; j++)
         putchar(' ');

      for (j = 0; j < i*2 - 1; j++)
         putchar('*');

      putchar('\n');
   }

   return 0;
}

You could also put i*2 - 1 in a variable so that it's not calculated at each iteration of the loop (unless the compiler sees that you're not modifying i).

Upvotes: 2

Related Questions