Skip to content

Instantly share code, notes, and snippets.

@sinannar
Created February 12, 2012 17:14
Show Gist options
  • Save sinannar/1809718 to your computer and use it in GitHub Desktop.
Save sinannar/1809718 to your computer and use it in GitHub Desktop.
draw a diamond with hole
/*
* HW03_091044005_PPART_1.c
*
* CREATED BY :Sinan NAR
* CREATING DATE :25/03/2011
*
* DESCRIPTION
* This program get an odd number from user
* And check this number that is between 1-21 and is it an odd number
* if these statements are okey,
* program draw a diamond with stars and middle of diamond should be an square with spaces
*
* NOTE:
* when the program writing;we seperate the shape as 4 part cause of the space shape in middle
* fist part of the shape we call it PART A and there is no space in the middle of it
* middle of the shape,i mean square part with space that include,we seperate it again as PART B and PART c
* PART B is the bigger part and PART C is the smaller part of the middle shape with space square
* fourth part of the shape we call it PART D and there is no space in the middle of it
* when we seperate this whole shape as four part,we have some problem with spaces but aour algoritm
* could solve this problem. we have some loop body in PART A and PART D for writing more space
* it makes our shape what you want from us
*
*
* REFERENCES:
* [1] 102_HW_03.pdf
*/
#include <stdio.h>
void draw_diamond(int);
void error(void);
void main()
{
int sekil;
printf("\n\tEnter an odd number \n\tThat should be between 1 and 21 \n\tFor drawing diamond\n>");
scanf("%d",&sekil);
while( sekil<1 || sekil>21 || (sekil%2)==0 )
{
error();
scanf("%d",&sekil);
}
//algoritmamiz 1 degeri icin haric diger butun degerler icin calistigi icin bunu yaptik
if(sekil==1)
{
printf(" * ");
}
else
{
draw_diamond(sekil);
}
}
void draw_diamond(int m)
{
int i,g,j,x,y,z,t,l,n,b,c,h;
int kare;
x=m-2;
y=(x+1)/2;
z=(x-1)/2;
/*** KISIM A ***/
for ( i=0;i<=(m+1)/2;i++ )
{
for(b=0;b<(m-1)/2;++b)
{
printf(" "); //dorde ayirdigimiz icin kaynaklanan baslangictaki kayip boslugu yazdiracak
}
for ( j=0;j<=((m+1)/2)+1-i;j++) //norma olmasi gereken boslugu yazdiracak
{
printf(" ");
}
for ( g=0;g<=2*i-2;g++) //yildizi yazdiracak
{
printf(" *");
}
printf("\n");
}
/*** KISIM B ***/
for(t=0,h=y;t<y;++t,--h) //i satir numarasi
{
for( l=h;l>=0;--l )//boslugu yazdiracak
{
printf(" ");
}
for (n=0;n<=t+1;++n ) //yildizlari yazdiracak
{
printf(" *");
}
for (kare=0;kare<x;++kare) //n-2 tane bosluk yazdiracak
{
printf(" "); //icerideki kareyi olusturan bosluklar
}
for( n=0;n<=t+1;++n ) //yildizlari yazdiracak
{
printf(" *");
}
printf("\n");
}
/*** KISIM C ***/
for(t=z,c=1;t>0;--t,++c)
{
for (l=0;l<=c+1;++l ) //boslugu yazdiracak
{
printf(" ");
}
for(n=0;n<t+1;++n) //yildizlari yazdiracak
{
printf(" *");
}
for (kare=0;kare<x;++kare) //boslugu yazdiracak
{
printf(" ");
}
for (n=0;n<t+1;++n) //yildizi yazdiracak
{
printf(" *");
}
printf("\n");
}
/*** KISIM D ***/
for(i=1;i<=(m+1)/2;i++)
{
for(b=0;b<(m-1)/2;++b)
{
printf(" "); //sekli dorde bolmemizden dolayi olsuan kaymayi onlemek icin bosluk yazdiracak
}
for( j=0;j<=i;j++) //olmasi gereken boslugu yazdiracak
{
printf(" ");
}
for(g=i*2;g<=(m+1);g++) //yildizi yazdiracak
{
printf(" *");
}
printf("\n");
}
}
void error(void)
{
printf("AN ERROR OCCURRED\n");
printf("It can be caused by on of\n");
printf("these fallowing statements\n");
printf("------------------------------------\n");
printf("| YOU ENTERED |\n");
printf("| 1. not an odd number |\n");
printf("| 2. a number that bigger than 21 |\n");
printf("| 3. a number that smaller than 1 |\n");
printf("------------------------------------\n");
printf("Please again enter\n");
printf("an odd number \n");
printf("that should be between 1-21\n");
printf("\n>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment