Like Tree1Likes
  • 1 Post By Adak

C Diamond

This is a discussion on C Diamond within the C Programming forums, part of the General Programming Boards category; This program is supposed is to take the input of a odd integer and print a symmetrical diamond. The thing ...

  1. #1
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18

    Question C Diamond

    This program is supposed is to take the input of a odd integer and print a symmetrical diamond. The thing is, is that I've created a diamond but it isn't symmetrical. If the user enters '5' it should be five *s on the third line not six. Can give me any suggestions. I have a general understanding of nested loops as a whole nested for loops are the worse for me. Thanks in advance.
    Code:
    #include <stdio.h>
    
    int main () 
    {
        
        int Height;
        int x, y, z;
        printf("What is the Height of the Diamond:\n");
        scanf("%d", &Height);
        for (x=(Height-1); x>0; --x)        
        {
            printf("%*s",x,"");
            for (y=x; y<Height; ++y)                
            {
                
                
                for (z=(Height-1); z>y; z--) 
                {
                    printf("*");
                }
                
            }
            printf("\n");                    
            
        }
        
        // Beginning of bottom half of the diamond...
        
        
        for (x=(Height-1)/2; x<Height; ++x) 
        {
            printf("%*s",x,"");
            for (y=x; y<=Height; y++) 
            {
                for (z=(Height-1); z>y; z--) 
                {
                    printf("*");
                }
                
            }
            
            printf("\n");
            
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use just two (not three) nested for loops for each half of the diamond pattern. Get the top half done, THEN start working on the lower half.

    Print nothing in the outer for loop - that's just for getting the row number and setting up the inner for loop - where ALL the printing is done.

    Use one or more if statements inside the inner for loop.

    Personally, I do these working from 0 to N, working upward, (incrementing), never decrementing. That just seems confusing to me. (If you like it, fine).

    Isn't the height of the diamond, the WHOLE height of the diamond? Seems like drawing the height of the diamond, and drawing half the height of the diamond, is going to give you 150% of what you should be drawing, yes?

    Variable names are important - please don't call your row variable X, since X is used for the horizontal column variable in all the gotoxy(x,y) statements, (and math and other functions in programming).

    Maybe row, rows, or rowNum?

    Same with y - it's confusing when you use it for the column variable, since it's always been used for the vertical (in math and programming).
    Maybe col or cols, or colNum?

    Sit down with pen and paper and draw this out a few times, by hand. SLOWLY. BECOME the tip of the pen. Think "what is the row number, and what is the column number", for every time you move the tip of the pen.

    If you concentrate on it, and BECOME the tip of the pen, you will see the patterns you follow, to draw this - and those patterns will also form the logic for your program.

    Try that, and see if that doesn't help.

  3. #3
    Registered User C_Nik's Avatar
    Join Date
    Dec 2011
    Location
    Planet Earth
    Posts
    18
    Thanks for the advice. I appreciate it a lot. I sat down and drew it out a couple of times and I see the pattern. The stars increase by 1+ each Col Number, but I can't figure out how to get the inner loop to read the Col Numbers so it can increment the row correctly and print that number of stars.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by C_Nik View Post
    Thanks for the advice. I appreciate it a lot. I sat down and drew it out a couple of times and I see the pattern. The stars increase by 1+ each Col Number, but I can't figure out how to get the inner loop to read the Col Numbers so it can increment the row correctly and print that number of stars.
    You're not there yet - get INTO it - you are now the cursor on your monitor or printer. You ONLY print across a single row of text, at any time. You NEVER back up, you NEVER move down to another row without having printed out a newline or running out of space.

    Ask yourself : "On the 1st row, how many stars? (should you print)"

    "On the 2nd row, how many stars?"

    "On the 3rd row, how many stars?"

    C'mon, work it out!
    C_Nik likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me with this diamond problem
    By 843 in forum C Programming
    Replies: 5
    Last Post: 10-17-2010, 02:35 AM
  2. Diamond diplay
    By Chiefsmokin in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2010, 04:45 AM
  3. Diamond
    By C++Nerd in forum C++ Programming
    Replies: 1
    Last Post: 10-04-2002, 04:36 AM
  4. a diamond
    By wagman in forum C Programming
    Replies: 5
    Last Post: 10-04-2001, 03:48 PM
  5. drawing a diamond
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2001, 08:42 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21