C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 14th, 2003, 11:08 PM
flag108 flag108 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 flag108 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question leading spaces in a printf

I am new to C programming. I have written a program that prints a pattern using astericks. It begins with a single '*' and increases each line, up to 10. However, I cannot figure out how to print with leading spaces. For example, I need to print the first row as 10 astericks, no spaces. Then, the next row should begin with 1 space and then print 9 astericks. Then, 2 spaces and then print 8 astericks, etc.

#include <stdio.h>

/* function main begins program execution */
int main ()
{
for( int cnt = 10; cnt >= 1; cnt--)
{
for( int k = 1; k <= cnt; k++)
printf( "*" );

printf( "\n" );

}

printf( "\n" );
for( int cntr = 1; cntr <= 10; cntr++)
{
for( int l = 1; l <= cntr; l++)
printf( "*" );

printf( "\n" );

}


return 0; /*indicate program ended successfully*/


} /*end function main*/


Please help!

Reply With Quote
  #2  
Old December 15th, 2003, 01:47 AM
peenie's Avatar
peenie peenie is offline
Google Relay Server
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Location: Oh christ I don't even know any more.
Posts: 1,812 peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)peenie User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)  Folding Points: 9953 Folding Title: Novice Folder
Time spent in forums: 2 Weeks 1 Day 19 h 12 m 24 sec
Reputation Power: 437
Send a message via AIM to peenie Send a message via MSN to peenie
You could print a space...
Code:
printf("space between words\n");
printf("  space before words\n");

Reply With Quote
  #3  
Old December 15th, 2003, 06:19 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Use code tags when posting code.

The number of asterisks is a function of the number of spaces:

asterisks = 10 - spaces ;

For spaces = 0 to 9, Loop once to print spaces, loop again to print asterisks, print newline, next.


PHP Code:
#include <stdio.h>

int main()
{
    
int i ;
    
int asterisks ;
    
int spaces ;

    for( 
spaces 0spaces 10spaces++ )
    {
        for( 
0spacesi++ )
        {
            
printf" " ) ;
        }

        
asterisks 10 spaces ;
        for( 
0asterisksi++ )
        {
            
printf"*" ) ;
        }

        
printf"\n" ) ;
    }




Clifford

Last edited by clifford : December 15th, 2003 at 07:00 AM.

Reply With Quote
  #4  
Old December 23rd, 2003, 10:51 PM
flag108 flag108 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 flag108 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry for the delay. Thank you very much for your response. Happy holidays!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > leading spaces in a printf

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap