C++ Draw Diamond

Jun 4, 2013 at 9:51am
why always my codes are complicated?
This code is to draw a diamond using C++
the code is working fine, but I need tips to make more simple codes


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
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
   
    int num;
    cout<<"Enter number of rows(odd between 1 and 79) : ";cin>>num;
    if(num%2!=0&&num<=79&&num>0)
    {
    
    int md,mdn=1,spn,sp,star,starn,inc=1;
    starn=num*2;
    star=starn-1;
    spn=num/2;
    for(md=mdn;md<=num+1;md+=2)
    {
                         for(sp=spn;sp>0;sp--)
                         {
                         cout<<" ";                        
                         }
    while(star<starn)
    {                    
    cout<<"*";
    star+=1;
    }
    cout<<endl;
    spn--;
    mdn+=2;
    inc+=2;
    star-=inc;   
}
    if(md>=num+1)
    {

    int md,mdn=1,spn,sp,star=0,starn=num-2,inc=2,stop=0;
    spn=num/2;
    for(md=mdn;md<=num-1;md+=2)
    {
                         for(sp=spn;sp>=num/2;sp--)
                         {
                         cout<<" ";                        
                         }
    while(star<starn)
    {                    
    cout<<"*";
    star+=1;
    }
    cout<<endl;
    spn++;
    mdn+=2;
    star=0;
    starn=starn-inc;  
    
    }
}
}
else
cout<<"You Entred a wrong value";
    
getch();
return 0;
}
Jun 4, 2013 at 10:13am
For example this:
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
#include <iostream>
#include <string>

int main()
{
    int rows(0);
    /* Input */
    std::cout << "Enter number of rows(odd between 1 and 79) : ";
    std::cin >> rows;
    while (rows%2 == 0 || rows < 1 || rows > 79) {
        std::cin.clear();           // If somebody enters not a number
        std::cin.ignore(100, '\n'); // We will skip it and clear errors
        std::cout << "You have entred a wrong value\n" <<
                     "Enter number of rows(odd between 1 and 79) : ";
        std::cin >> rows;
    }
    /* Top part */
    for(int i = 0; i <= rows/2; ++i)
        std::cout << std::string(rows/2 - i, ' ') <<
                     std::string( i * 2 + 1, '*') << '\n';
    /* Bottom part */
    for(int i = rows/2 - 1; i >= 0; --i)
        std::cout << std::string(rows/2 - i, ' ') <<
                     std::string( i * 2 + 1, '*') << '\n';
    /* Wait for enter */
    std::cin.ignore(100, '\n');
    std::cin.get();
}
#include <iostream>
#include <string>
#include <cmath>

int main()
{
    int rows(0);
    /* Input */
    std::cout << "Enter number of rows(odd between 1 and 79) : ";
    std::cin >> rows;
    while (rows%2 == 0 || rows < 1 || rows > 79) {
        std::cin.clear();           // If somebody enters not a number
        std::cin.ignore(100, '\n'); // We will skip it and clear errors
        std::cout << "You have entred a wrong value\n" <<
                     "Enter number of rows(odd between 1 and 79) : ";
        std::cin >> rows;
    }
    const int half = rows / 2;
    for(int i = 0; i < rows; ++i) {
        const int spaces = std::abs(half - i);
        const int stars  = std::abs(spaces - half) * 2 + 1;
        std::cout << std::string(spaces, ' ') <<
                     std::string( stars, '*') << '\n';
    }
    /* Wait for enter */
    std::cin.ignore(100, '\n');
    std::cin.get();
}
EDIT: added single loop too.
It isn't supporting hollow diamond like coder777 one.
Last edited on Jun 4, 2013 at 1:10pm
Jun 4, 2013 at 12:51pm
The whole thing with a single loop:
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
#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
  int rows(0);
  /* Input */
  std::cout << "Enter number of rows(odd between 1 and 79) : ";
  std::cin >> rows;
  while (rows%2 == 0 || rows < 1 || rows > 79)
  {
    std::cin.clear();           // If somebody enters not a number
    std::cin.ignore(100, '\n'); // We will skip it and clear errors
    std::cout << "You have entred a wrong value\n" <<
                 "Enter number of rows(odd between 1 and 79) : ";
    std::cin >> rows;
  }
  const int mid = (rows / 2);
  for(int i = 0; i < rows; ++i)
  {
    const int w = abs(mid - i);
    std::cout.setf(std::ios::right);
    std::cout.fill(' ');
    std::cout.width(w + 1);
    std::cout << "*";
    if(w != mid)
    {
      std::cout.fill(' '); // Note: change to '*' if diamond should be filled
      std::cout.width(rows - (2 * w) - 1);
      std::cout << "*";
    }
    std::cout << "\n";
  }
  return 0;
}
Jun 4, 2013 at 2:44pm
Recursive:
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
#include <iostream>
#include <string>

    using namespace std;

void diamond(int size, std::string stars="*")
{
    string temp = string((size-1)/2, ' ') + stars;
    cout << temp << endl;
    if (size>1) 
    {
        diamond(size-2, stars+"**");
        cout << temp << endl;
    }    
}

int main()
{   
    int num;
    cout << "Enter number of rows(odd between 1 and 79) : ";
    cin >> num;
    while (!((num%2) && (num<=79) && (num>0)) )
    {
        cout << "Out of range, please try again: ";
        cin >> num;    
    }

    diamond(num);
    return 0;
}
Jun 6, 2013 at 3:29pm
Wow, I kept tracking this topic from the second I posted this problem and in few hours I got several replays!

+1 for the users, +1 for the forums.
Topic archived. No new replies allowed.