473,563 Members | 2,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Please help me develop draw nested squares C++

25 New Member
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 4610
AdrianH
1,251 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 New Member
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 Recognized Expert Specialist
Awesome! Good to see you figured it out.
Feb 20 '07 #10

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

Similar topics

2
3927
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 skills, especially with MySQL are rudimentary</disclaimer> However my code (link below) fails, the nested database call does not return any data and...
72
4356
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 python-dev list: http://www.python.org/peps/pep-0289.html In brief, the PEP proposes a list comprehension style syntax for creating fast, memory...
46
9884
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 already in a loop." Then he goes on to portray a contrived example that doesn't tell me under what conditions a nested loop might be favoured as a...
1
9605
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the...
5
5225
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 am not sure how to use a triple-nested for...next loop to try all possibilities. For instance, take a look at these numbers: a 3 5 7 8 ...
8
2196
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 increment by 1 each time the next line is drawn. I really never used jpanel or jframe much less swing. any help I coulg get would be appreciated. ...
0
1372
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 gs As Integer = 34 Private Const hw As Integer = 6 Private Const hhw As Integer = hw \ 2 Private bf As Bitmap Private drag As...
4
2969
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 factorial of a positive integer n (written n! and pronounced “n factorial”) is equal to the product of the positive integers from 1 to n. Write a program...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.