Thread: Program to make stars mostly done, need help?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Question Program to make stars mostly done, need help?

    Ok, I am new to this board so if I have gone out of line or done something by accident, I will apologize in advance. I want to make a program that generates a number of lines with stars. Only Odd numbers though. The user is asked to input a number and the output places that many lines in out of stars. EX>.

    Input number: 5;

    *

    ***

    *****

    ***

    *

    It doesn't put the line 2 with 2 stars and line 4 with 4 stars. Number being inputted is number of stars on middle line. Eg. 7 would have 7 stars.

    So far this is what I have...


    Code:
    
    #include <stdio.h>
    
    int main (void)
    
    {
    
    int num, space, spaces, line;
    
    printf("Please enter an Odd number of lines in the diagram: ");
    scanf("%d", &num);
    
    if((num <= 0)||(num%2 == 0))
    printf("\nPlease input a positive Odd Integer");
    
    else{
    
    space = num;
    
    for (int i=1; i<=num-2; i++)
            {
    
            for (int j=1; j<=space; j++)
            printf(" ");
    
            for (int j=1; j<=2*i-1; j++)
            printf("*");
            printf("\n");
            space--;
            }
           }
    return 0;
    }

    Thank you in advance for reading this post.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by patso View Post
    Ok, I am new to this board so if I have gone out of line or done something by accident, I will apologize in advance. I want to make a program that generates a number of lines with stars. Only Odd numbers though. The user is asked to input a number and the output places that many lines in out of stars. EX>.

    Input number: 5;

    *

    ***

    *****

    ***

    *

    It doesn't put the line 2 with 2 stars and line 4 with 4 stars. Number being inputted is number of stars on middle line. Eg. 7 would have 7 stars.

    So far this is what I have...


    Code:
    
    #include <stdio.h>
    
    int main (void)
    
    {
    
    int num, space, spaces, line;
    
    printf("Please enter an Odd number of lines in the diagram: ");
    scanf("%d", &num);
    
    if((num <= 0)||(num%2 == 0))
    printf("\nPlease input a positive Odd Integer");
    
    else{
    
    space = num;   //??????????????????????
    
    How about a nice do while loop for this? 
    
    do   {
       printf("Please enter an Odd positive number...");
       scanf("%d", &num);
    
       garbage = getchar();  //get's rid of the '\n' in the input stream.
    }while(num < 1 || num %2 == 0);
    
    
    
    for (int i=1; i<=num-2; i++)
            {
    
            for (int j=1; j<=space; j++)
            printf(" ");
    
            for (int j=1; j<=2*i-1; j++)
            printf("*");
            printf("\n");
            space--;
            }
           }
    return 0;
    }

    Thank you in advance for reading this post.
    Are you compiling this in C? Looks like C++.

    So what's your output look like?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    using SSH to compile, it is in school so I am pretty sure it is C. I use pico at school.

    My output shows

    input number: 5;

    *

    ***

    *****

    But as soon as I go past 5 it gets to be a large number of stars for 7 or 9. I also don't know how to get it to go from the 5 stars to the 3 to the 1.

    *****

    ***

    *

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    The below code puts out what you need, but I suck at C and for loops give me a headache, but you should be able to rewrite it to whatever is required. Not formatted either -

    Input #7 outputs;
    *
    ***
    *****
    *******
    *****
    ***
    *
    Code:
    else{
    
    for (int i=0; i<=num; i++)
      {
    
          if(i%2==0){
             int k=0;
             
            while(k<=i){
              printf("*");
              k++;
             }
            }
             if(i<num){
               printf("\n");
               }
             }
             
    for (int l=num-2; l>=0; l--)
       {
    
          if(l%2==0){
             int k=0;
             
            while(k<=l){ 
              printf("*");
              k++;
             }
            }
               if(l<=num){
                 printf("\n");
                }
               }     
              }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Talking Thread finished

    Awesome, thank you so much. You were extremely helpful. This is exactly what I needed, and it is easy and clear so I can read it. Oldman47 you were an excellent help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-17-2008, 11:38 AM
  2. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  3. A doubt on how to make a program
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 08-20-2004, 11:16 AM
  4. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM
  5. any suggestions on how to make this program smaller?
    By bajan_elf in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 03:24 AM