### 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 .

 ``1234567`` ``````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 ...

 ``1234567891011121314151617181920212223`` ``````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:
 ``123456789101112131415161718`` ``````#include 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.
 ``1234567891011121314151617181920`` ``````#include #include 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
 ``12345678910111213141516171819`` ``````#include #include 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] = ' ' ; } }``````

Last edited on
 ``123456789101112131415161718`` ``````#include #include #include 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),

 ``123456789101112131415161718192021`` ``````#include #include 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.