Thread: spaces

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Wink spaces

    I have a problem for school , I could really use some help on, I needed to make a loop that would print a block of asteriks 10 x 10, then I need to break the block down into a triangle like so
    Code:
    int main(int argc, char *argv[])
    {
       
    
    	{   
    
    	int x;
    	int y;
    	for ( x=0; x<=9; x++) 
            {
    
    		 for (y=x-0;y <=9; ++y){
    		printf("*");                  }
    
    
    	     printf("\n");
    	}
    	}
        return 0;
    }
    my problem is the next step I need a loop that will print
    Code:
          **********
            *********
              ********
                *******
                  ******
                    *****
                      ****
                        ***
                          **
                           *
    I realized the only way is to add spaces, problem is I dont know how to add spaces to a loop yet, they should line up and be squared, but I cant get them to stay lined up when I post this.
    thx for any help,
    btw, notice i used the code box this time lol

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    for(i=0, j=0;i<10;i++) {
      for(; j<i;j++)  //print the leading spaces for each row
        printf(" ");
      
      //now add your code to print out the number of stars, here
    
      //now print your \n char
      //and loop back for the next row
    }
    I noticed the code tags - will wonders never cease??

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I realized the only way is to add spaces, problem is I dont know how to add spaces to a loop yet
    The exact same way that you're adding asterisks: printf(" "); You just need to write a quick loop that on each iterations, writes the correct number of spaces and the the correct number of asterisks - that should be simple linear math.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    thx again guys!
    sometimes I think I make this a lot harder than it really is

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII File Copy Problem: Expand tabs to multiple spaces
    By matrixx333 in forum C Programming
    Replies: 5
    Last Post: 10-21-2009, 03:13 AM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  3. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  4. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM