#include <iostream>
#include <cstdlib>
usingnamespace 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;
}
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
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:
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.