6 Replies - 1858 Views - Last Post: 17 October 2007 - 08:02 AM Rate Topic: -----

#1 shanebp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5393
  • View blog
  • Posts: 27,387
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 shanebp  Icon User is offline

  • New D.I.C Head

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

Re: A different asterisks problem question

Posted 17 October 2007 - 07:38 AM

View Postno2pencil, 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. :(

View PostLouisda16th, 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!
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5393
  • View blog
  • Posts: 27,387
  • Joined: 10-May 07

Re: A different asterisks problem question

Posted 17 October 2007 - 07:49 AM

View Postshanebp, on 17 Oct, 2007 - 07:37 AM, said:

View Postno2pencil, 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

Was This Post Helpful? 0
  • +
  • -

#6 Louisda16th  Icon User is offline

  • dream.in.assembly.code
  • member icon

Reputation: 15
  • View blog
  • 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 :).
Was This Post Helpful? 0
  • +
  • -

#7 shanebp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1