I got some problems with my program here. There are lots of lacking in my code. Hope you can help me with this guys. I'm still a student. Here's the problem:

A program that prints figures of (*):
1. Displays a menu
[1] Figure 1
[2] Figure 2
[3] Figure 3
[4] Figure 4
[5] Figure 5
[6] Figure 6

Enter your choice:_

2. Input for no. of levels to be printed.
3. Use functions for menu and 6 functions for printing the different figures.

Here's my codes:

#include"stdio.h"
int menu();
void fig1(int a);
void main()
{
int choice;
choice=menu();
scanf("%d",&a);
fig1(a);
}

int menu()
{
int choice;
printf("[1] Figure 1\n");
printf("[2] Figure 2\n");
printf("[3] Figure 3\n");
printf("[4] Figure 4\n");
printf("[5] Figure 5\n");
printf("[6] Figure 6\n\n");
printf("Enter your choice:",&a);
scanf("%d",&choice);
return(choice);
}

void fig1(int a)
{
int x,y;
for(x=1;x<=a;x++)
{
for(y=1;y<=x;y++)
printf("*");
printf("\n");
}
getch();
}

MENU OUTPUT:
[1] Figure 1
[2] Figure 2
[3] Figure 3
[4] Figure 4
[5] Figure 5
[6] Figure 6

Enter your choice:_


OUTPUT EVERY FIGURE:
*
* *
* * *
* * * *
Figure 1


* * * *
* * *
* *
*
Figure 2


*
* *
* * *
* * * *
Figure 3


* * * *
* * *
* *
*
Figure 4


*
* *
* * *
* * * *
Figure 5


* * * *
* * *
* *
*
Figure 6

Figure 5 and 6 are Christmas tree structure. I can't seem to find a way how make it a Christmas tree.

First you need to determine where the center of the tree will be, then set an int variable to be that value. The variable count is just the number of stars that will be printed on the current row.

int main()
{
    int center = 10;
    int count = 1;
    for(int i = 0; i < 10; i++)
    {
        printf("%*s", center, " ");
        for(int j = 0; j < count*2; j++)
            printf("*");
        printf("\n");
        --center;
        count++;
    }
}

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks for replying Ancient Dragon. Here's my code again. What's wrong with my code? Take a look my menu and how to call the figures if I input a number in the "Enter your choice.

#include<stdio.h>
#include<conio.h>
void fig1();
void fig2();
void fig3();
void fig4();
void fig5();
void fig6();
void exit();
void main()
{
 int cho;
 printf("[1] Figure 1\n");
 printf("[2] Figure 2\n");
 printf("[3] Figure 3\n");
 printf("[4] Figure 4\n");
 printf("[5] Figure 5\n");
 printf("[6] Figure 6\n");
 printf("[7] Exit\n\n");
 printf("Enter your choice:");
 scanf("%d",&cho);
 if(cho==1)
 {
  fig1();
 }
 else if(cho==2)
 {
  fig2();
 }
 else if(cho==3)
 {
  fig3();
 }
 else if(cho==4)
 {
  fig4();
 }
 else if(cho==5)
 {
  fig5();
 }
 else if(cho==6)
 {
  fig6();
 }
 else if(cho==7)
 {
  exit();
 }

 fig1();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }
 
 fig2();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }

 fig3();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }

 fig4();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }

 fig5();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }

 fig6();
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  main();
 }
}

Edited 6 Years Ago by SHENGTON: n/a

Your functions are not really functions, since you put a semi-colon on the end of their first line, in your program.

I've fixed that for you, and made some important notes in this code, for you. The function main() should not be called, inside your program, as noted. Also, all your fig() functions are now drawing the same tree shape.

#include<stdio.h>
#include<conio.h>

//without the void's, your compiler may assume
//that an int is a parameter to the function
//Turbo C does that, among others.

void fig1(void);
void fig2(void);
void fig3(void);
void fig4(void);
void fig5(void);
void fig6(void);
//void exit(); exit is a C word, don't use 
//it for the name of a function.


int main()
{
 int cho, i;

 do {
   printf("[1] Figure 1\n");
   printf("[2] Figure 2\n");
   printf("[3] Figure 3\n");
   printf("[4] Figure 4\n");
   printf("[5] Figure 5\n");
   printf("[6] Figure 6\n");
   printf("[7] Exit\n\n");
   printf("Enter your choice:");
   scanf("%d",&cho);
   /* The next line isn't needed, here, if the user is careful
      Why do you want it? 
      Without it, if the user enters a letter, instead of a 
      number, the program enters an endless loop.
   */
   i = getchar(); //"pulls" the newline off the keyboard buffer
   if(cho==1)
   {
    fig1();
   }
   else if(cho==2)
   {
    fig2();
   }
   else if(cho==3)
   {
    fig3();
   }
   else if(cho==4)
   {
    fig4();
   }
   else if(cho==5)
   {
    fig5();
   }
   else if(cho==6)
   {
    fig6();
   }
  // else if(cho==7) { //handled by the do while loop

  }while(cho != 7);
  printf("\n\n\t\t\t    press enter when ready");
  i = getchar(); 
  ++i;
  return 0;
 }

