The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 398 Weeks Ago, 5 Days Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
I have a problem with my code and i have a question. When i hit the 6 option it doesnt exit the program.
Here is my code:
#include<stdio.h>
#include<stdlib.h>
//Local Declarations
void patternOne(int k);
void patternTwo(int k);
void patternThree(int k);
void patternFour(int k);
void patternFive(int k);
int getOption();
int getSize();
//Main Function
int main(void)
{
//Variables
int opt;
int size;
while(1)
{
//Display the menu
printf("M A I N M E N U\n");
printf("----------------\n");
printf("1. Pattern One\n");
printf("2. Pattern Two\n");
printf("3. Pattern Three\n");
printf("4. Pattern Four\n");
printf("5. Pattern Five\n");
printf("6. Quit\n");
//Switch case to display chosen option.
opt=getOption();
size=getSize();
switch(opt)
{
case 1:
patternOne(size);
break;
case 2:
patternTwo(size);
break;
case 3:
patternThree(size);
break;
case 4:
patternFour(size);
break;
case 5:
patternFive(size);
break;
case 6:
printf("Good Bye!\n");
exit(0);
break;
default :
break;
}
}
return 0;
}
//Function for PatternOne using a WHILE loop.
void patternOne(int k)
{
int i=0, j=0;
while(i<k)
{
j=0;
while(j<k)
{
if(i==j)
printf("%X",k);
else
printf("*");
j++;
}
printf("\n");
i++;
}
}
//Function for PatternTwo using a FOR loop.
void patternTwo(int k)
{
int i=0,j=0;
for(i=0; i<k; i++)
{
for(j=0; j<k; j++)
{
if(j==(k-i-1))
printf("%X",k);
else
printf("*");
}
printf("\n");
}
}
//Function for PatternThree using a DO....WHILE loop.
void patternThree(int k)
{
int i=0,j=0;
do
{
j=0;
do
{
if((k-j-1)<i)
printf("%X",k);
else
printf("*");
j++;
}
while (j<k);
printf("\n");
i++;
}
while(i<k);
}
//Function for PatternFour using a DO...WHILE loop.
void patternFour(int k)
{
int i=0,j=0;
do
{
j=0;
do {
if(j<i)
printf("%X",k);
else
printf("*");
j++;
}
while(j<k);
printf("\n");
i++;
}
while(i<k);
}
//Function for PatternFive using Recursion.
void patternFive(int k)
{
int i;
for(i=k; i>=1; i--)
printf("%X",i);
printf("\n");
if(k>1)
patternFive(k-1);
}
//Function of getOption to store option given by user
int getOption()
{
int c;
while(1)
{
printf("Choose an Option(Between 1 and 6):");
scanf("%d",&c);
if(c>=1 && c<=6)
return c;
else
printf("Your option is incorrect. Please try again.\n");
}
}
//Function of getSize to store the size input by user.
int getSize()
{
int k;
while(1)
{
printf("Choose a Pattern Size(Between 2 and 15): ");
scanf("%d",&k);
if(k>=2 && k<=15)
return k;
else
printf("Your pattern size is incorrect. Please try again.\n");
}
}
any kind of help will do but please dont do it for me;
Hel-----------lo -----------Sir-----------/Ma-----------dam-----------Tha-----------nk -----------You----------- fo-----------r u-----------sin-----------g o-----------ur -----------web-----------sit-----------e a-----------nd -----------acq-----------uis-----------iti-----------on -----------of -----------my -----------sol-----------uti-----------on.-----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- ca-----------tch-----------