# A different asterisks problem question

Page 1 of 1

## 6 Replies - 2007 Views - Last Post: 17 October 2007 - 08:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=35545&amp;s=4a13aa6afeb1ea5f858d707a639f4b5b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 shanebp

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 10-October 07

# A different asterisks problem question

Posted 17 October 2007 - 07:23 AM

Hey guys, I am new to C programming and I am tackling the ( seems like ) common asterisks problem.

There are four figures which you have to make the program print out. I have successfully completed the first two. The last two require spaces to be entered to make the shape. The shape is:

**********
*********
********
*******
******
*****
****
***
**
*

Its basically that.. just as you can imagine, its 90 degrees straight up on the RIGHT side of it ( couldn't get the real shape to come out on this haha ). The other one is that except shape (with 90 degrees on the right side ) flipped upside down.

My code for the first shape is ( code for the second shape is almost the same ):

Quote

#include <stdio.h>

int main (void)

{
int x, y;

for ( x=1; x<11; x++ )

{

for ( y=1; y<=x; y++ )

{

printf( "*" );

}

printf( "\n" );

}

return 0;

}

My problem is, I don't know how to get the spaces in there to make the shape. I've tried almost everything I know how to do. Any help is appretiated!

This post has been edited by shanebp: 17 October 2007 - 07:24 AM

Is This A Good Question/Topic? 0

## Replies To: A different asterisks problem question

### #2 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: A different asterisks problem question

Posted 17 October 2007 - 07:32 AM

Quote

Its basically that.. just as you can imagine, its 90 degrees straight up on the RIGHT side of it

You mean the mirror image of what is being shown?

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 5963
• Posts: 29,137
• Joined: 10-May 07

## Re: A different asterisks problem question

Posted 17 October 2007 - 07:33 AM

What you would want to do is set your total width. Check how far from the right to the left you'll be printing, & then subtract that number from the total width.

So, say your width will be 80, & you're printing the 1st line with only one asterisks, you'd take 80, minus 1, print 79 spaces (I would suggest within a for loop), & then print your one asterisks.
Next line, 80, minus 2, print 78 spaces, then your two asterisks...

I hope this helps.

### #4 shanebp

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 10-October 07

## Re: A different asterisks problem question

Posted 17 October 2007 - 07:38 AM

no2pencil, on 17 Oct, 2007 - 07:33 AM, said:

What you would want to do is set your total width. Check how far from the right to the left you'll be printing, & then subtract that number from the total width.

So, say your width will be 80, & you're printing the 1st line with only one asterisks, you'd take 80, minus 1, print 79 spaces (I would suggest within a for loop), & then print your one asterisks.
Next line, 80, minus 2, print 78 spaces, then your two asterisks...

I hope this helps.

Hey thanks, it does sort of. The only problem is, im at an EXTREMELY basic stage of programming. We just started on the for loops. Pretty much we know how to printf, scanf, while, else, and for. I have no I dea how I would put dimentions in there.

Louisda16th, on 17 Oct, 2007 - 07:32 AM, said:

Quote

Its basically that.. just as you can imagine, its 90 degrees straight up on the RIGHT side of it

You mean the mirror image of what is being shown?

Yes exactly, sorry if i'm confusing anybody!

### #5 no2pencil

• Professor Snuggly Pants

Reputation: 5963
• Posts: 29,137
• Joined: 10-May 07

## Re: A different asterisks problem question

Posted 17 October 2007 - 07:49 AM

shanebp, on 17 Oct, 2007 - 07:37 AM, said:

no2pencil, on 17 Oct, 2007 - 07:33 AM, said:

What you would want to do is set your total width. Check how far from the right to the left you'll be printing, & then subtract that number from the total width.

So, say your width will be 80, & you're printing the 1st line with only one asterisks, you'd take 80, minus 1, print 79 spaces (I would suggest within a for loop), & then print your one asterisks.
Next line, 80, minus 2, print 78 spaces, then your two asterisks...

I hope this helps.

Hey thanks, it does sort of. The only problem is, im at an EXTREMELY basic stage of programming. We just started on the for loops. Pretty much we know how to printf, scanf, while, else, and for. I have no I dea how I would put dimentions in there.

It wouldn't really be dimensions. Think of it like this, in the code you printed, you have no bounds, you just start printing in your loop. Simply print spaces for max minus the current loop number prior to that before printing.

```#include <stdio.h>

int main(void) {
int max=80;
int x, y, z;

for(x=1;x<11;x++) {
for(z=0;z<max-x;z++) { // determin the max (width) minus the current loop to get your spaces needed
printf(" ");
}
for(y=1;y<=x;y++) {
printf("*");
}
printf("\n");
}
return 0;
}

```

This post has been edited by no2pencil: 17 October 2007 - 07:51 AM

### #6 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: A different asterisks problem question

Posted 17 October 2007 - 07:52 AM

Let's work out on the algorithm.
The number of * keeps decreasing linearly. Also during this time the number of spaces increases. Lets look at a sequence here:
Row 1: 11 *, 0 spaces, x = 1
Row 2: 10 *, 1 space, x = 2
Row 3: 9 *, 2 spaces, x =3
Row 4: 8 *, 3 spaces, x =4
and so on.....

Notice a relation between x and number of spaces. Number of spaces is always one less than x (your current row number). So the logic is simple: In the second loop, you need to cycle from 1 to 11 again. Print a space only when y<x. Otherwise print a *. You'll need an if-else construct. Try writing the code now.
Hope this helps .

### #7 shanebp

• New D.I.C Head

Reputation: 0
• Posts: 6
• Joined: 10-October 07

## Re: A different asterisks problem question

Posted 17 October 2007 - 08:02 AM

Hey guys, both your comments helped alot! I will remember this for future reference! After some hard searching ( 2 hours ) I also found the key to the problem online! Heres what the page had in case anybody has the same problem.

Quote

#include <stdio.h>

int main (void)

{
int x, y, z;

for ( y=10; y>=1; y-- )

{

for ( x=1; x<=10 - y; x++ )

printf( " " );

for ( z=1; z<=y; z++ )

printf( "*" );

printf( "\n" );
}

printf( "\n" );

return 0;

}

x meaning spaces
y meaning rows
and z meaning columns

But I have to get going to another class right now! I'll check back later on, THANKS A TON! for the help guys!!!

-Shane B.P.