void fig1(void)  //No semi-colon on this line!
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  /*If fig1() is a function (a real one), then program execution
    will return to the calling function. You don't need to
    call it, to make that happen. 

    For main(), you never call it again, anyway.
    main(); <-- no
  */
 }
 
 void fig2(void)
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  //main();
 }

 void fig3(void)
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  //main();
 }

 void fig4(void)
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  //main();
 }

void fig5(void)
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  //main();
 }

 void fig6(void)
 {
  int i,j;
  clrscr();
  for(i=1;i<=5;i++)
  {
   for(j=5;j>=i;j--)
   {
    printf("* ");
   }
   printf("\n\n");
  }
  //main(); don't call main(). That's for the program, itself.
 }

Hello Adak,

Thanks for helping me with this. Here's my code again. I got a little problem with figure 3-6 on how to do it.

[IMG]http://i168.photobucket.com/albums/u162/SHENGTON/untitled-2.jpg[/IMG]

#include<stdio.h>
#include<conio.h>
void fig1(void);
void fig2(void);
void fig3(void);
void fig4(void);
void fig5(void);
void fig6(void);
int main()
{
 int cho, i;
 do{
  printf("[1] Figure 1\n");
  printf("[2] Figure 2\n");
  printf("[3] Figure 3\n");
  printf("[4] Figure 4\n");
  printf("[5] Figure 5\n");
  printf("[6] Figure 6\n");
  printf("[7] Exit\n\n");
  printf("Enter your choice:");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   fig1();
  }
  else if(cho==2)
  {
   fig2();
  }
  else if(cho==3)
  {
   fig3();
  }
  else if(cho==4)
  {
   fig4();
  }
  else if(cho==5)
  {
   fig5();
  }
  else if(cho==6)
  {
   fig6();
  }
 }while(cho != 7);
  printf("\n\n\t\t\t    press enter when ready");
  i = getchar();
  ++i;
  clrscr();
  return 0;
}

