2

P.S. I'm a beginner and I was trying to find the following output :

         1 
      1  2  3 
   1  2  3  4  5 
1  2  3  4  5  6  7 
   1  2  3  4  5 
      1  2  3 
         1

and here's my try :

#include<stdio.h>
#include<conio.h>

void main() {    
    int n;
    scanf("%d",&n);   

    for(int i=1;(i<=2*n);i++){
        int temp=1,t=2*n-1;     
           
        for(int j=0;j<abs(n-i);j++){
            printf("  ");
        } 
        for(int j=t;j>=abs((2*(i-1))-t);j--) {            
            printf(" %d",temp);
            temp++;
        }                 
    printf("\n");        
    }    
}

as you can see.. I tried my best to remove the i=n condition UNSUCCESSFULLY. or if anyone can provide a more easier way to print the pattern.. I'd my grateful

Blu
  • 21
  • 2
  • What do you get when you run your version? – nicomp Mar 15 '22 at 17:50
  • C++ is not C. Please format your code blocks properly. – Passer By Mar 15 '22 at 17:53
  • Please apply consistent indentation. – Yunnosch Mar 15 '22 at 18:02
  • 2
    What do you mean by "remove the i=n" condition? Where is that condition? What do you expect the result of removing it to be? – Yunnosch Mar 15 '22 at 18:03
  • 2
    I don't understand. Why is it important to remove a particular condition from the program? – John Bollinger Mar 15 '22 at 18:03
  • 1
    Is the desired output for the input `4` or some other value? – Jonathan Leffler Mar 15 '22 at 18:07
  • @nicomp I get 1 123 12345 1234567 1234567 12345 123 1 I want to remove that extra 7 digit line – Blu Mar 15 '22 at 18:08
  • `int` is the *only* conforming return type for `main()` in a hosted C implementation. – John Bollinger Mar 15 '22 at 18:10
  • @Yunnosch i increments from 0 to 2n.. at i=n it prints a line that I dont want.. – Blu Mar 15 '22 at 18:11
  • @JonathanLeffler yup for 4.. maybe I should have used 4 directly instead of declaring a variable but ye.. for that pattern, the input should be 4 – Blu Mar 15 '22 at 18:12
  • @JohnBollinger cauz it prints a line I dont need in my output – Blu Mar 15 '22 at 18:13
  • No, it doesn't? Your program prints almost the specified output for me (with input "4"). There are no extra lines, but the first several lines are each indented one level fewer than they should be. – John Bollinger Mar 15 '22 at 18:16
  • 1
    How about using two outer loops? First for printing increasingly long lines. Second for printing one fewer decreasingly long lines. That should allow you to program more straightforward, drop the `abs()` and get rid of whatever awkward construct you do not like ... even if I still cannot find it in your shown code. – Yunnosch Mar 15 '22 at 18:16
  • Thanks for indentation. Now drop three quarters of your white space, if I may propose that. – Yunnosch Mar 15 '22 at 18:21
  • If really you just want not printing the nth line, you can try inserting at the beginning of the outer loop `if(i==n) continue;` – Damien Mar 15 '22 at 18:29
  • @Damien but that would devoid me of the required white space.. and not print a symmetric pattern. right ?? – Blu Mar 15 '22 at 18:34
  • You have updated the question title to emphasize use of `abs()`. Is it actually important to use that function? – John Bollinger Mar 15 '22 at 18:54
  • 1
    FYI, updating the title with the question status isn't how SO is supposed to work. Rather, you should accept an answer as correct, including one you write yourself if you feel so inclined. SO is meant to serve as a database of information for future users. Simply saying "I found my answer" in the title but neglecting to post it is useless for any future visitors, you might as well delete this post. – yano Mar 15 '22 at 19:27
  • Title modification rolled back. – John Bollinger Mar 15 '22 at 19:34

1 Answers1

1

For a given positive input n, you want to print 2 * n - 1 lines.

Now consider the indent of each line: it counts from n - 1 positions down to 0, then back up to n - 1. If you number the lines starting with 1, then that indent is abs(n - line) positions.

The count of numbers to print on each line can be viewed as a function of the indent: the maximum is 2 * n - 1, and each unit of indent reduces that by 2. With a bit of rearrangement, that gives a maximum value to print on each line of 2 * (n - indent) - 1.

That should be sufficient information for you to write a program that prints your pattern, using a single outer loop over all the pattern lines, and employing the abs() function meaningfully. The details are left as the exercise they are meant to be.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Looking carefully at he figure, the ident is `n_blank = 3 * abs(line - n) + 1;`, with 1-indexed `line`. – Damien Mar 15 '22 at 20:02
  • 1
    Well, @Damien, it depends a bit on what you consider the indent and what you consider the (possibly space-padded) field. Anyway, when I say "positions", I am being generic -- not a number of characters, but a number of indentation units, whatever those happen to be. Obviously, if there is a desire for an additional fixed indent on each line then that can be handled as a separate matter. – John Bollinger Mar 15 '22 at 20:08