C programming pattern problem diamond


can anyone help to code it

@debomastet335, there are many ways to try and solve this problem, please post what you have tried so far…
Here below is an attempt at a parametric and array-based solution, the issue is trying to make the diamond print legibly for numbers > 10.

1 Like

Honestly, these “please do my homework for me” questions are not a good thing, you should really show what you’ve tried etc, but they are also catnip

function * row([x,y], n) {
    // NOTE `limit` is the inverse of `n` (the input value):
    // if `n` is 3, row 1 is 3, row 2 is 2, row 3 is 1).
    let limit = n - y + 1;
    do { yield (x >= limit) ? x + y - limit : 0; } while (++x < n);
    do { yield (x >= limit) ? x + y - limit : 0; } while (--x >= 1);
}

function * values(n, y = 1) {
    do { yield* row([1, y], n); } while (++y < n);
    do { yield* row([1, y], n); } while (--y >= 1);
}

function render (n, cellWidth = 3) {
    let i = 1;
    let output = "";

    for (value of values(n)) {
        // NOTE pad values to the given cellWidth, replacing
        // zeroes with empty strings.
        output += `${!value ? "" : value}`.padStart(cellWidth, " ");
        // NOTE if the incremented counter indicates the
        // end of a row, append a newline then set the
        // counter back to its starting value.
        if (++i > n + n - 1) {
            output += "\n"
            i = 1;
        }
    }

    return output;
}
> console.log(render(10))
                             1                            
                          2  3  2                         
                       3  4  5  4  3                      
                    4  5  6  7  6  5  4                   
                 5  6  7  8  9  8  7  6  5                
              6  7  8  9 10 11 10  9  8  7  6             
           7  8  9 10 11 12 13 12 11 10  9  8  7          
        8  9 10 11 12 13 14 15 14 13 12 11 10  9  8       
     9 10 11 12 13 14 15 16 17 16 15 14 13 12 11 10  9    
 10 11 12 13 14 15 16 17 18 19 18 17 16 15 14 13 12 11 10 
     9 10 11 12 13 14 15 16 17 16 15 14 13 12 11 10  9    
        8  9 10 11 12 13 14 15 14 13 12 11 10  9  8       
           7  8  9 10 11 12 13 12 11 10  9  8  7          
              6  7  8  9 10 11 10  9  8  7  6             
                 5  6  7  8  9  8  7  6  5                
                    4  5  6  7  6  5  4                   
                       3  4  5  4  3                      
                          2  3  2                         
                             1
> console.log(render(5, 2))
         1         
       2 3 2       
     3 4 5 4 3     
   4 5 6 7 6 5 4   
 5 6 7 8 9 8 7 6 5 
   4 5 6 7 6 5 4   
     3 4 5 4 3     
       2 3 2       
         1

I think as long as we don’t do it in C it’s fine :smirk:

1 Like

thats a nice solution @DanCouper, I really need to get up to speed with JS generator functions

I think a can make it simpler: the issue I had was solved with two do…while loops in each generator, so the values go up then down, and I think I could just use one with a counter instead (I could also dump them and inline everything, but I wanted the separation between generating and rendering).

I could also look at using some maths to generate the sequence, it looks very much like something that could be plotted on a graph (so using coordinates defo seems the best approach)

Ugh I spent far too long trying to build a solution that wasn’t just basic nested loops, I really should have got more actual work done today. Catnip though

1 Like

Hlo, thanks for the reply.I try it in c by using loop but don’t come.Give the code below:

# include <stdio.h>
int main()
{
	int i,j,k;
	for(i=1;i<=5;i++)
	{
		for(j=1;j<=5-i;j++)
		{
			printf(" ");
		}
		for(k=1;k<=i;k++)
		{
			printf("%d",k);
		}
		for(k=1;k<i;k++)
		{
			printf("%d",k);
		}
		printf("\n");
	}
for(i=1;i<=5;i++)
	{
		for(j=1;j<=i;j++)
		{
			printf(" ");
		}
		for(k=1;k<=5-i;k++)
		{
			printf("%d",k);
		}
		for(k=1;k<5-i;k++)
		{
			printf("%d",k);
		}
		printf("\n");
	}
	}

Ok, fair start, you force printed the top and bottom halfs of the diamond, but really, it is at first enough to focus only on the top left quadrant, as the top right quadrant is a mirror of the top-left and the bottom- half is a mirror of the top-half. So you can simply cut down your code to this

int main()
{
	int i,j,k;
	for(i=1;i<=5;i++)
	{
		for(j=1;j<=5-i;j++)
		{
			printf(" ");
		}
		for(k=1;k<=i;k++)
		{
          printf("%d",k);
		}
		printf("\n");
	}
}

which will give

    1
   12
  123
 1234
12345

Now, there are 3 main issues, (1) the diagonal (hypotenuse) is constant and (2) the adjacent (center column of the diamond) has both odd and even numbers, where your problem requires only odd and (3) All elements between the center and diagonal need adjustment and are dependent on (1) and (2). So first let’s forget (2) and (3) and deal with (1). With a very simple conditional you can vary the diagonal numbers , ex.

int main()
{
	int i,j,k;
	for(i=1;i<=5;i++)
	{
		for(j=1;j<=5-i;j++)
		{
			printf(" ");
		}
		for(k=1;k<=i;k++)
		{
           if(k==1) printf("%d",i);
           else printf("%d",k);
		}
		printf("\n");
	}
}

prints

    1
   22
  323
 4234
52345

Now obviously this still won’t work, but can you think of a way to make the adjacent (center column of diamond) to print only odd numbers ? post your attempts below and we’ll continue…

can i use

if(k%2==1) printf("%d",k++) 
else k--;

i hope it can work.
or can i use while loop to print this.
please suggest the best way

@debomastet335

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Did you try it yourself? , what answer did you get ?, I will give you more hints, the question you need to ask yourself is, at what point in my inner loop do the center column values get printed in terms of the outer loop? you can then isolate that point using a conditional,
To help you answer that, you can do a simple print on the screen inside your nested loop and analyze each step of the loop.

printf("i=%d,k=%d\n",i,k);

results

i=1,k=1
i=2,k=1
i=2,k=2
i=3,k=1
i=3,k=2
i=3,k=3
i=4,k=1
i=4,k=2
i=4,k=3
i=4,k=4
i=5,k=1
i=5,k=2
i=5,k=3
i=5,k=4
i=5,k=5

I finally solve it.check the code.`

#include<stdio.h>
 
int main()
{
    int i,j,k,n,l=0;
 
    printf("Enter the no of lines\n");
    scanf("%d",&n);
 
    for(i=1;i<=n;i++)
    {
    	for(j=0;j<n-i;j++)
    	printf(" ");
        k = i;
        for(j=1;j<=i;j++,k++)
        {
            printf("%d",k);
        }
        
        k--;
        for(j=1;j<i;j++)
          {  
          	printf("%d",--k);
          	  
          	
          	}
         printf("\n");
    }
    
    //end of upper part.
     for(i=n-1; i>=0; i--){   //for each row
            //outer for loop
        for(j=0; j<=l; j++)//print space at the begining
        printf(" ");
        k=i;
         for(j=1; j<=i; j++,k++)//print right side of                        pyramid
        
        printf("%d",k);
        
        k--;
         for(j=i-1; j>=1; j--)//print right side of                      pyramid
        printf("%d",--k);
     printf("\n");
    l++;
    }
    
    
}

Good Job! now are there any ways you could refactor the bottom mirror ?