Computing.Net > Forums > Programming > Help with Nested Loops

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with Nested Loops

Reply to Message Icon

Name: anon_omas
Date: April 24, 2005 at 20:13:46 Pacific
OS: Windows XP
CPU/Ram: Pentium IV ; 256 MB Ram
Comment:

I have to write a program in C++ for class that uses nested loops to display a hollow rectangle with stars ( * ). I can display a whole rectangle with the following code:

---------------
// This will ask the user to enter the height of the rectangle.
cout << "Please enter the height of the rectangle." << endl;
cin >> rec_height;

// This will ask the user to enter the width of the rectangle.
cout << "Please enter the width of the rectangle." << endl;
cin >> rec_width;

// This will pass the height and width of the rectangle to the function.
draw_rectangle = Rectangle(rec_height, rec_width);

/* This function will accept two integers, the height and width, and will draw a rectangle
of that height and width.*/
int Rectangle(int a, int b)
{
int rh = 1;
int rw = 1;

for (rh = 1; rh <= a; rh++)
{
for (rw = 1; rw <= b; rw++)

cout << "*";
cout << endl;
}

return 0;
}

----------------
A hollow rectangle would look something like this:

****
* *
* *
****

I tried modifying the code:
---------------
int HollowRectangle(int a, int b)
{
int rh = 1;
int rw = 1;

for (rh = 1; (rh == 1) || (rh == a); rh++)
{
for (rw = 1; rw <= b; rw++)
{

cout << "*";
}

cout << endl;
}

for (rh = 2; (rh > 1) & (rh < a); rh++)
{
for (rw = 1; rw <= b; rw++)
{
cout << " ";
}

cout << endl;
}

return 0;
}
---------------

But it only displays one line of stars and then all blank lines. I was wondering if anyone had any ideas or knew what part of the code I should try modifying. I tried a bunch of other modifications to the code, but they just caused an infinite loop of "*".

Basically, I just need the first and last lines to have "*" all across, and the middle lines just have a "*" at the first and last column of each line.

Thank you very much for any help.



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: April 25, 2005 at 09:30:41 Pacific
Reply:

Here you go:

#include <conio>
#include <iostream>
using namespace std;
void HollowRectangle(int Height, int Width)
{
    // we need to build the rectangle from the top to the bottom
    // so we will start with the rows as the outer loop

    for(int row=0;row<Height;row++)
    {
        // now check to see if we are top row or bottom row
        if(row == 0 || row+1 == Height)
        {
            // fill in the columns with asterisks
            for(int column=0;column<Width;column++)
            {
                cout << "*";
            }
        }
        else
        {
            for(int column=0;column<Width;column++)
            {
                // we always put an asterisk in first column
                // and last column
                if(column == 0 || column+1 == Width)
                    cout << "*";
                else
                    cout << " ";
            }
        }
        cout << endl;
    }
}

void main(void)
{
    HollowRectangle(10,10);
    getch();
}

The problem i see with your attempt is that you are attempting to print out your asterisks first then your spaces second. You can't do that, they have to be done together.

Chi

They mostly come at night...mostly


0

Response Number 2
Name: Don Arnett
Date: April 25, 2005 at 11:59:38 Pacific
Reply:

for(int row=0;row<Height;row++)
{
   // Always output a first column asterisk
   cout << "*";

   // Loop thru inner cols
   for(int column=1;column<Width-1;column++)
   {
      // Output * or space
      cout << (row == 0 || row+1 == Height) ? "*" : " ";
   }

   //Always output an end column asterisk
   cout << "*";
}
cout << endl;


Be sure to come back and let us know if our suggestions helped!


0

Response Number 3
Name: Don Arnett
Date: April 25, 2005 at 12:09:01 Pacific
Reply:

I forgot to comment. I took CHs code and combined it a bit. I didn't test but it should work with a little or no tweaking.

The idea is that for the inner loop, you always output an asterisk in the first and last columns, so I have a cout << "*"; surrounding my inner loop. THen I change the inner loop to count from 1 to width-1, rather than 0 to width, so the inner loop goes thru all columns except for the left and rightmost.

Then the only question is whether the inner loop should be outputting * or spaces.

An even more efficient version would be:

char mychar;
for(int row=0;row<Height;row++)
{
   // Always output a first column asterisk
   cout << "*";

   // Set char to output
   mychar = (row == 0 || row+1 == Height) ? "*" : " ";

   // Loop thru inner cols
   for(int column=1;column<Width-1;column++)
   {
      // Output * or space
      cout << mychar;
   }

   //Always output an end column asterisk
   cout << "*";
}
cout << endl;

Be sure to come back and let us know if our suggestions helped!


0

Response Number 4
Name: anon_omas
Date: April 25, 2005 at 20:13:43 Pacific
Reply:

Thanks so much everyone! Don, your version is probably more efficient, but we didn't really learn 'char' yet, and I don't know if he wants us to do it that way.

I integrated Chi's code into my program, and it works fine. I knew that I had to do the asteriks and spaces together and not separate, but I wasn't quite sure how.

Thanks again everyone.


0

Response Number 5
Name: Chi Happens
Date: April 26, 2005 at 07:22:14 Pacific
Reply:

Also, Don's doesnt work right. (sorry don but i tested yours and although it is more obfuscated it needs a few changes to work right)

for(int row=0;row<Height;row++)
{
   // Always output a first column asterisk
   cout << "*";
   // Loop thru inner cols
   for(int column=1;column<Width-1;column++)
   {
      // Output * or space

      // don's original outputs ones and zeros preceeded and followed by *s
      // cout << (row == 0 || row+1 == Height) ? "*" : " ";
      //You have to modify it so that it prints the characters and not the result of the comparison
      cout << ((row == 0 || row+1 == Height) ? "*" : " "); // added parens
   }

   //Always output an end column asterisk

   // don's code forgot to add endl each time:
   //cout << "*";
   // i simply added an endl
   cout << "*" << endl;
}
cout << endl;

and the char one wont work because you cannot convert a char * to a char, so you have to modify it like this:

char mychar;
for(int row=0;row<Height;row++)
{
    // Always output a first column asterisk
    cout << "*";

    // Set char to output

    // don's has same issue with 1's and zero's insead of *s
    // as well as generating error "cannot convert char * to char
    mychar = (row == 0 || row+1 == Height) ? "*" : " ";
    // mine forces the conversion and adds parens to send * instead of 1 and 0
    mychar = *((row == 0 || row+1 == Height) ? "*" : " ");

    // Loop thru inner cols
    for(int column=1;column<Width-1;column++)
    {
        // Output * or space
        cout << mychar;
    }

    //Always output an end column asterisk

    // again, don forgot the endl:
    cout << "*";
    // i simply added an endl
    cout << "*" << endl;
}
cout << endl;


So you would have to know about pointers as well as chars.

Nice try tho, Don. It is definitely more obfuscated and optimized.

Chi

They mostly come at night...mostly


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Further help needed.... VB.NET service



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with Nested Loops

newbie need help on nested loops www.computing.net/answers/programming/newbie-need-help-on-nested-loops/4863.html

Nested loop in batch file help www.computing.net/answers/programming/nested-loop-in-batch-file-help/19396.html

Help with debugging C++ code www.computing.net/answers/programming/help-with-debugging-c-code/13083.html