Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- diamond_number_pattern_with_abs()_shortest_code.c
- Enter the number of rows of triangle above longest row: 3
- diamond_pattern_with_abs(h);
- ---*---
- --***--
- -*****-
- *******
- -*****-
- --***--
- ---*---
- diamond_number_pattern_with_abs_1(h);
- ---4---
- --343--
- -23432-
- 1234321
- -23432-
- --343--
- ---4---
- diamond_number_pattern_with_abs_2(h);
- ---1---
- --212--
- -32123-
- 4321234
- -32123-
- --212--
- ---1---
- diamond_number_pattern_with_abs_3(h);
- ---4---
- --345--
- -23456-
- 1234567
- -23456-
- --345--
- ---4---
- diamond_number_pattern_with_abs_31(h);
- ---4---
- --543--
- -65432-
- 7654321
- -65432-
- --543--
- ---4---
- diamond_number_pattern_with_abs_4(h);
- ---1---
- --222--
- -33333-
- 4444444
- -33333-
- --222--
- ---1---
- diamond_number_pattern_with_abs_5(h);
- ---4---
- --333--
- -22222-
- 1111111
- -22222-
- --333--
- ---4---
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include<stdio.h>
- /*
- ---*---
- --***--
- -*****-
- *******
- -*****-
- --***--
- ---*---
- */
- void diamond_pattern_with_abs( int h )
- { // h - number of rows of triangle above longest row
- int row, col;
- char c; // the character by which the diamond is printed
- for (row=-h; row<=h; row++)
- {
- for (col=-h; col<=h; col++)
- {
- // calculate the current char to print diamond, '-' or '*'
- // c = abs(col) + abs(row) <= h ? '*' : '-'; // or
- if( abs(col) + abs(row) <= h )
- c = '*';
- else
- c = '-';
- printf("%c",c);
- }
- printf("\n"); // new row
- }
- }
- /*
- ---4---
- --343--
- -23432-
- 1234321
- -23432-
- --343--
- ---4---
- */
- void diamond_number_pattern_with_abs_1( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- for (row=-h; row<=h; row++)
- {
- num = 0;
- for (col=-h; col<=h; col++)
- {
- if( col <= 0 )
- num++;
- else
- num--;
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- /*
- ---1---
- --212--
- -32123-
- 4321234
- -32123-
- --212--
- ---1---
- */
- void diamond_number_pattern_with_abs_2( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- for (row=-h; row<=h; row++)
- {
- num = h+2;
- for (col=-h; col<=h; col++)
- {
- if( col <= 0 )
- num--;
- else
- num++;
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- /*
- ---4---
- --345--
- -23456-
- 1234567
- -23456-
- --345--
- ---4---
- */
- void diamond_number_pattern_with_abs_3( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- for (row=-h; row<=h; row++)
- {
- num = 0;
- for (col=-h; col<=h; col++)
- {
- num++;
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- /*
- ---4---
- --543--
- -65432-
- 7654321
- -65432-
- --543--
- ---4---
- */
- void diamond_number_pattern_with_abs_31( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- for (row=-h; row<=h; row++)
- {
- num = 2*h+2;
- for (col=-h; col<=h; col++)
- {
- num--;
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- /*
- ---1---
- --222--
- -33333-
- 4444444
- -33333-
- --222--
- ---1---
- */
- void diamond_number_pattern_with_abs_4( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- num=0;
- for (row=-h; row<=h; row++)
- {
- if( row <= 0 )
- num++;
- else
- num--;
- for (col=-h; col<=h; col++)
- {
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- /*
- ---4---
- --333--
- -22222-
- 1111111
- -22222-
- --333--
- ---4---
- */
- void diamond_number_pattern_with_abs_5( int h )
- { // h - number of rows of triangle above longest row
- int num, row, col;
- num=2*h-1;
- for (row=-h; row<=h; row++)
- {
- if( row <= 0 )
- num--;
- else
- num++;
- for (col=-h; col<=h; col++)
- {
- if( abs(col) + abs(row) <= h )
- printf("%d",num);
- else
- printf("-");
- }
- printf("\n"); // new row
- }
- }
- int main(void)
- {
- int h = 3; // h - number of rows of triangle above longest row of diamond
- printf("\n diamond_pattern_with_abs_1(%d) \n\n", h);
- diamond_pattern_with_abs(h);
- printf("\n diamond_number_pattern_with_abs_1(%d) \n\n", h);
- diamond_number_pattern_with_abs_1(h);
- printf("\n diamond_number_pattern_with_abs_2(%d) \n\n", h);
- diamond_number_pattern_with_abs_2(h);
- printf("\n diamond_number_pattern_with_abs_3(%d) \n\n", h);
- diamond_number_pattern_with_abs_3(h);
- printf("\n diamond_number_pattern_with_abs_31(%d) \n\n", h);
- diamond_number_pattern_with_abs_31(h);
- printf("\n diamond_number_pattern_with_abs_4(%d) \n\n", h);
- diamond_number_pattern_with_abs_4(h);
- printf("\n diamond_number_pattern_with_abs_5(%d) \n\n", h);
- diamond_number_pattern_with_abs_5(h);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement