473,413 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Please help me develop draw nested squares C++

25
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.

Expand|Select|Wrap|Line Numbers
  1. Create a program to print nested squares 
  2. Input: Accept the width of the largest square 
  3. Output: A series of nested squares with 1 space between them
  4. Sample:
  5. > eca4.exe 10
  6. **********
  7. *        *
  8. * ****** *
  9. * *    * *
  10. * * ** * *
  11. * * ** * *
  12. * *    * *
  13. * ****** *
  14. *        *
  15. **********
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>                 //Required if your program does any I/O
  2. #include <string>                   //Required if your program uses C++ strings
  3. using namespace std;                //Required for ANSI C++ 1998 standard.
  4.  
  5.  
  6.  
  7. void drawRectangle(int w, char symbol)
  8. {     
  9.  
  10.     for ( int i = 0; i < w; i++)                   //How many Lines we need to draw?
  11.     {
  12.         if(i == 0 || i == (w - 1))                 //Is it the first line or the last line?
  13.         {
  14.              for(int cFnL = 0; cFnL < w; cFnL++)   //Print out full char for the first and the last line
  15.              {
  16.                      cout << symbol;
  17.              }
  18.              cout << endl;
  19.         }
  20.         else                                       //Draw middle lines
  21.         {
  22.              if(i % 2 == 0 && i < (w/2))
  23.              {  
  24.                      for(int cFnL = 0; cFnL < w; cFnL++)   // Top half of top side of inside squares
  25.                      {
  26.                              cout << "-";
  27.                      }
  28.                      cout << endl;  
  29.              }
  30.              else if(i % 2 == 1 && i >= (w/2))
  31.              {
  32.                      for(int cFnL = 0; cFnL < w; cFnL++)   //Bottom haft of bottom side of inside squares
  33.                      {
  34.                              cout << "-";
  35.                      }
  36.                      cout << endl; 
  37.              }
  38.              else
  39.              {
  40.                      for(int cFnL = 0; cFnL < w; cFnL++)   //Space lines between squares
  41.                      {
  42.                              cout << " ";
  43.                      }
  44.                      cout << endl; 
  45.              }
  46.         }
  47.     }
  48. }
  49.  
  50. int main (int argc, char **argv)    //Arguments to main are command line arguments
  51. {
  52.     char reply;                     //A character variable to hold user input
  53.     int iWidth;
  54.  
  55.     cout << "Enter integer the Width of the largest square: ";
  56.     cin >> iWidth;
  57.     cout << endl;
  58.  
  59.     drawRectangle(iWidth, '*');       //Call function to draw out
  60.  
  61.     cout << endl;
  62.  
  63.  
  64.     // This section stops the program 'flashing' off the screen.
  65.     cout << "Press q (or any other key) followed by 'Enter' to quit: ";
  66.     cin >> reply;
  67.     return 0;
  68. }
Feb 15 '07 #1
10 4594
AdrianH
1,251 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. Create a program to print nested squares 
  2. Input: Accept the width of the largest square 
  3. Output: A series of nested squares with 1 space between them
  4. Sample:
  5. > eca4.exe 10
  6. **********
  7. *        *
  8. * ****** *
  9. * *    * *
  10. * * ** * *
  11. * * ** * *
  12. * *    * *
  13. * ****** *
  14. *        *
  15. **********
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
Feb 15 '07 #2
wazzup
25
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!
Feb 15 '07 #3
AdrianH
1,251 Expert 1GB
Ok, I ran your programme and got this:
Expand|Select|Wrap|Line Numbers
  1. Enter integer the Width of the largest square: 10
  2.  
  3. **********
  4.  
  5. ----------
  6.  
  7. ----------
  8. ----------
  9.  
  10. ----------
  11.  
  12. **********
  13.  
  14. Press q (or any other key) followed by 'Enter' to quit: 
  15.  
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
Feb 16 '07 #4
wazzup
25
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...
Feb 18 '07 #5
wazzup
25
Here is what I meant

Expand|Select|Wrap|Line Numbers
  1. **********
  2.  
  3. - ------ - // <-- I want to skip 2 blanks here
  4.  
  5. - - -- - - // <-- I want to skip 4 blanks here
  6. ----------
  7.  
  8. ----------
Feb 18 '07 #6
AdrianH
1,251 Expert 1GB
Here is what I meant

Expand|Select|Wrap|Line Numbers
  1. **********
  2.  
  3. - ------ - // <-- I want to skip 2 blanks here
  4.  
  5. - - -- - - // <-- I want to skip 4 blanks here
  6. ----------
  7.  
  8. ----------
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
Feb 19 '07 #7
wazzup
25
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>
Feb 20 '07 #8
wazzup
25
Finally, I got it. This problem took me more than 5 hours. Hahha. Such a dummy! Thanks for all the helps!
Feb 20 '07 #9
Ganon11
3,652 Expert 2GB
Awesome! Good to see you figured it out.
Feb 20 '07 #10
AdrianH
1,251 Expert 1GB
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
Feb 21 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Simon | last post by:
Hi, I am having a little problem with my PHP - MySQl code, I have two tables (shown below) and I am trying populate a template page with data from both. <disclaimer>Now I would like to say my...
72
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
by: Candace | last post by:
I have to write a program to find all Pythagorean triples for a right triangle. I know that the squares of two sides of the triangle must equal the square of the third (longest) side. However, I...
8
evilmonkey
by: evilmonkey | last post by:
I am using swing to nest squares with in squares (256 times) and that part is working fine, but what i want to do is change the color of each line so that the red green and blue components each...
0
by: kam45 | last post by:
I did one program that draws a line with two squares at the ends and I can just click and extend each ends. It works ok but I need to move the whole line. Would anyone knows how? Private Const...
4
by: dragony2000 | last post by:
I want to solve these questions using C# , Please !!! ************************************************************* 1- The factorial method is used frequently in probability problems. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.