Thread: I need some thoughts on this Diamond Nested For loop

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    1

    Post I need some thoughts on this Diamond Nested For loop

    Hello everyone, I just joined this forum. I need some thoughts about a diamond nested for loop, the code below shows 17 stars at middle which is what I need, and one more thing I need is to only print out
    1, 4, 9, 13, 17, 13, 9, 4, 1, * pattern. Thanks guys!

    Code:
     #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>
    
    int main()
    {   clrscr();
        int     numStars = 1,
         numSpaces = 8;
    
        while (numSpaces > 0)
        {
        for (int i = 0; i < numSpaces; i++)
        {
            cout << " ";
        }
        for ( i = 0; i < numStars; i++)
        {
            cout << "*";
        }
        cout << "\n";
    
        numStars += 2;
        numSpaces--;
        }
        while (numStars > 0)
        {
        for (int i = 0; i < numSpaces; i++)
        {
            cout << " ";
        }
        for ( i = 0; i < numStars; i++)
            {
                cout << "*";
            }
            cout << "\n";
        numStars -= 2;
            numSpaces++;
        }
    
        getch();
        return 0;
    }

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    stdio.h is a C header not a C++ header. conio.h is nonstandard. iostream.h was deprecated sometime around the time the dinosaurs became extinct. In two of your loops i is undefined.

    The code will work and print out a diamond of stars if you fix the undefined loop variables, and remove the nonstandard stuff.

    As for changing the loops to print out a different diamond pattern, well you managed to get this far on your own, I am sure you can work that out, unless of course you have copied this code from somewhere in which case you might be up a certain creek without a paddle.

    Which compiler are you using in class? Turbo-C? If so and you're serious about learning programming, find a better school.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Ah, but did the OP get this far all by themselves?

    Apart from munging the code to work with Turbid C, it seems remarkably similar to this post
    Print star ('*') diamond in C with nested loops? - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I love the username though lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Diamond in C program in Decrement loop
    By arsalanmemon in forum C Programming
    Replies: 3
    Last Post: 12-06-2013, 08:39 AM
  2. Nested loops - asterisk diamond
    By charl33t in forum C Programming
    Replies: 3
    Last Post: 07-11-2013, 11:08 PM
  3. Replies: 6
    Last Post: 07-30-2010, 09:37 PM
  4. diamond shape, for loop only
    By bombers in forum C Programming
    Replies: 2
    Last Post: 10-27-2009, 06:00 PM
  5. Using a for loop to make a diamond...
    By wco5002 in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2007, 08:56 PM

Tags for this Thread