1

I have to print this:

          0          
        1 0 1        
      2 1 0 1 2      
    3 2 1 0 1 2 3    
  4 3 2 1 0 1 2 3 4  
5 4 3 2 1 0 1 2 3 4 5

My code:

#include<stdio.h>
    int main()
    {
        int i,j,k,l;

        for(i=1;i<=6;i++)
        {
            for(j=6;j>i;j--)
            {
                printf(" ");
            }

            for(k=i-1;k>=0;k--)
            {
                printf("%d",k);
            }

            for(l=1;l<i;l++)
                printf("%d",l);
            printf("\n");
        }
        return 0;
    }

My output:

     0          
    101        
   21012      
  3210123    
 432101234  
54321012345

I have just started coding in C so this is new to me. How do I put space between numbers so that the final output looks more elegant than it currently is?

I tried %-6d in printf("%d",k); to adjust the width but it had wrong indentation of the output.

1
  • If I were you I start building a 2d matrix and then printout result. Commented Oct 2, 2015 at 7:53

3 Answers 3

5

That's easily fixed. Everywhere that you currently print a character (other than the newline), simply print that character followed by a space.

If you do that, you'll see the output you desire:

          0 
        1 0 1 
      2 1 0 1 2 
    3 2 1 0 1 2 3 
  4 3 2 1 0 1 2 3 4 
5 4 3 2 1 0 1 2 3 4 5 

I won't show you the code since it's almost certainly classwork and you'll be a better coder if you nut it out yourself. But I will give you the tidbit that there are three lines that need to change. That should be more than enough to get the issue solved.

As a side note, this will also print a space at the end of each line, after the final digit. If that's not allowed, there are ways to fix it, primarily by changing the final loop so it doesn't output a space for the final item on the line.

It will also start failing miserably if you start trying to use two-digit numbers. To solve that, you need to know the widest number and use printf fields specifiers with widths. However, since it wasn't in the original spec, I haven't implemented that (see YAGNI).


If you wanted to implement something like that (with both the larger numbers and removed space at line end), you could use something like:

#include<stdio.h>

#define COUNT 15

int main(void) {
    // Sanity check.

    if (COUNT < 1) return 0;

    // Get maximum width.

    int width = 1, value = COUNT;
    while (value > 9) {
        value /= 10;
        width++;
    }

    // For each line.

    for (int line = 1; line <= COUNT; line++) {
        // Output leading spaces.

        for (int column = COUNT; column > line; column--)
            printf("%*s ", width, "");

        // Output descending digits.

        for (int column = line - 1; column >= 0; column--)
            printf("%*d ", width, column);

        // Output ascending digits.

        for (int column = 1; column < line; column++) {
            printf("%*d", width, column);
            if (column < line - 1) putchar(' ');
        }

        // Finish off line.

        putchar('\n');
    }

    return 0;
}

It has the following advantages:

  • it allows for variable widths.
  • it uses more appropriately named (and fewer) loop variables.
  • it doesn't output the space at the end of each line.

Just don't use it as is if this is classwork, you'll almost certainly be found out.

Sign up to request clarification or add additional context in comments.

8 Comments

@LPs, I did try it, the output you see in the answer is what I got.
@LPs: this is said in the answer, space is a character other than newline.
@MarineBiologist: every character other than the newline!!!!!!!!! :-) That means your line which spits out a single space should be spitting out two of them. That will fix your alignment issue.
@paxdiablo Perfect! Forgot about space line :)
@MarineBiologist, see the code I added at the end (the if statement inside the for loop). This will let you do it. The original method I suggested was not that good, the changed one is better.
|
1

use this.. add one space in in all three loops.

int i, j, k, l;
for (i = 1; i <= 6; i++) {
    for (j = 6; j > i; j--) {
        System.out.printf("  ");
    }
    for (k = i - 1; k >= 0; k--) {
        System.out.printf("%d ", k);
    }
    for (l = 1; l < i; l++) {
        System.out.printf("%d ", l);
    }
    System.out.printf("\n");
}

Comments

-1

Instead of printf("%d",k) use printf("%d ",k).

1 Comment

This won't add enough spaces at the beginning of each line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.