Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > code for two half diamond shapes

Reply
Thread Tools

code for two half diamond shapes

 
 
mitchellpal@gmail.com
Guest
Posts: n/a
 
      01-26-2006
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_stars +2;
printf("\n");

}



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

 
Reply With Quote
 
 
 
 
mitchellpal@gmail.com
Guest
Posts: n/a
 
      01-26-2006
anyone wanna post me the code...?
am done to my best

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-26-2006
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_stars +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.com, 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/>

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-26-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing the first half of a string to the second half joe chesak Ruby 7 09-23-2010 03:34 AM
ie7 displaying only half of page (cuts page in half)... trint ASP .Net 4 09-11-2007 10:56 AM
3d Shapes Jessica Weiner ASP .Net 3 02-24-2006 12:45 AM
regexp that matches half then conditionally excludes the other half Mike Ballard Perl Misc 6 11-15-2005 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57