void fig1(void)
{
 int i,j,k;
 clrscr();
 printf("Input a number:");
 scanf("%d",&k);
 for(i=k;i>=1;i--)
 {
  for(j=k;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}

void fig2(void)
{
 int i,j,k;
 clrscr();
 printf("Input a number:");
 scanf("%d",&k);
 clrscr();
 for(i=1;i<=k;i++)
 {
  for(j=k;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}

void fig3(void)
{
 int i,j,k;
 clrscr();
 printf("Input a number:");
 scanf("%d",&k);
 for(i=1;i<=k;i++)
 {
  for(j=k;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}

void fig4(void)
{
 int i,j;
 clrscr();
 for(i=1;i<=5;i++)
 {
  for(j=5;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}

void fig5(void)
{
 int i,j;
 clrscr();
 for(i=1;i<=5;i++)
 {
  for(j=5;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}
void fig6(void)
{
 int i,j;
 clrscr();
 for(i=1;i<=5;i++)
 {
  for(j=5;j>=i;j--)
  {
   printf("* ");
  }
  printf("\n\n");
 }
}

Let's look at figure 3:

*
   **
  ***
 ****
*****

And let's say we have no left side margin, so the * that's closest to the left side, is actually all the way to to the left side.

How would you make this by hand? Probably, you'd say "I've got to have a maximum of 5 *'s, so what I'll do for the first row is press the space bar 4 times, and then print one *.

So mathematically you're saying:
NumberOfSpaces = (Greatest number of *'s in any row - number of *'s in this row.)

Which in this case, (5 *'s), means:

spaces = 5 - 1

for the first row, and:

spaces = 5 - 5

for the last row in figure 3.

That's how I do this by hand, and probably you do about the same. So have your program do it the same way. You have just three variables to consider:

1) spaces before the first *
2) the number of *'s to print on the line.
3) the largest number of *'s to print on any line

That's how you figure out the *'s to print for figure 3 and 4. Do that, and see what you can do with figure 5 and 6 (the Christmas tree shape figures).

Edited 6 Years Ago by Adak: n/a

Hello Adak,

You mean I have to create another "for" loop for spaces? Under?

No nested for loop is needed for figure 1-4. There is a way to do this with nested for loops, but why use more loops, if it doesn't help make the code simpler, faster, or more intuitive?

You need 1 for loop, which counts from 0 up to the largest number of *'s in any of your rows. That's number 3) in my post, above. (This is true for figures 1,2,3 and 4, but not 5 or 6).

Inside that loop, you need an if statement to tell your program whether it should print a space or a *, on this time through the loop.

And that's all you need for figure 1,2,3, and 4. For figure 5 and 6, you need to:

1) print half of the spaces you calculate should be printed for that row
2) print the *'s
3) print a newline - you don't actually have to print the other half of the spaces

Edited 6 Years Ago by Adak: n/a

Hello Adak,

Finally I figured it out. Here's the code:

#include<stdio.h>
#include<conio.h>
void fig1(void);
void fig2(void);
void fig3(void);
void fig4(void);
void fig5(void);
void fig6(void);
int main()
{
 int cho, i;
 do{
  printf("[1] Figure 1\n");
  printf("[2] Figure 2\n");
  printf("[3] Figure 3\n");
  printf("[4] Figure 4\n");
  printf("[5] Figure 5\n");
  printf("[6] Figure 6\n");
  printf("[7] Exit\n\n");
  printf("Enter your choice:");
  scanf("%d",&cho);
  i = getchar();
  if(cho==1)
  {
   fig1();
  }
  else if(cho==2)
  {
   fig2();
  }
  else if(cho==3)
  {
   fig3();
  }
  else if(cho==4)
  {
   fig4();
  }
  else if(cho==5)
  {
   fig5();
  }
  else if(cho==6)
  {
   fig6();
  }
 }while(cho != 7);
  printf("\n\n\t\t\t    press enter when ready");
  i = getchar();
  ++i;
  clrscr();
  return 0;
}

void fig1(void)
{
 int i,j,k;
 clrscr();
 printf("Input a number of lines:");
 scanf("%d",&k);
 for(i=k;i>=1;i--)
 {
  for(j=k;j>=i;j--)
  {
   printf("*");
  }
  printf("\n");
  j--;
 }
}

void fig2(void)
{
 int i,j,k;
 clrscr();
 printf("Input a number of lines:");
 scanf("%d",&k);
 for(i=1;i<=k;i++)
 {
  for(j=k;j>=i;j--)
  {
   printf("*");
  }
  printf("\n");
 }
}

void fig3(void)
{
 int i,j,k,l,m;
 clrscr();
 printf("Input the number of lines:");
 scanf("%d",&k);
 m=k-1;
 for(l=0;l<k;l++)
 {
  for(i=m;i>0;i--)
  {
   printf(" ");
  }
  for(j=0;j<=l;j++)
  {
   printf("*");
  }
  printf("\n");
  m--;
 }
}

void fig4(void)
{
 int i,j,k,l,m;
 clrscr();
 printf("Input the number of lines:");
 scanf("%d",&k);
 m=k;
 for(l=0;l<k;l++)
 {
  for(i=0;i<=l;i++)
  {
   printf(" ");
  }
  for(j=m;j>0;j--)
  {
   printf("*");
  }
  printf("\n");
  m--;
 }
}

void fig5(void)
{
 int i,j,k,l,m;
 clrscr();
 printf("Input the number of lines:");
 scanf("%d",&k);
 m=k-1;
 for(l=0;l<k;l++)
 {
  for(i=m;i>0;i--)
  {
   printf(" ");
  }
  for(j=0;j<=l;j++)
  {
   printf("* ");
  }
  printf("\n");
  m--;
 }
}

void fig6(void)
{
 int i,j,k,l,m;
 clrscr();
 printf("Input the number of lines:");
 scanf("%d",&k);
 m=k;
 for(l=0;l<k;l++)
 {
  for(i=0;i<=l;i++)
  {
   printf(" ");
  }
  for(j=m;j>0;j--)
  {
   printf("* ");
  }
  printf("\n");
  m--;
 }
}

Thanks for helping me out.

pls help me to creat a program and its code...
i need it dis week

Edited 6 Years Ago by krian11: n/a

im having problems with my turbo C.. the error says that "unable to open include file stdio.h,conio.h" can anyone help!!!1

Edited 5 Years Ago by alreykjohn: n/a

im having problems with my turbo C.. the error says that "unable to open include file stdio.h,conio.h" can anyone help!!!1

i cant actually help.. but when i try to compile my work... the same appears..

In your TC directory is a file named readme or readme.txt (forget which), and it explains how to install the software.

What's happening with this error, is that the compiler can't find these header files. Read the readme file, and set your directory option for the compiler, under the "Options" >> compiler (iirc), headings of the IDE.

Use <stdio.h> with the brackets, if you are using the header directory you have in the compiler listing. Use "stdio.h" with double quotes instead, if you have them in a different sub directory.

IIRC, the directories should go:

C 
-- TC 
---   -- BIN, 
         headers
         examples
         etc.

If that doesn't work, please start up a new thread - this one is old, and marked as solved already. It won't get a lot of attention.

This question has already been answered. Start a new discussion instead.