Displaying patters using nested loop

hi..

I am trying to display this following set of patterns


 (a)           (b)               (c)             (d)
 *             **********        **********               *
 **            *********          *********              **
 ***           ********            ********             ***
 ****          *******              *******            ****
 *****         ******                ******           *****
 ******        *****                  *****          ******
 *******       ****                    ****         *******
 ********      ***                      ***        ********
 *********     **                        **       *********
 **********    *                          *      **********



I managed to do a and b, but I am stuck with c and d..Can someone help me, just perhaps describe the logic.. I will try it out myself.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
*Program from deitel*/

#include<iostream>

using namespace std;

int main()
{
	int i,j;

	cout<<"(a)";
	cout<<"\n";
	for(i=1;i<=10;i++)
	{
		for(j=1;j<=i;j++)
		{
			cout<<"*";
			
		}
		cout<<"\n";
	}

	cout<<"\n";
	cout<<"(b)";
	cout<<"\n";
	for(i=10;i>0;i--)
	{
		for(j=i;j>0;j--)
		{
			cout<<"*";
			
		}
		cout<<"\n";
	}

	cout<<"\n";
	cout<<"(c)";
	cout<<"\n";
	for(i=1;i<=10;i++)
	{
		cout<<" ";
		for(i=2;i<=10;i++)
		{
			cout<<"*";
		}
		

		
		
	}


return 0;
}
Last edited on
in (c) you have to write i ' 'and 10-i '*'. This is almost the same as (a), but you need to have two loops inside the fist one: one for ' ' and one for '*'
This looks kind of fun and I have a minute to kill. Note that this is an intentionally silly approach.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>

int main( int argc, char * argv[] )
{
    using namespace std;

    string      spacing(                   2, ' ' ),
              big_space(                  10, ' ' ),
           little_space(                   1, ' ' ),
              big_stars(    big_space.size(), '*' ),
           little_stars( little_space.size(), '*' );

    cout << left << setw( 11 ) << "(a)" << spacing
         << left << setw( 11 ) << "(b)" << spacing
         << left << setw( 11 ) << "(c)" << spacing
         << left << setw( 11 ) << "(d)" << endl;

    for( int i = big_space.size(); i > 0; --i )
    {
        cout << little_stars << big_space << spacing
             << big_stars << little_space << spacing
             << little_space << big_stars << spacing
             << big_space << little_stars << endl;

        little_stars += '*';
        big_stars.erase( big_stars.size() - 1, 1 );
        little_space += ' ';
        big_space.erase( big_space.size() - 1, 1 );
    }

    return 0;
}
(a)          (b)          (c)          (d)        
*            **********    **********            *
**           *********      *********           **
***          ********        ********          ***
****         *******          *******         ****
*****        ******            ******        *****
******       *****              *****       ******
*******      ****                ****      *******
********     ***                  ***     ********
*********    **                    **    *********
**********   *                      *   **********

This one is a more serious approach, although still not what your teacher is after:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
 
int main( int argc, char * argv[] )
{
    using namespace std;
 
    cout << left << setw( 11 ) << "(a)" << "  "
         << left << setw( 11 ) << "(b)" << "  "
         << left << setw( 11 ) << "(c)" << "  "
         << left << setw( 11 ) << "(d)" << endl;
                                                                                                                        
    for( int i = 1; i < 11; ++i )
    {   
        cout << string(      i, '*' ) << string( 11 - i, ' ' ) << "  "
             << string( 11 - i, '*' ) << string(      i, ' ' ) << "  "
             << string(      i, ' ' ) << string( 11 - i, '*' ) << "  "
             << string( 11 - i, ' ' ) << string(      i, '*' ) << endl;
    }   
 
    return 0;
}

Ok, I'm done now...
Last edited on
Topic archived. No new replies allowed.