Make an shape

hello i'm the beginner on programming

i need to make this shape in order :
\x\x\xx/x/x/
x\x\x\/x/x/x
xx\x\xx/x/xx
xxx\x\/x/xxx
xxxx\xx/xxxx
xxxxx\/xxxxx

with 3 input and
\x\x\x\x\xx/x/x/x/x/
x\x\x\x\x\/x/x/x/x/
xx\x\x\x\xx/x/x/x/
xxx\x\x\x\/x/x/x/
xxxx\x\x\xx/x/x/
xxxxx\x\x\/x/x/
xxxxxx\x\xx/x/
xxxxxxx\x\/x/
xxxxxxxx\xx/
xxxxxxxxx\/
with 5 input.

i already make the source code here
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
#include <iostream>

using namespace std;

int main()
{
    int n;

    cout<<"masukan nilai = ";
    cin>>n;

    for (int i=1; i<=n; i++){
        for (int j=1; j<=n; j++){
            if (i<=j){
                cout<<"\\";
            }
            cout<<" ";
            for (int w=n;w>=1;w--){
                if (j==n-(w-1) && i>n-(w-1)){
                    cout<<" ";
                }
            }
        }
        for (int j=n; j>=1; j--){
                cout<<" ";
            if (i<=j){
                cout<<"/";
            }
            for (int w=n;w>=1;w--){
                if (j==n-(w-1) && i>n-(w-1)){
                    cout<<" ";
                }
            }
        }
            cout<<"\n";
        for (int j=1; j<=n; j++){
            cout<<" ";
             if (i<=j){
                cout<<"\\";

            }
            for (int w=n;w>=1;w--){
                if (j==n-(w-1) && i>n-(w-1)){
                    cout<<" ";
                }
            }
        }
        for (int j=n; j>=1; j--){
            if (i<=j){
                cout<<"/";
            }
            cout<<" ";
            for (int w=n;w>=1;w--){
                if (j==n-(w-1) && i>n-(w-1)){
                    cout<<" ";
                }
            }
        }
        cout<<endl;
    }

    return 0;
}


but is too much looping for this section how me to reduced the looping used?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main()
{
   char SPACE = ' ';
   int N;
   cout << "Input N: ";   cin >> N;
   int N2 = 2 * N;

   string L, R, blanks( N2, SPACE );
   for ( int i = 0; i < N; i++ ) { L += "\\ "; R += " /"; }
   L = blanks + L;  R = R + blanks;

   for ( int i = N2; i > 0; i-- ) cout << L.substr( i, N2 ) << R.substr( N2 - i, N2 ) << endl;
}



or even ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

string operator *( int n, string s )
{
   string result;
   if ( n <= 0 ) return "";
   while ( n-- ) result += s;
   return result;
}

int main()
{
   string B = " ", L = "\\ ", V = "\\/", R = " /";
   int N;
   cout << "Input N: ";   cin >> N;

   for ( int i = 0; i < 2 * N; i++ ) 
   {
      int diag = N - ( i + 1 ) / 2;
      cout << i * B   +   diag * L   +   ( i % 2 ) * V   +   diag * R << '\n';
   }
}



or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

void diag( int i, int j, char c )
{
   if ( j >= i && ( j - i ) % 2 == 0 ) cout << c;
   else                                cout << ' ';
}

int main()
{
   int N;
   cout << "Input N: ";   cin >> N;

   for ( int i = 0; i < 2 * N; i++ )
   {
      for ( int j = 0        ; j <  2 * N; j++ ) diag( i, j, '\\' );
      for ( int j = 2 * N - 1; j >= 0    ; j-- ) diag( i, j, '/'  );
      cout << '\n';
   }
}



Input N: 3
\ \ \  / / /
 \ \ \/ / / 
  \ \  / /  
   \ \/ /   
    \  /    
     \/     


Input N: 5
\ \ \ \ \  / / / / /
 \ \ \ \ \/ / / / / 
  \ \ \ \  / / / /  
   \ \ \ \/ / / /   
    \ \ \  / / /    
     \ \ \/ / /     
      \ \  / /      
       \ \/ /       
        \  /        
         \/     




Last edited on
Topic archived. No new replies allowed.