Thread: Outputting in a Chart Format

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Outputting in a Chart Format

    I was wondering if it was possible to have an output in the form of a chart?

    Such as;

    N T F
    1 2 5
    4 6 3
    2 5 9

    If so how is this done?

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Yes it is possible. Here's the sarcastic answer...
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      printf("N\tT\tF\n1\t2\t5\n4\t6\t3\n2\t5\t9\n");
      return 0;
    }
    But the real answer isn't that much more difficult.
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    How would you use a variable in this?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      char *chart = "N\tT\tF\n1\t2\t5\n4\t6\t3\n2\t5\t9";
    
      puts(chart);
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Say I want to output the equation x+6 into a chart format showing both the x values and the sum.

    IE;
    X Sum
    0 6
    1 7
    2 8
    etc;

    Is it possible to do with an array?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's possible, but you're probably better off building the chart on the fly.
    If you understand what you're doing, you're not learning anything.

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    How would you use a variable in this?
    Like this

    Code:
    #include <stdio.h>
    
    int main () {
      
      int x;
      int number = 6;
      int sum;
      
      printf("X\tSum\n");
    
      for( x=0; x<20; x++){
        sum=x+number;
        printf("%i\t%i\n",x,sum);    
        }  
    
      return 0;
      }
    And yes you can use arrays but i dont see the need in this example.
    Last edited by GanglyLamb; 10-14-2004 at 10:26 AM.

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    For more versatility (and in my opinion readability), you can place field widths in your printf statement.
    Code:
    printf ("%3d%3d\n", x ,  x + 6);
    That would (for this example) print both x and the sum of x + 6, each with a width of 3 chararacters. So given a simple loop with this statement where x increments through 1-3 would output...
    Code:
       1   7
       2   8
       3   9
    Additionally you can fill the whitespace (unused field space) with leading zeros if you wish (defaults to empty spaces). To fill them with 0's it would be,
    Code:
    printf ("%03d%03dn", x ,  x + 6);
    Another simple loop would output...
    Code:
    001007
    002008
    003009
    If you precede the field width with a - (minus) sign, then the output will be left justified vs the default right justification.
    Code:
    printf ("%-3d%-3d\n", x ,  x + 6);
    Resulting in...
    Code:
    1   7
    2   8
    3   9
    Last edited by Scribbler; 10-14-2004 at 02:47 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Congradulations. You've managed to get your work done for you without doing anything on your own. You're now officially a leech.

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

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    For more versatility (and in my opinion readability), you can place field widths in your printf statement.
    Code:
    printf ("%3d%3d\n", x ,  x + 6);
    But why limit yourself to predefined widths when you can have fun with:
    Code:
    printf("%*d%*d\n", 3, x, 3, x +6);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Outputting in SVG format
    By Basca in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 02:54 PM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  5. outputting a ASCII chart
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2002, 11:55 AM