Trying to understand the logic behind creating shapes

I just started doing some object oriented programming and I've run into some trouble with creating shapes out of text. I'm trying to understand the logic behind creating a diamond. I feel like I'm slow but could someone help me to understand how this works?

I am really struggling, I think because all the nested for loops are throwing me off. Any and all help is appreciated!


#include <iostream>
using namespace std;
int main()
{
int i;

for( i=0;i<3;i++)
{
for(int j=3;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;
}
for(i=3;i>0;i--)
{
for(int j=3;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;

}
for(int i=0;i<3;i++)
{
for(int j=3;j>i;j--)
cout<<" ";
for(int k=0;k<i;k++)
cout<<" *";
cout<<endl;

system("pause");
return 0;
}
}
//I give you simple code for making any pattern.
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
#include <iostream>
using namespace std;
int main()
{
int pat[7][7]={{0,0,0,1,0,0,0},
                       {0,0,1,0,1,0,0},
                       {0,1,0,0,0,1,0},
                       {1,0,0,0,0,0,1},
                       {0,1,0,0,0,1,0},
                       {0,0,1,0,1,0,0},
                       {0,0,0,1,0,0,0},

             };

     for(int i=0; i<7; i++)
     {  for(int j=0; j<7; j++){
         if(pat[i][j]){
            cout<<"*"; 
         }else{
         cout<<" ";
         }

         }
         cout<<endl;
     }


    return 0;
}
Awwwhhh. That's cheating, sujitnag. Way to ruin the fun. :P

Let's go through some algorithmic thoughts on how to print shapes.

Whenever you are designing an algorithm to print a shape you need to think about a few things.
1) What shape am I printing?
2) How wide do I want it to be?
3) What characters need to be printed?
4) What order do these need to be printed?

So, in your case, you are printing a diamond, let's say five wide, made out of asterisks (*) and spaces. The order these need to be printed in will be spaces first and then asterisks. This is because in order to make sure that the top and bottom of the diamond are in line with the middle, you need to space them over. So, let's make an algorithm!

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
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>

using namespace std;

int main()
{
    // Diamond
    // 5 Wide
    // Made of *'s and spaces.
    int size = 5;

    // There are a few things to control when
    // printing shapes. First you need to control
    // the current line you are printing. So,
    // make a loop for it.

    // You need to print the upper half first.
    // Both halves need to be printed separately.
    for (int i = 0; i < size; ++i)
    {
        // Now, we need to think about how many spaces
        // come before out asterisk.
        for (int sp = 0; sp < size - i - 1; ++sp)
            cout << " ";

        // Now that we have spaced out, print all of
        // your asterisks to make the diamond.
        for (int ast = 0; ast < i + 1; ++ast)
            cout << "*";

        // Go to the next line
        cout << endl;
    }

    // Now print the lower half of the diamond, by
    // doing what you did earlier in reverse.
    // All you have to do is swap the conditions on
    // the two inner for loops and tweak it a bit.
    for (int i = 0; i < size - 1; ++i)
    {
        // Number of spaces is dependent on the current line (i).
        for (int sp = 0; sp < i + 1; ++sp)
            cout << " ";

        // Number of asterisks is dependent on the current line and the size.
        for (int ast = 0; ast < size - i - 1; ++ast)
            cout << "*";

        // Go to the next line
        cout << endl;
    }
    return 0;
}


I tried to explain as much as possible in the code comments, but just go through it and think about it and try to understand. If you have any questions, let us know.

Tresky
Last edited on
@Tresky
are you joking.
what do you mean by algorithm.
every program follow a algorithm.






Topic archived. No new replies allowed.