print shapes using characters for loops

Hey guys,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for (i = 1; i <= n; i++)
		{
			
		 	 	  	  	  	   
	 	 for (j = 1; j <= i; j++)	 	  	 
				cout << "*\t";
				cout << endl;
				
						 	 	 
			for (int l=1; l <= i; l++)
               
              {
                  for (int m=1 ; m <=i; m++)
                  {
                      cout << "&\t";
                  }
                cout << endl;
              } 
			  	     	 
	
		}


I have modified the inner for loops to this.

So now it prints out like this when n=3.

*
&
**
&&
&&
***
&&&
&&&
&&&

but I want this
*
&
*
**
&&
&&
*
**
***
&&&
&&&
&&&

Please explain the code, as I don't understand it fully
Thank you for your help :)
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main(){
    int n = 0;
    cout << "Enter a positive whole number: ";
    cin >> n;
    for(int i = 0; i < n; i++){for(int j = 0; j < i; j++){cout << "*\t" << endl;}}
}


If I'm understanding the point of your code, this should work just fine.
I need another nested while loop to print out '&' characters. I'm just not sure where to place it, as when I've tried, I only get infinite loops of '&' characters printed out.
i can tell you right now there something wrong with this....


while (n < 0 && n > 10); '


so n is gona be less than 0 AND greather than 10 at the same time....
edit what I said here was wrong. I'm delirious from staring at code. ummm I tried to rewrite this a lil bit but i'm fried. your on your own.
Last edited on
This should help :D I did the code a few weeks back. Not exact yours but is very similar

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    
    int l,w,h, input, width, width2, recwidth, twidth2, twidth, twidth3;
    string character;
    cout << "what shape do you want to create" << endl;
    cout << "1) square 2) Rectangle 3) Triangle " << endl;
    cin >> input;
    if (input == 1)
    {         cout << "OK. I am ready. What is the side length" << endl;
              cin >> l ;
              cout << "what character do you want to use????" << endl; 
              cin >> character;
              system ("CLS");
              for (int width=1; width <= l; width++)
               
              {
                  for (int width2=1 ; width2 <=l; width2++)
                  {
                      cout << character   ;
                  }
                cout << endl;
              }                      
                    
    }
    else if (input ==2)
    {
              cout << "OK. I am ready. What is the side length" << endl;
              cin >> l ;
              cout << "OK. I am ready. what is the width?" << endl;
              cin >> recwidth ;
              cout << "what character do you want to use????" << endl; 
              cin >> character;
              system ("CLS");
              for (int width=1; width <= l; width++)
               
              {
                  for (int width2=1 ; width2 <=recwidth; width2++)
                  {
                                            cout << character ;
                  }
              cout << endl;
              }   
              
    }                 
    else if (input ==3)
    {
              cout << "OK. I am ready. What is the side length" << endl;
              cin >> l ;
              cout << " what is the second side?" << endl;
              cin >> twidth2 ;
              cout << "what character do you want to use????" << endl; 
              cin >> character;
              system ("CLS");
              for (int width=1; width <= l; width++)
               
              {
                  for (int width2 = width -1 ; width2 < twidth2; width2--)
                  {
                      cout << character ;
                       cout << endl;
                  }
              cout << endl;
              } 
      }  
    
    {
        cout << " " << endl;
    }
    
                  
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

post edited. please refer to first post of thread
Last edited on
You should try to figure this out yourself:

I am a begginer and it only took me 5 min to figure it out, just by messing with it.
This way you lear better as well.

Well here is you code

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
	int n, i;
	int j, k;
	
	do{
		cout <<" Enter a positive int n: ";
		cin >> n;
		cout << endl;
	   } while (n < 0 || n > 10);// better if you change it to OR 
	
	
  do{
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= i; j++)
			{
				cout << "*\t";
			}
			cout << endl;
			for (j = 1; j <= i; j++)
			{
				cout << "&\t";
			}
            cout << endl;
		}
	 } while (i <= n);


	return 0;
}
I'm not very sure what the variables in the for loop does. Can someone explain it to me?
Finally figured it out myself. Thanks for your help anyways :D
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
for (i = 1; i <= n; i++)
		{
			 	 	 	  	  	   	  	 
		  {	   
					 	 	 
			  for (j=1; j <= i; j++)
	               
	              {
	                  for (k=1 ; k <=j; k++)
	                  {
	                      cout << "*\t";
						  	
						 
	                  }
					 
					  
					  cout << endl;
	                  
	              } 
			  	   for (int l=1; l <= i; l++)
 
             		 {
		                  for (int m=1 ; m <=i; m++)
		                  {
		                      cout << "&\t";
		                  }
					  cout << endl;
				     }   	   
			}
		}
Topic archived. No new replies allowed.