Please help me develop draw nested squares C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wazzup
    New Member
    • Feb 2007
    • 25

    Please help me develop draw nested squares C++

    C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far.

    Code:
    Create a program to print nested squares 
    Input: Accept the width of the largest square 
    Output: A series of nested squares with 1 space between them
    Sample:
    > eca4.exe 10
    **********
    *        *
    * ****** *
    * *    * *
    * * ** * *
    * * ** * *
    * *    * *
    * ****** *
    *        *
    **********
    I thought algorithm for this would be.

    * Top Lines of inside squares would be 1-8 , 1-3 - 6-8 (Don't count first and last lines of largest square)

    * Bottom Lines of inside squares would be 2-7, 2-4 5-7 (Don't count first and last lines of largest square)

    Here is what I got so far. Please help me with some ideas to develop

    Code:
    #include <iostream>                 //Required if your program does any I/O
    #include <string>                   //Required if your program uses C++ strings
    using namespace std;                //Required for ANSI C++ 1998 standard.
    
      
    
    void drawRectangle(int w, char symbol)
    {     
         
        for ( int i = 0; i < w; i++)                   //How many Lines we need to draw?
        {
            if(i == 0 || i == (w - 1))                 //Is it the first line or the last line?
            {
                 for(int cFnL = 0; cFnL < w; cFnL++)   //Print out full char for the first and the last line
                 {
                         cout << symbol;
                 }
                 cout << endl;
            }
            else                                       //Draw middle lines
            {
                 if(i % 2 == 0 && i < (w/2))
                 {  
                         for(int cFnL = 0; cFnL < w; cFnL++)   // Top half of top side of inside squares
                         {
                                 cout << "-";
                         }
                         cout << endl;  
                 }
                 else if(i % 2 == 1 && i >= (w/2))
                 {
                         for(int cFnL = 0; cFnL < w; cFnL++)   //Bottom haft of bottom side of inside squares
                         {
                                 cout << "-";
                         }
                         cout << endl; 
                 }
                 else
                 {
                         for(int cFnL = 0; cFnL < w; cFnL++)   //Space lines between squares
                         {
                                 cout << " ";
                         }
                         cout << endl; 
                 }
            }
        }
    }
    
    int main (int argc, char **argv)    //Arguments to main are command line arguments
    {
        char reply;                     //A character variable to hold user input
        int iWidth;
        
        cout << "Enter integer the Width of the largest square: ";
        cin >> iWidth;
        cout << endl;
        
        drawRectangle(iWidth, '*');       //Call function to draw out
        
        cout << endl;
        
        
        // This section stops the program 'flashing' off the screen.
        cout << "Press q (or any other key) followed by 'Enter' to quit: ";
        cin >> reply;
    	return 0;
    }
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Originally posted by wazzup
    C++ is new to me and I'm trying solve this problem but get stuck. Please help me out. Below is the task and after that is what i got so far.

    Code:
    Create a program to print nested squares 
    Input: Accept the width of the largest square 
    Output: A series of nested squares with 1 space between them
    Sample:
    > eca4.exe 10
    **********
    *        *
    * ****** *
    * *    * *
    * * ** * *
    * * ** * *
    * *    * *
    * ****** *
    *        *
    **********
    I thought algorithm for this would be.

    * Top Lines of inside squares would be 1-8 , 1-3 - 6-8 (Don't count first and last lines of largest square)

    * Bottom Lines of inside squares would be 2-7, 2-4 5-7 (Don't count first and last lines of largest square)
    I don't understand. What do you mean by Top Lines and Bottom Lines? Can you please point them out in your diagram?

    Thanks,


    Adrian

    Comment

    • wazzup
      New Member
      • Feb 2007
      • 25

      #3
      The way I explained may not clear.

      Basicly, I meant that we have 10 char * for each crossing lines (left to right)

      The second crossing line we have 2 *. ( in loops to 10, position of those was 0 and 9)

      The fourth crossing line we have 4 * ( in loops to 10, position of those was 0 2 7 9)
      The fith crossing line we have 6 * ( in loops to 10, position of those was 0 2 4 5 7 9)

      All of those lines I called Top Sides of squares ( Square have 4 sides: top, left, right, bottom) and Opposite of those crossing lines, I called Bottom Squares Sides

      I don't know how to make a loops that keep add on 2 more *
      Please help me out. Thanks much!

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Ok, I ran your programme and got this:
        Code:
        Enter integer the Width of the largest square: 10
        
        **********
                  
        ----------
                  
        ----------
        ----------
                  
        ----------
                  
        **********
        
        Press q (or any other key) followed by 'Enter' to quit:
        This looks like you’ve got the horizontal lines working. What you need now is to get the vertical lines and the top and bottom of the boxes to show.

        Let’s try and break this down.

        The place of the first set of dashes you have drawn, it should draw the vertical lines of the outer box and the top face of the inner one.

        In place of the next set of dashes should be drawn the vertical lines of the outer two boxes and the top face of the inner one.

        In place of the next set of dashes should be drawn the vertical lines of the outer two boxes and the bottom face of the inner one.

        And finally, in place of the next set of dashes should be drawn the vertical lines of the outer boxes and the bottom face of the inner one.

        Can you see how you can use which line you are on to draw what I have stated?

        Give it a shot and let me know your progress.


        Adrian

        Comment

        • wazzup
          New Member
          • Feb 2007
          • 25

          #5
          Thanks for reply. But what you're saying that where I got stuck. Maybe I explain not really clear

          First dashes line, there are 2 blanks and the Second dashes line has 4 blanks.

          I really don't know how to make the loops skip 2 blanks at first dashes line and 4 blanks at second dashes line and go on...

          Comment

          • wazzup
            New Member
            • Feb 2007
            • 25

            #6
            Here is what I meant

            Code:
            **********
                      
            - ------ - // <-- I want to skip 2 blanks here
                      
            - - -- - - // <-- I want to skip 4 blanks here
            ----------
                      
            ----------

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by wazzup
              Here is what I meant

              Code:
              **********
                        
              - ------ - // <-- I want to skip 2 blanks here
                        
              - - -- - - // <-- I want to skip 4 blanks here
              ----------
                        
              ----------
              Right, so you know what you want outputted, this is essential. Without a target you won’t know if you are done. :).

              Now, in the code you know what line you are on when you print out a line. So based on this information, you have a way of determining what to print.

              i.e. From the code’s perspective:
              1. I am on the 0th line, so I need to draw a line across the top, w chars wide
              2. I am on the 2nd line, so I need to draw a line across the top, w-2 chars wide. PLUS, I also need to draw 2 chars on either side to represent the outer box and the space between boxes.
              3. I am on the 4th line, so I need to draw a line across the top, w-4 chars wide. PLUS, I also need to draw 4 chars on either side to represent the outer boxes and the space between boxes.

              Note: the first point can be generalised to match the other 2 points as you are outputting 0 chars on either side to represent the nonexistent outer box.

              Now what I have outlined here is for doing the top half. You will need to modify this algorithm to do the bottom half.

              Does this make sense? Try it out and let me know how you are making out.


              Adrian

              Comment

              • wazzup
                New Member
                • Feb 2007
                • 25

                #8
                After many fails, I figure it out the way to solve. I wrote the code and seem to works but when I try to enter an odd number for input, program gave me wrong results. How can I develop this? Please help!

                <code removed>
                Last edited by Ganon11; Feb 20 '07, 10:27 PM. Reason: code removed

                Comment

                • wazzup
                  New Member
                  • Feb 2007
                  • 25

                  #9
                  Finally, I got it. This problem took me more than 5 hours. Hahha. Such a dummy! Thanks for all the helps!

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    Awesome! Good to see you figured it out.

                    Comment

                    • AdrianH
                      Recognized Expert Top Contributor
                      • Feb 2007
                      • 1251

                      #11
                      Originally posted by wazzup
                      Finally, I got it. This problem took me more than 5 hours. Hahha. Such a dummy! Thanks for all the helps!
                      Glad that you were able to figure it out. Sorry if I wasn't around at the end, but it looks like you didn't need me anyway. :)

                      Excellent work!


                      Adrian

                      Comment

                      Working...