Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- diamond_numbers_pattern_v1.c
- Diamond star pattern with abs().
- n is the number of stars in the upper triangle of diamond.
- Diamond will have 2*n-1 raws.
- Example: n = 4
- ------*-
- ----*-*-*-
- --*-*-*-*-*-
- *-*-*-*-*-*-*-
- --*-*-*-*-*-
- ----*-*-*-
- ------*-
- row spaces * r
- 0. 6 1 -3
- 1. 4 3 -2
- 2. 2 5 -1
- 3. 0 7 0
- 4. 2 5 1
- 5. 4 3 2
- 6. 6 1 3
- */
- #include "stdio.h"
- #include "stdlib.h" // for function abs()
- /*
- ---*
- --***
- -*****
- *******
- -*****
- --***
- ---*
- */
- void diamond_of_stars_v11(int n){
- // n - number of rows in upper triangle of diamond
- int r, c; // r - row, c - column
- for (r = -n+1; r < n; r++){ // loop for rows
- for (c = 0; c < abs(r); c++) // print "-"
- printf("-");
- for (c = 0; c < 2*(n-abs(r))-1; c++) // print "*"
- printf("*");
- printf("\n"); // new row
- }
- }
- /*
- *********
- ****-****
- ***---***
- **-----**
- *-------*
- **-----**
- ***---***
- ****-****
- *********
- */
- void hollow_diamond_of_stars_v11(int n){
- // n - number of rows in upper triangle of diamond
- int r, c; // r - row, c - column
- for (r = -n; r <= n; r++){ // loop for rows
- for (c = 0; c <= abs(r); c++) // print "*"
- printf("*");
- for (c = 0; c < 2*(n-abs(r))-1; c++) // print "-"
- printf("-");
- for (c = 0; c <= abs(r); c++) // print "*"
- if( (r==-n || r==n) && c > n-1 )
- ; // do nothing
- else
- printf("*");
- printf("\n"); // new row
- }
- }
- /*
- ------*-
- ----*-*-*-
- --*-*-*-*-*-
- *-*-*-*-*-*-*-
- --*-*-*-*-*-
- ----*-*-*-
- ------*-
- */
- void diamond_of_stars_v12(int n){
- // n - number of rows in upper triangle of diamond
- int r, c; // r - row, c - column
- for (r = -n+1; r < n; r++){ // loop for rows
- for (c = 0; c < abs(r); c++) // print "--"
- printf("--");
- for (c = 0; c < 2*(n-abs(r))-1; c++) // print "*-"
- printf("*-");
- printf("\n"); // new row
- }
- }
- /*
- * * * * * * * * *
- * * * * - * * * *
- * * * - - - * * *
- * * - - - - - * *
- * - - - - - - - *
- * * - - - - - * *
- * * * - - - * * *
- * * * * - * * * *
- * * * * * * * * *
- */
- void hollow_diamond_of_stars_v12(int n){
- // n - number of rows in upper triangle of diamond
- int r, c; // r - row, c - column
- for (r = -n; r <= n; r++){ // loop for rows
- for (c = 0; c <= abs(r); c++) // print "* "
- printf("* ");
- for (c = 0; c < 2*(n-abs(r))-1; c++) // print "- "
- printf("- ");
- for (c = 0; c <= abs(r); c++) // print "* "
- if( (r==-n || r==n) && c > n-1 )
- ; // do nothing
- else
- printf("* ");
- printf("\n"); // new row
- }
- }
- /*
- ---1
- --121
- -12321
- 1234321
- -12321
- --121
- ---1
- */
- void diamond_of_numbers_v211(int n){
- // n - number of rows in upper triangle of diamond
- int r, c, num; // r - row, c - column, num - current number
- for (r = -n+1; r < n; r++){ // loop for all rows of the diamond
- for (c = 0; c < abs(r); c++) // print blanks "-"
- printf("-");
- num = 1;
- for (c = 0; c < 2*(n-abs(r))-1; c++){ // print numbers
- if( c < n-abs(r)-1 )
- printf("%d", num++);
- else
- printf("%d", num--);
- }
- printf("\n"); // new row
- }
- }
- /*
- ---4
- --434
- -43234
- 4321234
- -43234
- --434
- ---4
- */
- void diamond_of_numbers_v212(int n){
- // n - number of rows in upper triangle of diamond
- int r, c, num; // r - row, c - column, num - current number
- for (r = -n+1; r < n; r++){ // loop for all rows of the diamond
- for (c = 0; c < abs(r); c++) // print blanks "-"
- printf("-");
- num = n;
- for (c = 0; c < 2*(n-abs(r))-1; c++){ // print numbers
- if( c < n-abs(r)-1 )
- printf("%d", num--);
- else
- printf("%d", num++);
- }
- printf("\n"); // new row
- }
- }
- /*
- ------1-
- ----1-2-1-
- --1-2-3-2-1-
- 1-2-3-4-3-2-1-
- --1-2-3-2-1-
- ----1-2-1-
- ------1-
- */
- void diamond_of_numbers_v221(int n){
- // n - number of rows in upper triangle of diamond
- int r, c, num; // r - row, c - column, num - current number
- for (r = -n+1; r < n; r++){ // loop for all rows of the diamond
- for (c = 0; c < abs(r); c++) // print blanks "--"
- printf("--");
- num = 1;
- for (c = 0; c < 2*(n-abs(r))-1; c++){ // print numbers
- if( c < n-abs(r)-1 )
- printf("%d-", num++);
- else
- printf("%d-", num--);
- }
- printf("\n"); // new row
- }
- }
- /*
- ------1-
- ----1-2-1-
- --1-2-3-2-1-
- 1-2-3-4-3-2-1-
- --1-2-3-2-1-
- ----1-2-1-
- ------1-
- */
- void diamond_of_numbers_v222(int n){
- // n - number of rows in upper triangle of diamond
- int r, c, num; // r - row, c - column, num - current number
- for (r = -n+1; r < n; r++){ // loop for all rows of the diamond
- for (c = 0; c < abs(r); c++) // print blanks "--"
- printf("--");
- num = n;
- for (c = 0; c < 2*(n-abs(r))-1; c++){ // print numbers
- if(c < n-abs(r)-1 )
- printf("%d-", num--);
- else
- printf("%d-", num++);
- }
- printf("\n"); // new row
- }
- }
- /*
- ----------1
- -------2 3 4
- ----5 6 7 8 9
- 10 11 12 13 14 15 16
- ---17 18 19 20 21
- ------22 23 24
- ---------25
- */
- void diamond_of_numbers_v231(int n){
- // n - number of rows in upper triangle of diamond
- int r, c, num=1; // r - row, c - column, num - current number
- for (r = -n+1; r < n; r++){ // loop for rows
- for (c = 0; c < 3*abs(r); c++) // print "-"
- printf("-");
- for (c = 0; c < 2*(n-abs(r))-1; c++) // print current number
- printf("%3d",num++);
- printf("\n\n"); // new row
- }
- }
- int main(void)
- {
- int n=4; // n - number of rows of the upper triangle of diamond
- printf("\n 1.1 \t n = %d \n\n", n);
- diamond_of_stars_v11(n);
- printf("\n 1.1.1 \t n = %d \n\n", n);
- hollow_diamond_of_stars_v11(n);
- printf("\n 1.2 \t n = %d \n\n", n);
- diamond_of_stars_v12(n);
- printf("\n 1.2.1 \t n = %d \n\n", n);
- hollow_diamond_of_stars_v12(n);
- printf("\n 2.1.1 \t n = %d \n\n", n);
- diamond_of_numbers_v211(n);
- printf("\n 2.1.2 \t n = %d \n\n", n);
- diamond_of_numbers_v212(n);
- printf("\n 2.2.1 \t n = %d \n\n", n);
- diamond_of_numbers_v221(n);
- printf("\n 2.2.2 \t n = %d \n\n", n);
- diamond_of_numbers_v222(n);
- printf("\n 2.3.1 \t n = %d \n\n", n);
- diamond_of_numbers_v231(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement