Printing pattern

Pages: 12
Hello Im supposed to print diamond pattern which look like
--#--
-###-
##+##
-###-
--#--

The user decides the size of it.
I know how to print a diamond pattern with a single character but this have many characters
Make a string. Print the string
Not like that
I need like the algorithm to print any size of that kind of pattern
Do you really mean for the middle row to have a '+' in the middle?

Try doing it without the + first. Make a table with the row number, the number of dashes
and the number of hashes:
Row   Dashes   Hashes
0     2        1
1     1        3
2     0        5
3     1        3
4     2        1

Do you see the pattern in the number of dashes and hashes?

Now here's a tip. I arbitrarily chose the row number as going from 0 to 4. Instead, let's go from -2 to +2:

Row   Dashes   Hashes
-2    2        1
-1    1        3
 0    0        5
 1    1        3
 2    2        1

Can you come up with a formula for the number of dashes and hashes based on the row number? Once you have that, it's just a for loop.

[ edit to correct the last row ]
Last edited on
Let me figure out the formula
Can't figure it out
I didn't have the last row number correct. It should be 2 instead of -2. I edited my post.

Clearly Dashes = abs(row).

Suppose N is the number of rows (5 in the example). The Hashes = N - 2*abs(Row)
Ok now it make sense
What about the loops
Guys I did the code without the "+" sign but now I need help if some one can add the algorithm of putting the "+" sign




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
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    clrscr();
    int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
  for(c=1; c<=space; c++)
  {
    cout<<"-";
  }
  
  for(c=1; c<=(2*k-1); c++)
  {
    cout<<"#";
  }
   for(c=1; c<=space; c++)
  {
    cout<<"-";
  }
  space--;

  cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
  for(c=1; c<=space; c++)
  {
    cout<<"-";
  }
  
  for(c=1 ; c<=(2*(n-k)-1); c++)
  {
    cout<<"#";
  }
   for(c=1; c<=space; c++)
  {
    cout<<"-";
  }
   space++;
  cout<<"\n";
    }
    getch();
}
The following are example
Size:3
--#--
-###-
##+##
-###-
--#--

Size:4
---#---
--###--
-##+##-
##+++##
-##+##-
--###--
---#---

Size:5
----#----
---###---
--##+##--
-##+++##-
##+++++##
-##+++##-
--##+##--
---###---
----#----

Last edited on
> for(c=1; c<=(2*k-1); c++)
You're not being charged by the byte for your storage, so start using meaningful names for things.

Also, stop trying to write the program without a clear understanding of the underlying pattern.
Before you even begin to type any code at all, you need to complete dhayden's table to include the pluses column.

Then you write the code to generate that table - nothing more, nothing less.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main()
{
  cout << "Enter number of rows (for diamond dimension) : ";
  int nRows;
  cin >> nRows;
  for ( int row = 0 ; row < nRows ; row++ )
  {
    int numDashes = row;  // was (2*k-1)
    int numHashes = row;
    int numPluses = row;
    cout << row << " "
         << numDashes << " "
         << numHashes << " "
         << numPluses << endl;
  }
}

This is the hard part, just getting the maths right.
You need to study the patterns, and fix lines 10,11,12 to regenerate the table you crafted by hand on paper.

If you can do that, you're done with the hard work.




It's plain sailing after that.
1
2
3
4
5
6
7
8
for ( int row = 0 ; row < nRows ; row++ ) {
  printSomeChars(numDashes,'-');
  printSomeChars(numHashes,'#');
  printSomeChars(numPluses,'+');
  printSomeChars(numHashes,'#');
  printSomeChars(numDashes,'-');
  cout << endl;
}


Where
1
2
3
void printSomeChars(int n, char c) {
  for ( int i = 0 ; i < n ; i++ ) cout << c;
}

1
2
3
  printSomeChars(numHashes,'#');
  printSomeChars(numPluses,'+');
  printSomeChars(numHashes,'#');   // nearly, but ... not quite 
How do I call the void printSomeChars
Should I put it inside the main or outside
And the printing part is kinda confusing
Forget about the printed pattern.

Focus on printing the table.

The pattern will follow later.

But based on your "how do I call a function" comment, I wonder whether you've paid any attention in class, or any attention to any of the replies of your 162 messages so far.

Messages which mostly boil down to "please do my homework for me".

I did everything except the printing of hashes I'm stuck

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
#include<iostream>
using namespace std;

void printSomeChars(int n, char c) {
  for ( int i = 0 ; i < n ; i++ )
   cout << c;
  }
  
int main()
{
  cout << "Enter number of rows (for diamond dimension) : ";
  int nRows;
  cin >> nRows;
  int numDashes;
  int numHashes;
  int numPluses;
  for ( int row = 0; row <nRows; row++ )
  {
    numDashes=(nRows-row)-1;   
    numHashes =(2*row-1);
    numPluses = 2*row-3;
    
  printSomeChars(numDashes,'-');
  printSomeChars(numHashes,'#');
  printSomeChars(numPluses,'+');
  printSomeChars(numHashes,'#');
  printSomeChars(numDashes,'-');
  cout << endl;             
  }
}
Last edited on
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;

void draw( int n, int i )
{
   string line = string( i, '-' ) + string( min(n-i,2), '#' ) + string( max(2*(n-i)-5, 0), '+' ) + string( min( n-1-i, 2 ), '#' ) + string( i, '-' ) + '\n';
   cout << line;
   if ( i )
   {
      draw( n, i - 1 );
      cout << line;
   }
}

int main()
{
   for ( int N = 3; N <= 5; N++ )
   {
      cout << "\nSize " << N << ":\n";
      draw( N, N - 1 );
   }
}

Sorry lastchance thanks for the help but can you add comments so that I can learn much better.
Thanks
Size:5
----#----
---###---
--##+##--
-##+++##-
##+++++##
-##+++##-
--##+##--
---###---
----#----

Ugh. I wish you'd given this example earlier. Your first example made me think that the central row was supposed to contain a single '+' and everything else was a dash or hash.

Please post the full text of the assignment.
Dee5 wrote:
Sorry lastchance thanks for the help but can you add comments so that I can learn much better.


Well, it was done by recursion - more to deter you from straight copying than anything else. However, a few comments:
- it is easier to take a looping version from i = -(n-1) to (n-1) than from 0 to 2n-1
- on each row you need formulae for:
--- the number of '-' (same on both sides)
--- the number of '#' on the left side (mainly 2, except in first and last rows)
--- the number of '+'
--- the number of '#' on the right side (cannot be the same as the left in the first two and last two rows).

Work out those number formulae using a table. They are basically linear, with clamping to 0 (minimum) and, in a couple of cases with '#', to 2 (maximum).
Last edited on
Pages: 12