Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- diamond_paterns_v1.c
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // return current char to print diamond, ' ' or '*'
- char diamond_of_chars(int x, int y, int h) {
- if(x < 0) x = -x;
- if(y < 0) y = -y;
- int value = h - x - y;
- return (value <= 0) ? ' ' : '*'; // diamond of stars
- }
- // return current number to print diamond
- char diamond_of_numbers(int x, int y, int h) {
- if(x < 0) x = -x;
- if(y < 0) y = -y;
- int value = h - x - y;
- return (value <= 0) ? ' ' : '0' + (char)value; // diamond of numbers
- }
- // return current char to print diamond, ' ' or '*'
- char empty_diamond_surrounded_by_stars(int x, int y, int h) {
- if(x < 0) x = -x;
- if(y < 0) y = -y;
- int value = -h + x + y;
- return (value <= 0) ? ' ' : '*'; // an empty diamond surrounded by stars
- }
- // return current number to print diamond
- char empty_diamond_surrounded_by_numbers(int x, int y, int h) {
- if(x < 0) x = -x;
- if(y < 0) y = -y;
- int value = -h + x + y;
- return (value <= 0) ? ' ' : '0' + (char)value; // an empty diamond surrounded by numbers
- }
- int main(void){
- int x, y, h=4;
- /*
- y number of current row
- x number of chars in current row
- h number of rows in upper triangle
- */
- /*
- *
- ***
- *****
- *******
- *****
- ***
- *
- */
- printf("\n Diamond of chars \n");
- for(y = -h; y <= h; y++) {
- for(x = -h; x <= h; x++)
- putchar(diamond_of_chars(x, y, h));
- putchar('\n');
- }
- /*
- 1
- 121
- 12321
- 1234321
- 12321
- 121
- 1
- */
- printf("\n Diamond of numbers \n");
- for(y = -h; y <= h; y++) {
- for(x = -h; x <= h; x++)
- putchar(diamond_of_numbers(x, y, h));
- putchar('\n');
- }
- /*
- **** ****
- *** ***
- ** **
- * *
- * *
- ** **
- *** ***
- **** ****
- */
- printf("\n Empty diamond surrounded by stars \n\n");
- for(y = -h; y <= h; y++) {
- for(x = -h; x <= h; x++)
- putchar(empty_diamond_surrounded_by_stars(x, y, h));
- putchar('\n');
- }
- /*
- 4321 1234
- 321 123
- 21 12
- 1 1
- 1 1
- 21 12
- 321 123
- 4321 1234
- */
- printf("\n Empty diamond surrounded by numbers \n\n");
- for(y = -h; y <= h; y++) {
- for(x = -h; x <= h; x++)
- putchar(empty_diamond_surrounded_by_numbers(x, y, h));
- putchar('\n');
- }
- printf("\n\n");
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement