Velocity Reviews > C++ > DIAMOND SHAPE

# DIAMOND SHAPE

coinjo
Guest
Posts: n/a

 11-10-2005
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?

int2str@gmail.com
Guest
Posts: n/a

 11-10-2005

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

int2str@gmail.com
Guest
Posts: n/a

 11-10-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

coinjo
Guest
Posts: n/a

 11-10-2005
#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

Neil Cerutti
Guest
Posts: n/a

 11-10-2005
On 2005-11-10, coinjo <(E-Mail Removed)> 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

coinjo
Guest
Posts: n/a

 11-10-2005
#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?

Arne Schmitz
Guest
Posts: n/a

 11-10-2005
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/ ---]

Bob Hairgrove
Guest
Posts: n/a

 11-10-2005
On 10 Nov 2005 11:46:41 -0800, "coinjo" <(E-Mail Removed)> 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
(E-Mail Removed)

Old Wolf
Guest
Posts: n/a

 11-10-2005
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?

int2str@gmail.com
Guest
Posts: n/a

 11-10-2005

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