Advertisement
dmilicev

diamond star pattern with number of rows v1 .c

Oct 11th, 2019
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. /*
  2.     Corrected code.
  3.  diamond star pattern with number of rows v1 .c
  4.  
  5.  If number_of_rows is even number, we have to add 1 to be odd number.
  6.  This is because diamond must be simetrical.
  7.  
  8. ----*-
  9. ---*-*-
  10. --*-*-*-
  11. -*-*-*-*-
  12. *-*-*-*-*-
  13. -*-*-*-*-
  14. --*-*-*-
  15. ---*-*-
  16. ----*-
  17.  
  18. row  spaces     *
  19.  
  20.  0.     4       1
  21.  1.     3       2
  22.  2.     2       3
  23.  3.     1       4
  24.  4.     0       5
  25.  
  26.  5.     1       4
  27.  6.     2       3
  28.  7.     3       2
  29.  8.     4       1
  30.  
  31.  
  32.     Upper half of diamond
  33.  
  34. For number of spaces in a row we have to find relation with r and nr, (nr/2 - r).
  35.  
  36. For number of "*-" in a row we have to find relation with r, (r+1).
  37.  
  38.     Lower half of diamond
  39.  
  40. For number of spaces in a row we have to find relation with r and nr, (r - nr/2).
  41.  
  42. For number of "*-" in a row we have to find relation with r and nr, (nr-r).
  43.  
  44.  
  45. If number_of_rows nr is even number, we increment it for 1, to be odd number,
  46. because diamond must be simetrical.
  47.  
  48. */
  49.  
  50. #include "stdio.h"
  51.  
  52. int main(void)
  53. {
  54. /*
  55.     r   row,
  56.     nr  number_of_rows,
  57.     ns  number_of_spaces,
  58.     na  number_of_asterisks
  59. */
  60.     int i, r, nr, ns, na;
  61.  
  62.     printf("\n Diamond of how much rows, nr = ");
  63.     scanf("%d", &nr);           // try 8, 9 or 50
  64.  
  65.     if(nr%2==0)                 // If number_of_rows nr is even number,
  66.         nr++;                   // we increment it for 1, to be odd number,
  67.                                 // because diamond must be simetrical.
  68.     printf("\n");               // print new row
  69.  
  70.     for (r=0; r<nr; r++)            // print rows one by one row
  71.     {
  72.         if(r<nr/2+1)                // Upper half of diamond
  73.         {
  74.             ns = nr/2-r;            // calculate ns and na
  75.             na = r+1;
  76.         }
  77.         else                        // Lower half of diamond
  78.         {
  79.             ns = r-nr/2;            // calculate ns and na
  80.             na = nr-r;
  81.         }
  82.  
  83.         for (i=0; i<ns; i++)        // print ns spaces "-"
  84.             printf("-");            // replace "-" with " "
  85.  
  86.         for (i=0; i<na; i++)        // print na "*-"
  87.             printf("*-");           // replace "*-" with "* "
  88.  
  89.         printf("\n");               // print new row
  90.     }
  91.  
  92.     return 0;                       // This is the simplest and most comprehensive code.
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement