Thread: Lost on columns

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Lost on columns

    I have to put some output in columns, but I don't even know where to get started. I just wondered if anybody could tell me what I should read about or how I may I get started, that would be a really big help, thanks.
    Basically, its just numbers but instead of the output being something like:
    1
    2
    3
    4
    5
    and so on, it would look like:

    1 2 3 5

    6 7 8 9... and so on


    thanks,
    Extro

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Perhaps you could post the source code for the attempt you have made. Then we can help. If you need a hint to get started, think about how you might use some sort of a loop, and the % (modulus) operator to generate the output you are looking for.

    ~/

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    This is the code:

    Code:
    include <stdio.h>
    
    int main (){
        int	open=0, skip;
        int	cnt = 0, skipped=0;
           
        printf ("How many mail boxes does he skip?\n");
        scanf("%d" , &skip);
    
        while (cnt < 150){
            cnt++;
            if (cnt % skip == 0){
                open++;
                printf("%d = closed\n", cnt);
            }else{
                skipped++;
                printf("%d = open\n", cnt);
            }
        }
        printf("Opened a total of %d boxes\nSkipped a total of %d boxes\n", open, skipped);
    
        return 0;
    }
    But right now it outputs it:

    1 = open

    2 = open
    3= open
    4-closed

    etc... depnding on the input
    but I need it to display it like:

    1 = open 2 = open 3 = open 4 = open

    5 = closed 6 = closed 7 = open

    and so on to 150.

    Most of the instructions I have found so far on how to do this are hard to understand.
    Thanks,
    Extro

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    The \n in those printf() statements will cause newline character to be written. As you're doing it in both printf()s, you'll be starting a newline every time.
    Use the modulos operator to determine when to start a newline, and output a \n then only.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    68
    You mean something like this?

    Code:
    
    
    #include <stdio.h>
    
    int main (){
        int	open=0, skip;		
        int	cnt = 0, skipped=0;        
        printf ("How many mail boxes does he skip?\n");
        scanf("%d" , &skip);
    
        while (cnt < 150){
            cnt++;
            if (cnt % skip == 0){
                open++;
                printf("%5d\n = closed\n", cnt);
            }else{
                skipped++;
                printf("%5d\n = open\n", cnt);
            }
        }
            return 0;
    }

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    After compiling your (above) code, did you find it did what you wanted? What Hammer suggested will work for you - get rid of the newlines, and use the modulus operator to determine when you need to insert the newline - from what I understand, you want a newline after every fourth column. So you ought to be checking your code something like this:

    Code:
    some_var % 4
    edit::

    You are iterating through your loop, in this case, 150 times - every time you go through the loop, increment a counter, and every fourth time, insert a newline.

    ~/
    Last edited by kermit; 06-23-2005 at 06:58 PM.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    68
    So far, it isnt working, although I understand what your saying it doesnt seem to wanna work
    Mostly I'm having a hard time figuring out where the modulus should go
    Thanks,
    Extro
    Last edited by Extropian; 06-23-2005 at 07:08 PM.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Extropian
    So far, it isnt working, although I understand what your saying it doesnt seem to wanna work
    Mostly I'm having a hard time figuring out where the modulus should go
    Thanks,
    Extro
    Perhaps you could do a simpler program, which prints out the numbers 1 to 20 in four columns - if you can get that to work, you will likely see how to modify this program to work the way you want it to.

    ~/

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    68
    Im sorry but I wouldnt know how? where does the mod operator go to create 4 columns? I dont mean to bother but Im really stuck

  10. #10
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    Try something like this:

    Code:
    // Loop through all values in the array.
    for (i = 0; i < size; i++) {
    
        // Print the current value.
        printf ("%i", array[i]);
    
        // If the current index is the end of a row (of size width), print a newline.
        if (i % width == width - 1)
            printf ("\n");
    
        // Otherwise print a tab character to make spacing clean.
        else
            printf ("\t");
    }
    Last edited by Raptor007; 06-23-2005 at 09:29 PM. Reason: Added comments.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    68
    can somebody attempt this for me a little? I hate asking but I'm hoping if I see it maybe I'll understand because this is just not sinking into the my little brain lol
    Thanks.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, methods have already been suggested, but I'll try this.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
       size_t i, size = 4;
       for ( i = 0; i < sizeof array / sizeof *array; ++i )
       {
          printf("%3d", array[i]);
          if ( i % size == size - 1 )
          {
             putchar('\n');
          }
          else
          {
             putchar(' ');
          }
       }
       putchar('\n');
       return 0;
    }
    
    /* my output
      1   2   3   4
      5   6   7   8
      9  10  11  12
     13  14  15  16
     17  18 
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    68
    Thanks, I'm really kind of a see and learn guy. I had a hunch it was an array but couldn't get it to work.
    Thanks a bunch,
    Extro

  14. #14
    I typecast anyway.
    Join Date
    Jun 2005
    Posts
    25
    It doesn't have to be an array, I just figured you had an array of values.

    Here it is with just plain sequential integers:
    Code:
    // Loop through all values in the array.
    for (i = 0; i < max; i++) {
    
        // Print the current value.
        printf ("%i", i);
    
        // If the current index is the end of a row (of size width), print a newline.
        if (i % width == width - 1)
            printf ("\n");
    
        // Otherwise print a tab character to make spacing clean.
        else
            printf ("\t");
    }

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Extropian
    I had a hunch it was an array but couldn't get it to work.
    It need not be.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if (file)
       {
          int value;
          size_t i = 0, size = 4;
          while ( fscanf(file, "%d", &value) == 1 )
          {
             printf("%3d", value);
             if ( i % size == size - 1 )
             {
                putchar('\n');
             }
             else
             {
                putchar(' ');
             }
             ++i;
          }
          putchar('\n');
       }
       return 0;
    }
    
    /* file.txt
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
    */
    
    /* my output
      1   2   3   4
      5   6   7   8
      9  10  11  12
     13  14  15  16
     17  18 
    */
    Last edited by Dave_Sinkula; 06-23-2005 at 09:58 PM. Reason: Damn I'm getting slow.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  3. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  4. Number of columns in ListControl
    By MPSoutine in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2003, 02:29 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM