Thread: Line of diamonds problem

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Line of diamonds problem

    Hiya I am trying to make a line of diamunds appear across the screen to whatever the user enters under the value of eight.

    It compiles fine but they keep arreaing down the screen and not across it. I was wondering is my function call the problem?

    Code:
    #include <stdio.h>
    
    /*function prototype*/
    void printDiamond ( void );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       int counter;
       int amount;
    
       printf("How many diamonds: ");
       scanf("%d", &amount);
    
       while ( amount > 8 )
       {
          puts("\nI cannot print more than eight diamonds!\n");
          printf("Please re-enter amount: ");
          scanf("%d", &amount);
       }
    
       for ( counter = 1; counter <= amount; counter++ )
       {
          printDiamond();
          printf("\n\n");
       }
    
       getch(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    /*function to print a diamond shape*/
    void printDiamond ( void )
    {
       printf(" *\n***\n *");
    }
    Double Helix STL

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do you want to print something like:
    Code:
     *  *
    ******
     *  *
    Then - see what is in the first line - you cannot finish printing 1 diamond and start another - you should do all of them at the same time...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks Vart
    Double Helix STL

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's nothing wrong with this logic:

    Code:
    for (counter = 1; counter <= amount; counter++ )
       ...
    But it's not the idiom that I think you should get comfortable with in C. This is the idiom of choice:
    Code:
    for (counter = 0; counter < amount; counter++)
      ...
    Which has the advantage of being automatically right to handle 0 based arrays, AND making it more certain that the double char operator "<=" won't wind up causing a bug because you forgot the = char, or you've overrun the upper limit of your array.

    For a C programmer, I would strongly recommend branding the red example, deep into your consciousness and your heart - if not your very soul. Know it in your sleep, dream it in your dreams, use it in your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. casting problem with strcmp command line arg
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 04-15-2006, 03:40 PM
  3. read to end of line problem
    By one1082 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2003, 04:30 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  5. problem passing an option as command line argument
    By papous in forum C Programming
    Replies: 3
    Last Post: 11-22-2001, 06:12 PM