By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

DIAMOND SHAPE

P: n/a
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?

Nov 10 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

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 '05 #2

P: n/a

in*****@gmail.com 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.

Tip: Math is your friend ;)

Cheers,
Andre

Nov 10 '05 #3

P: n/a
#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
for 7 or greater than 7 values. Please Help!

Nov 10 '05 #4

P: n/a
On 2005-11-10, coinjo <co****@gmail.com> 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?

How tall is your diamond?
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
your loop indexes.

Plus, it must do something sensible with height zero:

How tall is your diamond?
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.

How tall is your diamond?
5
#
###
#####

How tall is your diamond?
1
#

How tall is your diamond?
2
#

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

How tall is your diamond?
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
Nov 10 '05 #5

P: n/a
#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?

Nov 10 '05 #6

P: n/a
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/ ---]
Nov 10 '05 #7

P: n/a
On 10 Nov 2005 11:46:41 -0800, "coinjo" <co****@gmail.com> 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]

Didn't your teacher tell you anything about indenting?

--
Bob Hairgrove
No**********@Home.com
Nov 10 '05 #8

P: n/a
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).

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?

Nov 10 '05 #9

P: n/a

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 '05 #10

P: n/a
On 2005-11-10, Old Wolf <ol*****@inspire.net.nz> 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. ;)

How high is your diamond?
5
#
#$
#$#
#$
#

--
Neil Cerutti
Nov 10 '05 #11

P: n/a
Neil Cerutti wrote:
Old Wolf <ol*****@inspire.net.nz> 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. ;)


Ouch, bad brain fade. Let's go for (MAX+1)/2 - num_hashes.

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 );

Nov 11 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.