473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code for two half diamond shapes

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 shown....i.e
two half diamonds...e.g if no. is 3 output;

* *
* * *
#include <stdio.h>

void star(int num);
void space(int num);
main()
{
int num;
do
{
printf("Please enter an odd number : ");
scanf("%d",&num );
}while(num % 2 ==0);

star(num);
}

void star(int num)
{
int count_stars=0;
int num_stars= 1;
int count_spaces= 1;
// loop for the upper part of the left diamond
while (num_stars <= num)

{
count_spaces =(num-num_stars)/2;

// loop for printing spaces input value
while (count_spaces > 0)
{
printf(" ");
count_spaces = count_spaces - 1;

}

//initialising number of stars
count_stars = num_stars;
//this is for printing stars
while (count_stars > 0)
{
printf("*");
//loop for printing spaces inside value which is 2 less the input
number
while (count_stars > 2)
{
printf(" ");
count_stars = count_stars -1;
}
count_stars = count_stars -1;
}
//void star (int num);

num_stars=num_s tars +2;
printf("\n");

}

}
and you say c ain't challenging! my email address is real.

Jan 26 '06 #1
3 5763
anyone wanna post me the code...?
am done to my best

Jan 26 '06 #2
mi*********@gma il.com wrote:
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 shown....i.e
two half diamonds...e.g if no. is 3 output;

* *
* * *
Let's start with the above. It should have read:

Hi guys,

Please help me with this problem. I have put halfhearted effort into
this, and now it's outputing only half of what I need. The user is
supposed to input an odd number between 0 and 20, and the program then
displays the shape as shown below, i.e. two half diamonds. E.g. if user
inputs 3:

* *
* * *

Also, an example with 5 would have helped more...
#include <stdio.h>

void star(int num);
void space(int num);
main()
{
int num;
do
{
printf("Please enter an odd number : ");
scanf("%d",&num );
}while(num % 2 ==0);
You should think about negative numbers as well. What sort of shape
would you print for a negative number?
star(num);
}
void star(int num)
{
int count_stars=0;
int num_stars= 1;
int count_spaces= 1;

// loop for the upper part of the left diamond
while (num_stars <= num)
{
count_spaces =(num-num_stars)/2;
// loop for printing spaces input value
while (count_spaces > 0)
{
printf(" ");
count_spaces = count_spaces - 1;
}
//initialising number of stars
count_stars = num_stars;
//this is for printing stars
while (count_stars > 0)
{
printf("*");
//loop for printing spaces inside value which is 2 less the input
number
while (count_stars > 2)
{
printf(" ");
count_stars = count_stars -1;
}
count_stars = count_stars -1;
}
//void star (int num);

num_stars=num_s tars +2;
printf("\n");
}
}
The above code actually compiles (for me, only when I replace C++ style
comments with C style, but that's just my compiler not being
C90-compliant, I guess), and it does output only the first half diamond
shape. You should have stated that more precisely, as "halfway" may
have been: half way vertically, as well as horizontally, not to mention
"up to line XXX of the program listing". ;-)

Your spacing is horrible (or your posting SW made it so), but I'd say
you didn't even _try_ doing the other half-diamond. I actually gave up
understanding your code, but was sufficiently intrigued to write my own
(a /very/ slow day at work).

I'm going to post my star() function below, and I encourage you to
figure out how it works. It's not commented, but I'm not doing
/everything/ for you! (To pedants: yes, I could have formatted it
better.) NB, the caller is supposed to ensure num >= 0!

void star(int num)
{
int lead = num / 2;
int hill = -1;
int valley = num - 2;
int i;

while (lead >= 0)
{
for (i = 0; i < lead; ++i) printf(" ");

printf("*");

for (i = 0; i < hill; ++i) printf(" ");

if (hill > 0) printf("*");

for (i = 0; i < valley; ++i) printf(" ");

if (valley > 0) printf("*");

for (i = 0; i < hill; ++i) printf(" ");

if (hill > 0) printf("*");

printf("\n");

--lead;
hill += 2;
valley -= 2;
}
}

I'm sure there's other/better ways of doing this, and you're free to
give it a shot.
and you say c ain't challenging! my email address is real.


This actually wasn't C at all, but a general problem solving exercise.
There's nothing C-specific here.

Cheers

Vladimir

PS
Regarding your second post:

a) Quote what you're replying to -- even when replying to yourself,
otherwise people won't know what you're on about. I struggled, even I
actually saw your previous post.
b) The tone in it moves you dangerously close to trolling. Noone here
has the duty to reply. The more insistent you are the less likely it is
that you'll get a reply (at least a polite one).

PPS
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>

Jan 26 '06 #3
mi*********@gma il.com wrote:
anyone wanna post me the code...?
am done to my best


Firstly, please provide context even when replying to yourself, it is
entirely possible that people have not yet seen the message you are
replying to. See http://cfaj.freeshell.org/google/for information on how
to do this.

Secondly, this is *not* a chat room. Allow at least a day or two before
assuming people are not going to answer. In this case I'm not answering
your original question because I've gone past it to see what extra
information you've provided, only to see this stupid post and as a
result decide I can't be bothered to help you this time.

Oh, and please don't use slang like "wanna", make an attempt to make it
easy for the people who might help you by writing in English. English
errors due to it not being your first language are fine, but shear
laziness isn't.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Jan 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2467
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
2
2409
by: emma middlebrook | last post by:
Hi I come from a C++ background and so am familiar with the 'dreaded inheritance diamond' i.e. the ambiguity of which data to use when a class appears twice in the hierarchy. Here's a link for anyone not familiar: http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 .. Base / \
4
2050
by: Schüle Daniel | last post by:
Hello C++ NG, I encountered a strange behaviour in this code #include <iostream> #include <cstdlib> #include <string> using std::cout; using std::endl;
0
1320
by: alwayssmiling | last post by:
Hi, Im drawing some shapes on the image surface (i.e when im displaying the image ) with the help of graphics. Since i want to move thsese shapes from one place to other place in the image surface. I want to erase the previous shapes at the previous positions. But how can i erase shapes in the image any ideas. Thank u.
1
2048
by: GeckoPunk | last post by:
"Diamond of Asterisks" -------------------------------------------------------------------------------- Sorry to all the experienced users who will think I'm lame for asking this question, but I'm having a hard time trying to figure out the VB.NET code to create a "Diamond of stars" that looks like this: * *** ***** *******
12
2408
by: StephQ | last post by:
I face the following problem. I wrote a poor's man plotting function: computePlot. This function append some x-values belonging to step and the correseponding f( x ) (for a given const member function f that takes and return a double) values to a reference stringstream, let's call this ss. Then I usually use ss.str() to transfer the...
9
10958
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
6
5713
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 class structure: class entity { public:
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.