Hi, I'm new to the whole programming environment. I'm currently taking an object oriented programming class and am a bit lost on my first assignment.
I know there is already thread for how to draw a diamond but it doesn't answer my question. I'm trying to draw a diamond with border characters and fill characters. The code I have so far draws the border characters but I'm not sure where to begin for the fill characters. I have tried a few different things but all them seem to do is make a square of fill characters. I'm not sure if I'm making any sense but any advice would be much appreciated. Thanks.

Sheri

void Diamond:: Draw()
{
  int h, g, i;

  for(h = 0; h < side; h++)
  {
    for(g = 0; g < side; g++)
    {
      if(h < side/2)
      {
        if(g == side/2 - h || g == side/2 + h)
        {
          cout << border;
        }
        else
        {
          cout << " ";
        }
      }
      else
      {
        if(g == h - side/2 || g == side - (h - side/2) - 1)
        {
          cout << border;
        }
        else
        {
          cout << " ";
        }
      }
    }
    cout << '\n';
  }
}

Recommended Answers

All 4 Replies

It is hard to determine what you require. Can you supply a textual pattern of what the design should look like after it is drawn?

Where do the values of "side" and "border" come from?

This is what it is supposed to look like:

           X 
          X X
         X O X
        X O O X
       X O O O X
      X O O O O X
     X O O O O O X
    X O O O O O O X
   X O O O O O O O X
  X O O O O O O O O X
 X O O O O O O O O O X
X O O O O O O O O O O X
 X O O O O O O O O O X
  X O O O O O O O O X
   X O O O O O O O X
    X O O O O O O X
     X O O O O O X
      X O O O O X
       X O O O X
        X O O X
         X O X
          X X
           X 

The values of the border and fill are hard-coded into the main function of the program. I'm writing the definition for the draw function that is called from main with the three parameters of side, border & fill characters. Right now all its giving me are the X's making an outline for the diamond.

That's supposed be the shape of a diamond:

X
X X
X O X
X O O X
X O O O X
X O O O O X
X O O O O O X
X O O O O O O X
X O O O O O O O X
X O O O O O O O O X
X O O O O O O O O O X
X O O O O O O O O O O X
X O O O O O O O O O X
X O O O O O O O O X
X O O O O O O O X
X O O O O O O X
X O O O O O X
X O O O O X
X O O O X
X O O X
X O X
X X
X

That's supposed be the shape of a diamond:

X
X X
X O X
X O O X
X O O O X
X O O O O X
X O O O O O X
X O O O O O O X
X O O O O O O O X
X O O O O O O O O X
X O O O O O O O O O X
X O O O O O O O O O O X
X O O O O O O O O O X
X O O O O O O O O X
X O O O O O O O X
X O O O O O O X
X O O O O O X
X O O O O X
X O O O X
X O O X
X O X
X X
X

Here are changes to your Draw() function that will get you your fill character--I set it to "O" like your pattern. You still need some code to push the pattern out to form a diamond (opposite triangle) on the left. but you are close. I did not notice until now that the pattern you provided was striped of spaces on the left.

Look at the gaps in the lines output too--your program is skipping every other line in the pattern...

Give it a shot and see what you come up with...

int side = 12;
char border = 'X';
char fillCh = 'O';
void Draw()
{
  int h, g;
 
  for(h = 0; h < side; h++)
  {
    for(g = 0; g < side; g++)
    {
      if(h < side/2)
      {
        if(g == side/2 - h || g == side/2 + h)
        {
          cout << border;
        }
        else if (g > side/2 - h && g < side/2 + h)
        {
          cout << fillCh;
        }
		else
			cout << " ";
      }
      else
      {
        if(g == h - side/2 || g == side - (h - side/2) - 1)
        {
          cout << border;
        }
        else if (g > h - side/2 && g < side - (h - side/2) - 1)
        {
          cout << fillCh;
        }
		else
			cout << " ";
      }
    }
    cout << '\n';
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.