Thread: Formatting printf

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Formatting printf

    ok say i have an array of numbers like this:

    Code:
    double num[] = {15.5, 17.2, 17.4, 15.2, 15.3, 17.8, 17.5}
    and i want to print them out on the screen like this:

    Code:
    for(i = 1; i<n; ++i) printf("%6.1lf\n", num[i]);
    how do i format the output so that i can print them out neatly 3 numbers per line, then another line underneath??

    For Eg;

    Code:
    15.5  17.2  17.4

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Remove the \n from your printf.
    Then add an if check that checks to see if the counter is a multiple of 3, and if so, put a newline before you print the next line.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Formatting printf

    First, fix the for loop:
    Code:
    for(i = 0; i<n; ++i)
    Then do what quzah said, since he beat me. Use the modulus operator % within the for loop
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 09:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM