# DIAMOND SHAPE

Discussion in 'C++' started by coinjo, Nov 10, 2005.

1. ### coinjoGuest

I need to write a program which takes a number as an input and prints a
diamond of # and \$. The number of rows in the shape is equal to the
number entered by the user. My program should display the shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.

#
#\$#
#\$#\$#
#\$#\$#\$#
#\$#\$#
#\$#
#

If user enters a value 6, the program prints diamond in following
shape.

#
#\$#
#\$#\$#
#\$#\$#
#\$#
#

I have tried very hard and this is the best i could come up with

#include<iostream>
using namespace std;

int main()
{

int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;

int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int a=0;
int b=0;

space1=rows/2;
space2=space1;
space3=space1;
a=rows-2;
space=space1+a;

int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;

int char1=1;
int char2=0;

b=a-1;

char2=space1+b;

while(count1<space1)
{

count2=0;
count3=0;

while(count2<space2)
{

cout<<" ";
count2=count2+1;

}

while(count3<char1)
{

cout<<"#";
count3=count3+1;

if(count3<char1)

{

cout<<"\$";
count3=count3+1;

}
}

cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;

}

while(count<=space)
{
cout<<"#";
count=count+1;
if(count<space)
{
cout<<"\$";
count=count+1;

}
}

cout<<endl;

while(count4<=space3)
{

count5=0;
count6=0;

while(count5<space4)
{

cout<<" ";
count5=count5+1;

}

while(count6<char2)
{

cout<<"#";
count6=count6+1;

if(count6<char2)
{

cout<<"\$";
count6=count6+1;

}
}

space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;

}
}

This above code works fine for 3 but not for other odd values like 5
and 7. Can anyone help?
Also how to make it work for even numbers?

coinjo, Nov 10, 2005

2. ### Guest

coinjo wrote:
> I need to write a program which takes a number as an input and prints a
> diamond of # and \$. The number of rows in the shape is equal to the
> number entered by the user. My program should display the shape for
> both even and odd value of size. For example if user enters number 7
> the program should print following shape.

[snipped ASCII art]

> I have tried very hard and this is the best i could come up with
>
> #include<iostream>
> using namespace std;
>
> int main()
> {
>
> int rows=0;
> cout<<"Enter the number of rows of the diamond"<<endl;
> cin>>rows;
>
> int space=0;
> int space1=0;
> int space2=0;
> int space3=0;
> int space4=1;
> int a=0;
> int b=0;

[snipped rest of code]

You should use less variables and think about your look conditions a
little more.

If you can, pick up a copy of "Accelerated C++" by A. Koenig and
B.E.Moo in a local bookstore. One of the first chapters attacks a
problem similars to yours. It won't give you an exact solution for your
problem, but it should push you onto the right track.

Cheers,
Andre

, Nov 10, 2005

3. ### Guest

wrote:
> You should use less variables and think about your look conditions a
> little more.

Of course I meant "loop conditions".

The program can be done with about 2 for loops, that make no
assumptions on the height of the diamond shape.

Cheers,
Andre

, Nov 10, 2005
4. ### coinjoGuest

#include<iostream>
using namespace std;

int main()
{

int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;

int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int temp=0;

space1=rows/2;
space2=space1;
space3=space1;

int count=-1;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;

int char1=1;
int char2=0;

while(rows>=2)
{
temp=rows/2;
count=count+1;
rows=temp;
}

char2=space1+count;

while(count1<=space1)
{

count2=0;
count3=0;

while(count2<space2)
{

cout<<" ";
count2=count2+1;

}

while(count3<char1)
{

cout<<"#";
count3=count3+1;

if(count3<char1)

{

cout<<"\$";
count3=count3+1;

}
}

cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;

}

while(count4<=space3)
{

count5=0;
count6=0;

while(count5<space4)
{

cout<<" ";
count5=count5+1;

}

while(count6<char2)
{

cout<<"#";
count6=count6+1;

if(count6<char2)
{

cout<<"\$";
count6=count6+1;

}
}

space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;

}
}

I have changed the code a bit. Now it works for both 5 and 3 but not

coinjo, Nov 10, 2005
5. ### Neil CeruttiGuest

On 2005-11-10, coinjo <> wrote:
> I need to write a program which takes a number as an input and
> prints a diamond of # and \$. The number of rows in the shape is
> equal to the number entered by the user. My program should
> display the shape for both even and odd value of size. For
> example if user enters number 7 the program should print
> following shape.
>
> #
> #\$#
> #\$#\$#
> #\$#\$#\$#
> #\$#\$#
> #\$#
> #
>
> If user enters a value 6, the program prints diamond in following
> shape.
>
> #
> #\$#
> #\$#\$#
> #\$#\$#
> #\$#
> #
>
> I have tried very hard and this is the best i could come up
> with

Start smaller.

Can you make a program to print the following simple output?

5
#
###
#####

Be careful, because you must not hard-code any number but zero.
Every other number you use in your computations must be obtained
from arithmetic on the input. The only truly hard part in this
problem is figuring out how to compute the values you'll need as

Plus, it must do something sensible with height zero:

0

In other words, a diamond with height zero must not print
anything.

I'll get you started, by solving this smaller problem for you.

#include <iostream>

using namespace std;

int main()
{
int height;
cout << "How tall is your diamond?" << endl;
cin >> height;
for (int i = 0; i < height; i = i + 2) {
for (int j = 0; j <= i; ++j) {
cout << '#';
}
cout << endl;
}
return 0;
}

Easy, right? Good.

Now modify it to add in those necessary leading spaces. Make sure
that your program does the right thing for diamonds of height 1
and 2.

5
#
###
#####

1
#

2
#

Got it? OK. Now modify it so that the start of every line is a #,
and then it alternates between \$ and #.

5
#
#\$#
#\$#\$#

Now you are more than half done. The only remaining step is to
add the code to print the mirror image of your diamond. If it's
not immediately obvious how to do it, then use the same process
as above to "build up" the bottom half of the diamond.

--
Neil Cerutti

Neil Cerutti, Nov 10, 2005
6. ### coinjoGuest

#include<iostream>
using namespace std;

int main()
{

int rows=0;
cout<<"Enter the number of rows of the diamond"<<endl;
cin>>rows;

int space=0;
int space1=0;
int space2=0;
int space3=0;
int space4=1;
int temp=3;

space1=rows/2;
space2=space1;
space3=space1;

int count=0;
int count1=0;
int count2=0;
int count3=0;
int count4=0;
int count5=0;
int count6=0;

int char1=1;
int char2=0;

if(rows>3)
{

while(temp!=rows)
{
temp=temp+2;
count=count+1;
}

}

char2=space1+count;

while(count1<=space1)
{

count2=0;
count3=0;

while(count2<space2)
{

cout<<" ";
count2=count2+1;

}

while(count3<char1)
{

cout<<"#";
count3=count3+1;

if(count3<char1)

{

cout<<"\$";
count3=count3+1;

}
}

cout<<endl;
count1=count1+1;
char1=char1+2;
space2=space2-1;

}

while(count4<=space3)
{

count5=0;
count6=0;

while(count5<space4)
{

cout<<" ";
count5=count5+1;

}

while(count6<char2)
{

cout<<"#";
count6=count6+1;

if(count6<char2)
{

cout<<"\$";
count6=count6+1;

}
}

space4=space4+1;
char2=char2-2;
count4=count4+1;
cout<<endl;

}
}

I have FINALLY come up with this code and i think it is OK for odd
values. Is it correct?

coinjo, Nov 10, 2005
7. ### Arne SchmitzGuest

coinjo wrote:

> I have FINALLY come up with this code and i think it is OK for odd
> values. Is it correct?

Dude, your code is EVIL... Take a look at Neil's code and compare. The
simpler your code, the better. Programming is not only about getting the
program to do what you want, but also to get it to do it in a sensible and
preferably simple way. Try structuring the problem at hand, that is the
first step in mastering an algorithm.

Arne

--
[--- PGP key FD05BED7 --- http://www.root42.de/ ---]

Arne Schmitz, Nov 10, 2005
8. ### Bob HairgroveGuest

On 10 Nov 2005 11:46:41 -0800, "coinjo" <> wrote:

>#include<iostream>
>using namespace std;
>
>int main()
>{
>
>int rows=0;
>cout<<"Enter the number of rows of the diamond"<<endl;
>cin>>rows;
>
>int space=0;
>int space1=0;
>int space2=0;
>int space3=0;
>int space4=1;
>int temp=3;

[snip]

--
Bob Hairgrove

Bob Hairgrove, Nov 10, 2005
9. ### Old WolfGuest

coinjo wrote:

> I need to write a program which takes a number as an input and prints
> a diamond of # and \$. The number of rows in the shape is equal to the
> number entered by the user. My program should display the shape for
> both even and odd value of size. For example if user enters number 7
> the program should print following shape.
>
> #
> #\$#
> #\$#\$#
> #\$#\$#\$#
> #\$#\$#
> #\$#
> #
>
> If user enters a value 6, the program prints diamond in following
> shape.
>
> #
> #\$#
> #\$#\$#
> #\$#\$#
> #\$#
> #
>
> I have tried very hard and this is the best i could come up with

on this thread aren't great either). I hope you can see that if
someone wants to, say, print a diamond of size 20, then your
program will fail hopelessly.

You will save time by thinking of a correct algorithm before you
even sit down at your computer. For example:
- for row number N, I need _____ spaces and _____ hashes

A little bit of thought will reveal that the number of spaces
needed is (N - 1) / 2, discarding any remainder, and the
number of hashes is N (for the first half), and TOTAL + 1 - N
(for the second half).

I recommend you use a function for actually drawing the line:

void draw_line( int num_spaces, int num_hashes )
{
// I'll leave you to fill in this bit
}

Then the main loop of your code is very simple:

int n;
int halfway = (MAX + 1) / 2;

for (n = 1; n <= halfway; ++n)
draw_line( (n - 1) / 2, n );

for (; n <= MAX; ++n)
draw_line( (n - 1) / 2, MAX + 1 - n );

Comprende?

Old Wolf, Nov 10, 2005
10. ### Guest

Old Wolf wrote:
> I recommend you use a function for actually drawing the line:
>
> void draw_line( int num_spaces, int num_hashes )
> {
> // I'll leave you to fill in this bit
> }
>
> Then the main loop of your code is very simple:
>
> int n;
> int halfway = (MAX + 1) / 2;
>
> for (n = 1; n <= halfway; ++n)
> draw_line( (n - 1) / 2, n );
>
> for (; n <= MAX; ++n)
> draw_line( (n - 1) / 2, MAX + 1 - n );
>
> Comprende?

I agree with your overall sentiment, but I don't like the fact that you
break out the loop into two parts. It's easy enough to make this one
look without using if, or anything else.

The end result can be a very clean two piece loop:
for ( row=0... )
for ( col=0... )

Cheers,
Andre

, Nov 10, 2005
11. ### Neil CeruttiGuest

On 2005-11-10, Old Wolf <> wrote:
> coinjo wrote:
>
>> I need to write a program which takes a number as an input and prints
>> a diamond of # and \$. The number of rows in the shape is equal to the
>> number entered by the user. My program should display the shape for
>> both even and odd value of size. For example if user enters number 7
>> the program should print following shape.
>>
>> #
>> #\$#
>> #\$#\$#
>> #\$#\$#\$#
>> #\$#\$#
>> #\$#
>> #
>>
>> If user enters a value 6, the program prints diamond in following
>> shape.
>>
>> #
>> #\$#
>> #\$#\$#
>> #\$#\$#
>> #\$#
>> #
>>
>> I have tried very hard and this is the best i could come up with

>
> Your code is dreadful (and some of the other solutions proposed
> on this thread aren't great either). I hope you can see that if
> someone wants to, say, print a diamond of size 20, then your
> program will fail hopelessly.
>
> You will save time by thinking of a correct algorithm before you
> even sit down at your computer. For example:
> - for row number N, I need _____ spaces and _____ hashes
>
> A little bit of thought will reveal that the number of spaces
> needed is (N - 1) / 2, discarding any remainder, and the number
> of hashes is N (for the first half), and TOTAL + 1 - N (for the
> second half).

A good algorithm can be quite hard to think of.

5
#
#\$
#\$#
#\$
#

--
Neil Cerutti

Neil Cerutti, Nov 10, 2005
12. ### Old WolfGuest

Neil Cerutti wrote:

> Old Wolf <> wrote:
>>
>> A little bit of thought will reveal that the number of spaces
>> needed is (N - 1) / 2, discarding any remainder, and the number
>> of hashes is N (for the first half), and TOTAL + 1 - N (for the
>> second half).

>
> A good algorithm can be quite hard to think of.

This problem is also apt for recursion:

void draw_diamond(int n_spaces, int n_hashes, int odd)
{
draw_line(n_spaces, n_hashes);

if (n_spaces > 0)
draw_diamond(n_spaces - 1, n_hashes + 1, odd);

if (n_spaces > 0 || !odd)
draw_line(n_spaces, n_hashes);
}

//...
draw_diamond( (MAX-1)/2, 1, MAX % 2 );

Old Wolf, Nov 11, 2005