Patterns

Could anyone please make the following pattern by accepting the number of lines , and also explain to me the logic behind adding the spaces .


1
2
3
4
5
6
7
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE   EDCBA
ABCD     DCBA
ABC       CBA
AB         BA
A           A


(P.S The above pattern is after accepting 7 lines )
Last edited on
We are not a code writing service. If you want to learn programming, make a plan and start writing some code and if you get stuck you can get help.

If you don't want to learn better go to:
https://www.assignmentexpert.com/blog/do-my-c-homework/
http://www.tutorsonnet.com/c++-programming-homework-help.php
i am unable to proceed after this ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void pattern1()
{
	int n, i, j, k,r;
	char b, c,d;
	cout << "Enter number of lines :\n";
	cin >> n;
	n = 2 * (n / 2) + 1;
	r=n;
	for (i = n; i >= 1; --i)
	{
		
		for (j = 1; j <= i; ++j)
		{  
				b = j + 64;
				c = b;
				cout << b;
		}
	cout << endl;
	}
		
	  _getch();

}


Pls help me to enter the spaces ....
Thnx
what does 6 input give?

I think something like this will produce the correct string for each iteration. Looping over that would get the series. It may not be 100%, but its a starting point. You will have to figure out mid and max.

char curr = 'A';
for(i = 0; i < mid; i++)
output[i] = curr++;
for(i = mid; i < max; i++)
output[i] = curr--;
for(i = 0; i < iteration; i++) //assuming iteration 0 is the full string, start at 1 to remove mid, etc...
{
output[mid+i] = ' ';
output[mid-1] = ' ';
}
Last edited on
Thnx m8 :)
Do this one piece at a time. I started by figuring how to print the top line. One trick with problems like this is to run your counter from, say, -6 to 6 and then use the abs() function to get a value that goes from 6 to zero and back to 6:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int n, i;
    cout << "Enter number of lines :\n";
    cin >> n;
    --n;			// it's easier to work with this
    for (i = -n; i <= n; ++i) {
	char ch = 'A' + n - abs(i);
	cout << ch;
    }
    cout << '\n';
}


This method also makes printing the spaces a lot easier too. Let's number the lines 0-6. At line #L, you print a space when abs(i) < L. Otherwise you print the character given above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>

const std::string myString = "ABCDEF";

int main()
{
   for (std::string::size_type i = 0; i <= myString.size(); ++i)
   {
       if ( i == 0)
       {
           std::cout << myString << "G" << std::string(myString.crbegin(), myString.crend()) << "\n";
       }
       else
       {
           std::cout << myString.substr(0, myString.size() - (i-1)) << std::string(2*i - 1, ' ')
            << std::string(myString.crbegin(), myString.crend()).substr(i-1) << "\n";
       }
   }
}

using another variable, std::string myStringReverse = "FEDCBA" for eg, you can work on it directly instead of reversing myString
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    std::size_t n ;
    std::cout << "number of lines? ";
    std::cin >> n ;
    if( n > alphabet.size() ) n = alphabet.size() ;

    std::string str = alphabet.substr( 0, n ) ;
    for( std::size_t i = n ; i > 0 ; --i )
    {
        std::cout << str << std::string( str.rbegin()+1, str.rend() )  << '\n' ;
        str[i-1] = ' ' ;
    }
}

http://coliru.stacked-crooked.com/a/0a16dd7b2c5934ad
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main()
{
   int rows;
   cout << "Input the number of rows: ";
   cin >> rows;
   int mid = rows - 1, last = 2 * mid;

   for ( int i = 0; i < rows; i++ )
   {                                                                                                             
      for ( int j = 0; j <= last; j++ )  cout << ( abs( mid - j ) < i ? ' ': (char)( 'A' + min( j, last - j ) ) );
      cout << endl;
   }
}



Alternatively (and rather similar to @JLBorges),

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

int main()
{
   const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   int rows;
   cout << "Input the number of rows: ";
   cin >> rows;
   int mid = rows - 1;

   string line = ALPHABET.substr( 0, rows );
   line += string( line.rbegin() + 1, line.rend() );

   for ( int i = 0; i < rows; i++ )
   {
      cout << line << endl;
      line[mid-i] = line[mid+i] = ' ';
   }
}
Last edited on
Topic archived. No new replies allowed.