473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DIAMOND SHAPE

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
11 12569

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

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
#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
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
#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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
10
by: coinjo | last post by:
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. Your program should display...
3
by: mitchellpal | last post by:
guys.... help me out here... my code is running halfway... how do i complete the other right half....... pp.. the user should input an odd number btw 0 and 20 then the program displays th shape as...
14
by: jasson118 | last post by:
i am a newber to C++ and have trouble with one of the problem from the book. can anyone able to use nested loops to display a diamond shape with " * ". * *** ***** ******* ********* *******
3
by: TWX | last post by:
Hi, everyone. I was hoping if anyone could help me with writing a general C++ program to receive one word and output to a text file such that the letters of the word are arranged to form a diamond...
0
by: Beano | last post by:
I am learning to program and I have a need for help to a problem I am sure most professional programmers can answer. I want to print a box of astrisks with a blanked out diamond shape in the center....
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
1
by: striker07 | last post by:
Someone help me how can i display a diamond shaped asterisk correctly pls. correct my program this is my code: Dim ctr, space, asterisk As Integer For ctr = 1 To 9 Step 2 For space = (9...
6
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.