Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Corrected code.
- diamond star pattern with number of rows v1 .c
- If number_of_rows is even number, we have to add 1 to be odd number.
- This is because diamond must be simetrical.
- ----*-
- ---*-*-
- --*-*-*-
- -*-*-*-*-
- *-*-*-*-*-
- -*-*-*-*-
- --*-*-*-
- ---*-*-
- ----*-
- row spaces *
- 0. 4 1
- 1. 3 2
- 2. 2 3
- 3. 1 4
- 4. 0 5
- 5. 1 4
- 6. 2 3
- 7. 3 2
- 8. 4 1
- Upper half of diamond
- For number of spaces in a row we have to find relation with r and nr, (nr/2 - r).
- For number of "*-" in a row we have to find relation with r, (r+1).
- Lower half of diamond
- For number of spaces in a row we have to find relation with r and nr, (r - nr/2).
- For number of "*-" in a row we have to find relation with r and nr, (nr-r).
- If number_of_rows nr is even number, we increment it for 1, to be odd number,
- because diamond must be simetrical.
- */
- #include "stdio.h"
- int main(void)
- {
- /*
- r row,
- nr number_of_rows,
- ns number_of_spaces,
- na number_of_asterisks
- */
- int i, r, nr, ns, na;
- printf("\n Diamond of how much rows, nr = ");
- scanf("%d", &nr); // try 8, 9 or 50
- if(nr%2==0) // If number_of_rows nr is even number,
- nr++; // we increment it for 1, to be odd number,
- // because diamond must be simetrical.
- printf("\n"); // print new row
- for (r=0; r<nr; r++) // print rows one by one row
- {
- if(r<nr/2+1) // Upper half of diamond
- {
- ns = nr/2-r; // calculate ns and na
- na = r+1;
- }
- else // Lower half of diamond
- {
- ns = r-nr/2; // calculate ns and na
- na = nr-r;
- }
- for (i=0; i<ns; i++) // print ns spaces "-"
- printf("-"); // replace "-" with " "
- for (i=0; i<na; i++) // print na "*-"
- printf("*-"); // replace "*-" with "* "
- printf("\n"); // print new row
- }
- return 0; // This is the simplest and most comprehensive code.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement