Help with stripped diamond

Hi I need to print a diamond with 2 symbols so that the two symbols loos like stripes, however I am not sure of how to get the symbols to alternate .



This is my function to print the regular diamond
void Ascii::printDiamond(int width, char symbol1, char symbol2)

{



for (int i = width; i >= 1; i--) {



for (int j = 1; j < i; j++) {

cout << " ";

}

for (int k = width; k >= i; k--) {

cout << symbol1 << " ";

}

cout << endl;

}

for (int l = 1; l <= width - 1; l++) {

for (int j = 1; j <= l; j++) {

cout << " ";

}

for (int k = width - 1; k >= l; k--) {

cout << symbol2 << " ";

}

cout << endl;

}

}
Last edited on
@@kassandra

This should be of help to you.

I also got rid of the L variable and replaced it with the i variable, since the first loop using the i variable was finished, and wasn't needed .

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
void  Ascii::printDiamond(int width, char symbol1, char symbol2)
{
	int i, j, k;

	for (i = width; i >= 1; i--)
	{
		for (j = 1; j < i; j++)
		{
			cout << " ";
		}

		for (k = width; k >= i; k--)
		{

			if(i%2==0)
			cout << symbol1 << " ";
			else
				cout << symbol2 << " ";
		}
		cout << endl;
	}

	for (i = 1; i <= width - 1; i++) 
	{

		for (j = 1; j <= i; j++)
		{
			cout << " ";
		}

		for (k = width - 1; k >= i; k--) 
		{
			if(i%2==0)
			cout << symbol2 << " ";
			else
				cout << symbol1 << " ";
		}
		cout << endl;
	}
}
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>
#include <algorithm>
using namespace std;

void printDiamond( int width, char symbol1, char symbol2 )
{
   for (int i = 1, step = 1; i; i += step )
   {
      cout << string( width - i, ' ' );
      for ( int j = 1; j <= i; j++ ) cout << symbol1 << ' ';
      cout << '\n';
      swap( symbol1, symbol2 );
      if ( i == width ) step = -1;
   }
}

int main()
{
   printDiamond( 8, '*', 'O' );
}


       * 
      O O 
     * * * 
    O O O O 
   * * * * * 
  O O O O O O 
 * * * * * * * 
O O O O O O O O 
 * * * * * * * 
  O O O O O O 
   * * * * * 
    O O O O 
     * * * 
      O O 
       * 
Last edited on
@whitenite1 thanks but I am still trying to get the code so that the stripes are diagonal. I got the first half of the diamond by using (k%2==0) but I still cant seem to get the bottom half to match.

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

void printDiamond( int width, char symbol1, char symbol2 )
{
   string alt( 1, symbol1 );   alt += symbol2;
   
   for (int i = 1, step = 1, sym0 = 0; i; i += step )
   {
      cout << string( width - i, ' ' );
      for ( int j = 1, sym = sym0; j <= i; j++, sym = 1 - sym ) cout << alt[sym] << ' ';
      cout << '\n';
      if ( i == width ) step = -1;
      if ( step > 0 ) sym0 = 1 - sym0;
   }
}

int main()
{
   printDiamond( 8, '*', 'O' );
}


       * 
      O * 
     * O * 
    O * O * 
   * O * O * 
  O * O * O * 
 * O * O * O * 
O * O * O * O * 
 O * O * O * O 
  O * O * O * 
   O * O * O 
    O * O * 
     O * O 
      O * 
       O 


If you want the stripes going the other way change it to
if ( step < 0 ) sym0 = 1 - sym0;
Last edited on
Topic archived. No new replies allowed.