Results 1 to 8 of 8

Thread: How to make a diamond in C with nested for loops

  1. #1
    Join Date
    Jun 2007
    Location
    San Juan,Puerto Rico
    Beans
    110
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    How to make a diamond in C with nested for loops

    Hi this is a solution to a common programming exercise where you have to make a program that makes diamonds using nested for loops. This is only one way you can do it. There are many ither ways to do it




    #include <stdio.h>
    int main()
    {
    int i,j,k;

    printf("Select Size \n");
    scanf("%d",&k);


    for(i=1; i<=k; i++)

    {

    for(j=1; j<=k-i; j++)

    {
    printf(" ");
    }

    for(j=1; j<=2*i-1; j++)

    {
    printf("*");
    }

    printf("\n");

    }

    for(i=1; i<k; i++)

    {

    for(j=1; j<=i; j++)

    {
    printf(" ");
    }

    for(j=2*k-2*i-1; j>=1; j--)

    {
    printf("*");
    }

    printf("\n");

    }

    return(0);

    }
    Attached Images Attached Images

  2. #2
    Join Date
    May 2007
    Location
    Sweden
    Beans
    84
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: How to make a diamond in C with nested for loops

    And?

    Btw, Always use [ code ] taggs when posting code.

  3. #3
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Beans
    366
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: How to make a diamond in C with nested for loops

    That's nice.

  4. #4
    Join Date
    Jun 2007
    Location
    /proc
    Beans
    329
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: How to make a diamond in C with nested for loops

    Cute
    Those that can,do.Those that can't,complain!

    Your right to speak doesn't include our obligation to take you seriously

  5. #5
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,447

    Re: How to make a diamond in C with nested for loops

    It's also a typical homework assignment, so it would probably be better form not to spread code around, especially considering that I'm afraid you will not be getting any mad props for this yet..
    LambdaGrok. | #ubuntu-programming on FreeNode

  6. #6
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to make a diamond in C with nested for loops

    I had to do something similar ... but in assembly
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  7. #7
    Join Date
    Dec 2006
    Beans
    28

    Re: How to make a diamond in C with nested for loops

    Here's a more elegant, but equally long example. It fixes a few problems such as making the number of vertical and horizontal asterisks equal to the actual size given. Also note that an even size yields an even number of stars on every line.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE_LEN 3
    
    int get_size()
    {
        char input_str[SIZE_LEN];
        
        printf("What size diamond? ");
        fgets(input_str, SIZE_LEN, stdin);
        printf("\n");
        return atoi(input_str);
    }
    
    void print_chars(int n_times, char c)
    {
        int i;
        for (i=0; i<n_times; i++)
        {
    	printf("%c", c);
        }
    }
    
    int main()
    {
        int i;
        int odd=0, even=0;
        int size;
    
        int n_stars=1;
        int reverse=0;
        
        size = get_size();
        
        if (size%2!=0)
    	odd=1;
        else
    	even=1;
        
        for (i=0; i<size; i++)
        {
    	print_chars(size/2-n_stars+odd, ' ');
    	print_chars(n_stars*2-odd, '*');
    	print_chars(1, '\n');
    	if (!reverse)
    	    n_stars+=1;
    	else
    	    n_stars-=1;
    	if (n_stars>size/2-even)
    	    reverse=1;
    	else if (n_stars<=0)
    	    break;
        }
        return 0;
    }

  8. #8
    Join Date
    Feb 2007
    Beans
    23,060
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: How to make a diamond in C with nested for loops


    From the Ubuntu Forums Code of Conduct.
    If a post is older than a year or so and hasn't had a new reply in that time, instead of replying to it, create a new thread. In the software world, a lot can change in a very short time, and doing things this way makes it more likely that you will find the best information. You may link to the original discussion in the new thread if you think it may be helpful.
    Thread closed.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •