Diamond Class. Please Help

Pages: 12
I am in an object oriented programming class and I need to figure out how to get this to print out properly. it prints out in a diamond shape but it is missing the point at the bottom and it is kind of messed up in the middle. If someone can please help me fix this code I would be so grateful! I only have until tomorrow night!

*and i'm only assigning int side a number for simplicity purposes. In my program it would be declared with the class diamond and passed in as a parameter*
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
int main()
{


int side = 6;


  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 << "*"; //filchar;
        }
		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 << "*";//filchar;
        }
		else
			cout << " ";
      }
    }
    cout << '\n';
  }
}
This is my output:

its not printing the correct number of sides (it is only printing 4) and the top '#' isn't centered.
PLEASE help! I would be so grateful!

#
#*#
#***#
#****#
#**#
##

it is automatically pulling the rest to the left side of the screen. but if you copy the code into an IDE and compile you will see exactly how it outputs
Last edited on
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
int h, g;

  for(h = 0; h < side; h++)
  {
    for(g = 0; g < side; g++)
    {
      if(h < side/2)
      {
        if(( g == side/2 - h - 1 && 0==side % 2 ) || ( g == side/2 - h && 1==side % 2 ) || g == side/2 + h) //magic 
        {
          cout << "#";//border;
        }
        else if (g > side/2 - h -1 && g < side/2 + h+1)
        {
          cout << "*"; //filchar;
        }
		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 << "*";//filchar;
        }
		else
			cout << " ";
      }
    }
    cout << '\n';
  }
@poteto:

Hey Thanks a lot for looking at my code. The only thing is that at the top and the bottom I only need one border character instead of two. And also for the middle I only need one line the separates the two "triangles" instead of two of the same sized lines. How would I implement that?
and I am not sure if this is relevant, but my int side = 6 needs to be 6 characters in length on each point of the diamond. I.E, the perimeter would be 6 * 4 = 24
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][4]=  {{2,0,0,0},  //just modifying the array to get proper shape
                       {2,2,0,0},
                       {2,1,2,0},
                       {2,1,1,2},
                       {2,1,2,0},
                       {2,2,0,0},
                       {2,0,0,0},

                    };

     for(int i=0; i<7; i++)
     {  for(int j=0; j<4; j++){
         switch(pat[i][j]){
         case 1:   cout<<"*";break;
		 case 2:cout<<"#";break; 
          default:  cout<<" ";break;
         }

         }
         cout<<endl;
     }


    return 0;
}



out put:

#
##
#*#
#**#
#*#
##
#

Last edited on
I can't use arrays for this assignment. I can only use loops
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
int h=9;// it should be odd;if even it work but small change in image.
// only h value need to change 
int w=(h-1)/2;
int k=0;
for(int i=0;i<h;i++)
   {   
   if(i>w) {k=(h-1)-i;   }else{k=i;   }
       
   	 for(int j=0;j<=k;j++){
        if(j==0 or j==k){cout<<"#";}
		 else{cout<<"*";}
		       	
        }
    cout<<endl;
   	 }
   	
   	   return 0;
}
Last edited on
it has to be even or odd because i'm building a class and the side has to be passed in as a parameter
first copy the code and run it.
test to change h value.
#
##
#*#
#**#
#***# //// if even
#***# ////h=10
#**#
#*#
##
#



#
##
#*#
#**#
#***# // if odd h=9
#**#
#*#
##
#

if you want 2 pic in every case the you change even no to odd by +/- 1 internaly;
similarly if you want 1 pic in every case the you change odd no to even by +/- 1 internaly;
Last edited on
but I need both those outputs combined to make a full diamond, not just half a diamond. I am not sure what you mean by changing the even to odd and odd to even internally by +/- 1
.
Last edited on
boss, draw your shape first.
from your 2nd post i think you need
#
##
#*#
#**#
#***#
#**#
#*#
##
#

i
Here is my new and revised code (and Size is passed in as a parameter and my border and fill character are already set):
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

void Diamond::Draw()
{
    for (int i = 0; i < Size*2-1; i++)
    {
        for(int k = 0; k < Size*2-1; k++)
        {
            if(i < (Size*2-1/2))
            {
                if( k == (Size*2-1)/2 - i - 1 && 0 == (Size*2-1)%2 ||(k == (Size*2-1)/2 - $
                {
                    cout << Border;
                }
                else if(k > (Size*2-1)/2 - i - 1 && k < (Size*2-1)/2 + i + 1)
                {
                    cout << Fill;
                }
                else
                    cout << " ";
            }
            else
            {
                if(k == i - (Size*2-1/2) || k == (Size*2-1) - i -(Size*2-1)/2 - 1)
                {
                    cout << Border;
                }
                else if (k > i - (Size*2-1)/2 && k < (Size*2-1) -i - (Size*2-1)/2 - 1)
                {
                    cout << Fill;
                }
                else
                    cout << " ";
            }
                
        }

        cout << "\n";
    }
}




this is the output that I keep getting for 7 units being passed in through Size:

d2 has size = 7 units.
      ^      
     ^*^     
    ^***^    
   ^*****^   
  ^*******^  
 ^*********^ 
^***********^
*************
*************
*************
*************
*************
*************


the top part looks perfect! I just can't seem to fix the bottom. Please help! my code is due by midnight tonight
what is size?
|
|
---------------w---
|
|
h
wait i give you code within 1/2 hour
like my class is diamond.

so if you put:

int main()
{
diamond d1(7);

d1.draw();
}

it would pass in the parameter as 7 which would equal Size.

int Size is part of my private member data
Last edited on
awesome I would greatly appreciate it!! I have been at this for like the past two days and the draw function is the only one I cannot get to work properly (well the bottom half of the diamond I can't get to work properly). Once I can get that, my code will be completely done.
You've made this too difficult. The trick is to print the top half and the bottom half separately. I suggest you do the following:
1. Write void printRow(unsigned size) that will write the printable characters of width size. For example:
printRow(1) prints
^
printRow(2) prints
^^
printRow(3) prints
^*^
printRow(4) prints
^**^

Test this out in a main program:
1
2
3
main() {
for (unsigned i=0; i<10; ++i) printRow(i);
}


2. Write a function printSpaces(unsigned n) that prints n spaces.

3. Now Diamond::Draw is
1
2
3
4
5
6
7
8
9
10
11
12
13
Diamond::Draw()
{
    // print the top half
    for (int i=1; i<Size; ++i) {
	printSpaces(someformula);
	printRow(someformula);
    }
    // print the bottom half
    for (int i=Size; i>0; --i) {
	printSpaces(someformula);
	printRow(someformula);
    }
}

I'll leave it to you to figure out what the formulas are for the number of spaces and characters in each row.
// its work

#include <iostream>
using namespace std;

class diamond{

private:
int size;
public:
diamond(int s){
size=s;
}
void draw(){
//int size=size;// it ssizeould be odd;
int w=(size-1)/2;
int k=0;

for(int i=0;i<size;i++)
{ //k=2*i+1 ;
if(i>w) {k=(size-1)-i; }else{k=i; }

for(int j=0;j<size;j++){
if(j==w-k or j==w+k){cout<<"#";}
if(j>w-k and j<w+k){cout<<"*";}
if(j<w-k or j>w+k){cout<<" ";}

}
cout<<endl;
}


}

};

int main()
{
diamond d{7};
d.draw();

return 0;
}
Pages: 12