Subscribe to C++ FunSchool        RSS Feed
-----

[C++] More on nested loops

Icon Leave Comment
Well, one of my friend told me to take another lesson in on nested loops. So, in this tutorial, I'd like to clear most of your doubts in the same.

First let's go by the definition of nested loops. Nested loops are nothing but loops inside loops. So what we will be focusing on is controlling multiple loops.

The simplest nested loop can be the one with just two loops, like:

loop1
{
loop2
{
:
:
:
}
}


Now, there can be 2 types of nested loops:
1. In which the inner loops are independent of outer loops.
2. In which the outer loops control the functioning of inner loops.

|I| Loops in which the inner loops are independent of the outer loops:

Let us take an example. If we want to print a square of a user defined size:
(if the user inputs 7 then)

% % % % % % %
% % % % % % %
% % % % % % %
% % % % % % %
% % % % % % %
% % % % % % %
% % % % % % %

Here we take 2 loops, one which controls the line number, and the other which controls what happens in the specific line. The code goes like this:

#include<iostream.h>
int main()
{  
   int n; //for the size of the square
   cin >> n; //getting size from the user
   for (int i=1; i<=n; i++) //to control the line number. Here we have 'n' lines
   {
      for (int j=1; j<=n; j++) //to control what happens in the line (print % 'n' times)
      {
         cout << "% ";
      }
   cout << endl; //to go to the next line after the printing in the line has been done
   }

   return 0;
}



That's it!
If we see carefully, we will notice that the cout << endl; has been placed in the outer loop (let's call it loop1) instead of the inner loop (loop2). This is because, the outer loop controls the line number, which the inner loop does not. So if we place it in the inner loop the output will be like this:

%
%
%
%
%
%
:
:
:
:
so on.. upto n2 times!

But, how did we know that we should place it in loop1? It is because, we want to change it the line multiple times. This "number of times" is a variable quantity and it depends on the user input. So we choose such a loop which accounts for the number of times the line changes. This is done by loop1. So we choose loop1.

In these types of loops, the loops are in no way interdependent. Here both the loops work independently, upto n times, which the user decides.

Now, we look into another type of nested loops, ie. loops which are interrelated.

|II| Loops in which the inner loops are dependent on the outer loops:

To explain these type of loops, we start with a statement. "I want to print a triangle, with the size determined by the user". THE NEED FOR SUCH DEPENDENT NESTED LOOPS COMES HERE, AS THERE IS A VARIABLE AMOUNT OF WORK DONE IN EACH LINE. Thus we feel that the inner loops may be dependent on the outer loops (which control the line number).

If we take a look at a simple triangle in C++, it will be something like this:

%
%%
%%%
%%%%
%%%%%
%%%%%%

To write the code, we follow simple steps. Note that these steps are not hard and fast rules which you should follow. You try to use which ever way you feel convenient.

STEP 1: See how many things are changing with time.
Here, the line number is changing and the width of the line (number of %) is also changing.

STEP 2: Identify what all factors control the change.
Here, the number of lines is controlled by the user input, and the width of the line is controlled by the line number. (1 % in line 1, 2 % in line 2, 3 % in line 3)

STEP 3: Try to form a pattern and see whether things are dependent on each other or not
Here, the pattern is that line 1 has 1 %, line 2 has 2 % and so on. So, the width of the line is dependent on the line number.

So, after this analysis, we write our program with nested loops. We will again use 2 loops, one controlling the line number and the other controlling the width of the line.

#include <iostream.h>
int main()
{
   int i;
   cin >> i; //getting the size of the triangle (its height)

   for (int i=1; i<=n; i++) //to control the line number
   {
      for (int j=1; j<=i; j++) //to control the width
      {
         cout << "%";
      }
   cout << endl; //to change line
   }

   return 0;
}



This is how we generally proceed. Again, you can have your own logic but, as far as I have seen, in many cases this method has helped me a lot.

So, for your exercise, I'd like to give you some programs.

Q1. WAPT: show the following pattern

_______$
______$$
_____$$$
____$$$$
___$$$$$
__$$$$$$
_$$$$$$$
$$$$$$$$

(If the user input is 8)

Q2. WAPT: show the following pattern

____$____
___$$$___
__$$$$$__
_$$$$$$$_
$$$$$$$$$

(If the user input is 5)

Q3. WAPT: show the following pattern

____$____
___$$$___
__$$$$$__
_$$$$$$$_
__$$$$$__
___$$$___
____$____

(If the user input is 4)

So this is all for today's tutorial. If there are any questions or doubts, feel free to email me at: navking.venkat7@gmail.com

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2014

S M T W T F S
 123456
78910111213
14151617181920
21 222324252627
28293031   

Tags

    Recent Entries

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories