Thread: Aligning/justification problem

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Aligning/justification problem

    Hi, I'm a beginner programmer, I pretty much did the exercise I was supposed to do, except I don't know how to justify one set of numbers specifically

    This is the end result:

    k sum
    --------
    1 1
    2 3
    3 6
    4 10
    5 15
    6 21
    7 28
    8 36
    9 45
    10 55
    11 66

    as you can see as it continues like this, due to 10 and the other 2 digit numbers, the numbers in the sum get shifted, i want them to stay in a justified order, how do I do that?

    here is the code:

    Code:
    //SUMK - prints the sum of the numbers from 1 to k, for all values of k between 1 and 20.
    
    #include <stdio.h>
    
    int main (int argc, char **argv) {
      
      int sum = 0;
      int k = 1;
      
      printf ("k      sum\n");
      printf ("----------\n");
      
      while (k < 21) {
        
        sum = sum + k;
        printf ("&#37;d", k);
        printf ("%9d", sum);
        printf ("\n");
      k++;
      }  
      return(0);
    }
    These posts start automatically justifying when I don't want it to!!! Here is what the output looks like:

    http://img365.imageshack.us/my.php?image=justjk5.jpg
    Last edited by JFonseka; 08-06-2007 at 03:35 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use &#37;9d or some other number for the first printf() as well.

    If you want the numbers to be left-aligned, use %-9d.

    Also, %09d fills the extra areas with zeros instead of spaces.

    [edit] So how do you like ConTEXT? . . . [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you've already used 9d for one set of formatting, explore other numbers for the other printf

    BTW
    printf( "&#37;d%9d\n", k, sum );
    would also work.


    Hint: try "%-4d"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by dwks View Post
    Use %9d or some other number for the first printf() as well.

    If you want the numbers to be left-aligned, use %-9d.

    Also, %09d fills the extra areas with zeros instead of spaces.

    [edit] So how do you like ConTEXT? . . . [/edit]
    Indeed it does, never noticed that, should have thought to try it, thanks! Context is fine...lol, why?

    Quote Originally Posted by Salem View Post
    Well you've already used 9d for one set of formatting, explore other numbers for the other printf

    BTW
    printf( "%d%9d\n", k, sum );
    would also work.


    Hint: try "%-4d"
    -4d works good with 6d on the other one, thanks!!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Context is fine...lol, why?
    I see you're using Vista, and I'm trying to figure out which of my editors (ConTEXT is one of them) will work with Vista . . . anyway, it's not that important. I'll find out soon enough.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Yea context v 0.98.3. I compile using cygwin though

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    In fact, i tried a bunch of other editors that could compile as well with vista and most failed or created errors in the output which i could not understand, so I decided to use context for editing and cygwin for compiling and execution and it works really well, it automatically justifies the code, so I like it.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I see. I installed Dev-C++ on Vista but it didn't work. (The GCC port, that is.) GCC only worked when run from bin/. I tried meddling with its library paths, include paths, you name it, but still no luck.

    Anyway, I "fixed" it with a program which cds to the right directory and calls GCC from there. I intend to install Cygwin on the computer sooner or later, so we'll see how that works.

    [edit]
    In fact, i tried a bunch of other editors that could compile as well with vista and most failed or created errors in the output which i could not understand, so I decided to use context for editing and cygwin for compiling and execution and it works really well, it automatically justifies the code, so I like it.
    Yes, exactly, so you know what I mean. I can send you the program I used to patch Dev-C++ if you like. It's not on this computer though.

    I'll have to search around to see if anyone else has encountered/fixed this problem. [/edit]

    [edit=2] I don't think it's a simple path problem, though it could be. I didn't debug it very extensively. [/edit]
    Last edited by dwks; 08-06-2007 at 04:13 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by dwks View Post
    I see. I installed Dev-C++ on Vista but it didn't work. (The GCC port, that is.) GCC only worked when run from bin/. I tried meddling with its library paths, include paths, you name it, but still no luck.

    Anyway, I "fixed" it with a program which cds to the right directory and calls GCC from there. I intend to install Cygwin on the computer sooner or later, so we'll see how that works.

    [edit]
    Yes, exactly, so you know what I mean. I can send you the program I used to patch Dev-C++ if you like. It's not on this computer though.

    I'll have to search around to see if anyone else has encountered/fixed this problem. [/edit]

    [edit=2] I don't think it's a simple path problem, though it could be. I didn't debug it very extensively. [/edit]

    Lol I did not understand much of those parameters you can put into the compiler so I left it alone. I guess they will release a vista compatible version later, vista is probably just recognizing it's gcc calling command as a potential security threat and ending it early, I'm just speculating, I'm a complete noob and probably wrong.
    But yea, Vista isn't the most flexible when it comes to development for home users at the moment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM