Need to fill in a hollow shape.

this is a diamond generator it generates diamonds that are hollow. But if someone knows how to make them full diamonds i would really appreciate it.

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 <cstdlib>
using namespace std;
int main()
{
    int rows=0;
    char item;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    cout<< "What would you like the diamond to be made of?";
    cin >> item;
    
    rows = rows /2;
    
    for ( int r=-rows; r <= rows;r++){
    for (int c =-rows;c <= rows;c++){
  if  (abs (r)+ abs(c)==rows)cout<<item;
		else cout <<" ";
}
cout <<endl;
}
return 0;
}
Last edited on
What does line 19 do?
Outputs a blank space.
In case it wasn't clear, cire was trying to help you. He knows what line 19 does, do you?
Yes it is responsible for the spaces but if I remove it and replace it with "item" it prints almost like a box shape. I also updated the code I was outputting item twice
Last edited on
if i delete that line it will not print.
I like way you run from -rows to rows. This is a common problem and in the past I've always told people to do it with 2 loops. This is nicer.

To do a problem like this, start with pencil and paper and write out a where things go. In this case, you want to know where the first and second marks. For example, with 5 rows:
Row (r)	1st mark	2nd mark
-2	3		3
-1	2		4
 0	1		5
 1	2		4
 2	3		3

Now determine a formula for the values:
 r	|r|+1		5-|r|

Now write code for this:
1
2
3
4
5
6
7
    int halfRows = rows/2;
    for (int r = -halfRows; r <= halfRows; r++) {
        int c = 1;
        // loop to print spaces and increment c until c == abs(r)+1;
        // loop to print marks and increment c until c > rows-abs(r)
        cout << '\n';
    }

You may find that this doesn't quite work when rows is even. Just add code near the top to increment rows if it's even.
Topic archived. No new replies allowed.