GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2004, 18:05
keperry keperry is offline
New Member
 
Join Date: Oct 2004
Posts: 7
keperry is on a distinguished road
Question

Help with nested for loops...please. sorry newbie


I am having trouble grasping the concept of nested for loops. Usually, I can find the pattern in things and figure them out, but for some reason, I just can seem to find a pattern in using nested for loops.

Can someone please help me interpret nested for loops?

Example:

Write a program that uses nested to print a hollow rectangle made out of the '|' and '_' characters. The program should prompt the user for the length and the width of the rectangle.

So here is what I started with:

CPP / C++ / C Code:
int main()
{
      int width;
      int height;

      cout << "Enter the height" << endl;
      cin >> height;
      cout << "Enter the width" << endl;
      cin >> width;

      for (int x=0; x < height; x++)
      {
          for (int y=0; y < width; y++)
          {

Now, I get all flustered cause I don't know what to do. Should I try to define the limits and then use 'if' statements. Please don't give me the code, I need to be able to do this on my own, and if I don't understand why, then I won't learn.
Last edited by dsmith : 14-Oct-2004 at 19:24. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 14-Oct-2004, 19:03
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
May be you should go ahead with the logic you have in your mind and complete the code and then if you face any problem, there are people to help.

Pls complete the remaining code and post it. We will surely try n' help if we can. As you said, you can't learn unless you do it on your own my friend.

Also, pls use code tags to post your code.

Good luck.
  #3  
Old 14-Oct-2004, 19:43
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Yep, I agree with nkhambal. You are on the right track.

There is probably others way to do it, but I would definitely need an if statement (or two).

Think of it as a type of grid, the first loop should draw the entire row, with the inner loop drawing each individual character of the row.

Try it out and post back if you have problems.

Good luck.
  #4  
Old 14-Oct-2004, 21:27
keperry keperry is offline
New Member
 
Join Date: Oct 2004
Posts: 7
keperry is on a distinguished road
Well, I appreciate the motivation
I am attempting, but I must let my brain uncramp from this mess. I will figure it out, its just a matter of time.
  #5  
Old 14-Oct-2004, 21:29
keperry keperry is offline
New Member
 
Join Date: Oct 2004
Posts: 7
keperry is on a distinguished road
Quote:
Originally Posted by nkhambal
Also, pls use code tags to post your code.


BTW - what are code tags?
  #6  
Old 14-Oct-2004, 22:00
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Using the values c and /c inside square brackets [] to enclose your code.

If you scroll to the very bottom of the "post message" page, or look in the FAQ, there's a link to all the codes you can use when you post messages. They help to highlight key terms to make it easier to read.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #7  
Old 15-Oct-2004, 13:22
keperry keperry is offline
New Member
 
Join Date: Oct 2004
Posts: 7
keperry is on a distinguished road
Well, I think that I got it. Does anyone have any suggestions on improving?

CPP / C++ / C Code:
int main()
{
      int width;
      int height;

      cout << "Enter the height" << endl;
      cin >> height;
      cout << "Enter the width" << endl;
      cin >> width;

      for (int x=0; x <= height; x++)
      {
          for (int y=0; y <= width; y++)
          {
              if (x == 0 )
                 cout << "_";
              else if ((y == 0) || (y == width))
              {
                 cout << "|";
              }
              else if ((x == height) && (y != 0 || y != width))
                 cout << "_";
              else
                 cout << " ";
          } //end inner loop
          cout << endl;
      } //end outer loop
      system("PAUSE");
      return 0;
}

Thanks to all the replied.
  #8  
Old 15-Oct-2004, 13:47
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
How about simplyfying loop to

CPP / C++ / C Code:

      for (int x=0; x <= height; x++)
      {
          for (int y=0; y <= width; y++)
          {
              if (x == 0 || x== height)
                 cout << "_";
              else if ((y == 0) || (y == width))
              {
                 cout << "|";
              } else
                 cout << " ";
          } //end inner loop
          cout << endl;
      } //end outer loop

Just saved one else if for you :-)

Also, you dont really want to use system() call. You can use getchar() instead, if the intension is to hold output window to see the output (I guess you are on windows)
  #9  
Old 15-Oct-2004, 14:44
keperry keperry is offline
New Member
 
Join Date: Oct 2004
Posts: 7
keperry is on a distinguished road
That is exactly what I had at first, but since we were told to use an underscore character the output looks funny on the bottom. You end up with something like this:
CPP / C++ / C Code:
_________
|       |
|       |
|       |
|       |
|       |
_________

That's why I added the extra else.

Also the system("PAUSE") (to hold output window), is something that we are supposed to use because of our compiler. For some reason, our teacher uses Dev-C++ version 4. It's free, but I am not sure why she doesn't have us use version 5 because it uses the newer standards....resistant to change like everyone else I guess.

Thanks for the post! Have you been programming long?
  #10  
Old 15-Oct-2004, 17:07
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi,

I think we are doing some mistake here..we are not actually printing the required number of "_" & "|". Loop logic is having some problem. If i input hight and width both as 10, i should be getting a square but I am not.

I have fixed this.

CPP / C++ / C Code:
for (int x=0; x <=height; x++)
      {
          
		  for (int y=0; y <=width; y++)
          {
             if (y == 0 && x>0)
              {
                 cout <<"|";
              } else if (y == width && x>0)
			  {
				cout <<"|";
              } else {
                 cout <<" ";
			  }
			  
			  if (x == 0 && y<width)
			  {
				 cout <<"_";
			  } else if (x==height && y<=width-1)
			  {
				  cout <<"_";
			  } else {
				  cout<<" ";
			  }
			  
	  } //end inner loop
          cout << endl;
      } //end outer loop

Parden me, if Above code is not properly indented. I did pasted an indented code but its not printing out the same.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using nested for loops dontcare C++ Forum 1 03-Oct-2004 12:33
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 11:32
newbie using Sam's series Skampy C++ Forum 3 06-Mar-2004 18:51
C++ books for newbie mak90thug C++ Forum 4 04-Feb-2004 14:58
Newbie Question about Borland andrewcarraway C++ Forum 4 24-Jan-2004 11:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.