Skip to content

Instantly share code, notes, and snippets.

@jroesch
Created August 31, 2012 21:07
Show Gist options
  • Save jroesch/3558995 to your computer and use it in GitHub Desktop.
Save jroesch/3558995 to your computer and use it in GitHub Desktop.
C Version
int main(){
int inputInt,oddRslt;
char awsr;
int checkNum(int inputInt);
int printDmd(int inputInt);
int repeat(char awsr);
int again=1;
/*main loop, loops entire program, ends on exit */
while(again != 0){
/*takes input*/
do{
printf("enter ODD size > 0:");
scanf("%i",&inputInt);
while (getchar() != '\n');
oddRslt=checkNum(inputInt);
} while(oddRslt != 1);
/*calls printDmd*/
printDmd(inputInt);
/*more prompt*/
printf("more?\n");
/*more answer*/
awsr = getchar();
while (getchar() != '\n');
again=repeat(awsr);
inputInt=0;
}
/* if again == 0 termiantes) */
printf("Goodbye!\n");
return 0;
}
/* number check, if odd,even,negative, or zero*/
int checkNum(int inputInt){
int isOdd, oddChk;
if(inputInt==0){
printf("Zero or noninteger, Please enter an odd integer.\n");
}
else if(inputInt < 0){
printf("Not a positive odd integer.\n");
isOdd=0;
return isOdd;
}
else{
oddChk = inputInt % 2;
if(oddChk == 1){
isOdd=1;
return isOdd;
}
else{
printf("You entered an even integer,"
" please enter an odd intger.\n");
isOdd=0;
return isOdd;
}
}
}
/*prints diamond*/
int printDmd(int inputInt){
int space,t,s,a,n,askT,askC;
n=inputInt;
space=(n/2);
askC=1;
askT=1;
for(t=1;t<=n;t++){
if(space<0)
s=space*-1;
else s=space;
for(s;s>0;s--){
printf(" ");
}
for(a=1;a<=askT;a++){
printf("*");
}
if(askC<((n/2)+1)){
askT+=2;
askC++;
}
else if(askC>=((n/2+1))){
askT-=2;
}
printf("\n");
if(space<=(n/2))
space--;
}
return 1;
}
/*evlautes response to more and sets again's value*/
int repeat(char awsr){
int again;
if((awsr=='Y') || (awsr== 'y'))
again=1;
else
again=0;
return again;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment