Nested Loop (Making A Diamond)

Hello,
Please take a look at my code and let me know how I can make my code more efficient or any tips you would like to add.
Thanks in advance.

The objective of this code is to make a diamond using c++ (WITHOUT USING SETW AND ONLY USING NESTED LOOPS)

/////CODE BEGIN//////
#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
/*
EXAMPLE OF A SIZE 5 DIAMOND
----x
---x-x
--x---x
-x-----x
x-------x
-x-----x
--x---x
---x-x
----x
*/
int size, x, xx, y;
cout << "Size?" << endl;
// size = 5;
cin >> size;

//Top Begin
for (x = 1; x < size; x++)
{
cout << "-";
}
cout << "*" << endl;
//Top End

for (y = 1; y < size; y++)
{
for (x = y; x < size - 1; x++)
{
cout << "-";

}
cout << "x";

for (xx = 0; xx < y; xx = xx++)
{
cout << "-";
}

for (xx = 1; xx < y; xx++)
{
cout << "~";
}

cout << "x" << endl;
}

for (y = 1; y < size-1; y++)
{

for (x = 0; x < y; x++)
{
cout << "-";
}
cout << "x";
for (xx = size-2; xx > y; xx--)
{
cout << "~";
}

for (x = y+1; x < size; x++)
{
cout << "-";
}

cout << "x" << endl;

}

//Bottom Begin
for (x = 1; x < size; x++)
{
cout << "-";
}
cout << "*" << endl;
//Bottom End

system("pause");
return 0;
}
You could use a lot less loops.

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

const char O = ' ';
const char X = '*';

int main()
{
   int M;
   int i, j;
   int first, last;

   cout << "Input size: ";   cin >> M;

   for ( i = 1; i <= 2 * M - 1; i++ )
   {
      first = abs( M - i ) + 1;
      last  = 2 * M - first;
      for ( j = 1; j <= last; j++ )
      {
         if ( j == first || j == last )  cout << X;
         else                            cout << O;
      }
      cout << endl;
   }
}

Last edited on
Thanks that sure does help! Is there an easy way to modify this to make the diamond horizontal instead of vertical?
make the diamond horizontal instead of vertical


I'm not quite sure what you mean here.

Characters go across one unit for every descent by one line, so the final aspect ratio of the diamond depends mainly on the height-to-width aspect ratio of characters on your console.
Last edited on
Never mind I looked at the diamond and it seems to look the same either way it is turned.

How did you come up with the following parts:

i = 1; i <= 2 * M - 1; i++

first = abs( M - i ) + 1;
last = 2 * M - first;

I just don't think I would have came up with that as easily as you did.

You need to seek for a pattern - largely by "try and see". What follows is how I saw the problem, but when I tried it on some colleagues they saw it a different way. Each to his own; a psychologist would probably have a field day here.

'first' is the position of the left-hand *; 'last' is the position of the right-hand *.

The position of 'first' runs:
1st row: M
2nd row: M-1
Mth row: 1 - for the ith row in the upper part, 'first' is M - i + 1
then
(M+1)th row: 2
(M+2)th row: 3
etc. - for the ith row in the lower part, 'first' is i - M + 1
A composite formula working for both parts is abs(M-i)+1. There are 2M-1 rows in total.

If you can find 'first' then you can find 'last'; since one always increases by 1 when the other decreases by 1 their sum is constant; i.e
last + first = constant ( = M+M from the first row)
Hence
last = 2M-first


You made a good attempt (but please use code tags when you post), so I don't mind giving you code.
Last edited on
(but please use code tags when you post), so I don't mind giving you code.



Sorry I am new to this... code tags? Do you mean post my code in between the "source code" button?
Last edited on
If you enter a reply, or go back and edit your posts, you will see to the right of the text entry region a set of formats that can be applied. Select your source code with a mouse and press the <> in this table of possible formats. It will put the selected region within code tags.

Unfortunately, this doesn't seem to work in the initial posting. What you can do, however, is immediately after posting a new problem, go back, edit your post and apply these tags. (You can also put the relevant tags in by typing, but it's quicker with a mouse.)

The two big advantages of code in code tags are:
(a) indenting is preserved, making it much easier for us to see the structure of your code;
(b) a little "gear wheel" appears at the upper right-hand corner; pressing it allows you to try the code in C++ shell, which works as long as there are no file read/writes.
If you enter a reply, or go back and edit your posts, you will see to the right of the text entry region a set of formats that can be applied. Select your source code with a mouse and press the <> in this table of possible formats. It will put the selected region within code tags.


Alright I understand. Thanks for being patient.
Topic archived. No new replies allowed.