Program that prints numbers in columns

This is a discussion on Program that prints numbers in columns within the C++ Programming forums, part of the General Programming Boards category; I need to write a program that prompts user for input of 2 integers (i,j) and the number of columns ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    Program that prints numbers in columns

    I need to write a program that prompts user for input of 2 integers (i,j) and the number of columns (c) the numbers will fill.

    the numbers need to print out i through j using c columns, consecutive numbers going down each column. As far as possible columns must be the same length with the left-most columns containing any "extra" numbers

    example output

    Numbers 11 through 23 in 5 columns:

    11 14 17 20 22
    12 15 18 21 23
    13 16 19


    Ok thats the problem. Now I need to use a one-dimensional array to do this. Where is a good place for me to start? This is the toughest part for me to visualize and begin writing the algorithm. Can someone help jumpstart me. Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You don't need an array.

    Simply start from the starting number:

    1) Output the number
    2) Increment the "column counter"
    3) if the "column counter" is equal to the number of columns specified go down a line ('\n')
    4) if number is not the ending number goto step 1

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Numbers 11 through 23 in 5 columns:

    11 14 17 20 22
    12 15 18 21 23
    13 16 19
    >You don't need an array.
    If the problem is printing the numbers in linear order as specified then an array is the easiest (and most general) method by far. If the items are a nice sequence of numbers as shown in the example then a beginner could probably figure it out in an afternoon:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int start, end, cols;
    
      cout<<"Starting value, ending value and columns: ";
      cin>> start >> end >> cols;
    
      const int count = end - start + 1;
      const int rows = ( ( count / cols ) + count ) / cols;
    
      cout.put ( '\n' );
    
      for ( int i = 0, first = start; i < rows; i++, first++ ) {
        for ( int j = first; j <= end; j += rows )
          cout<< j <<' ';
    
        cout.put ( '\n' );
      }
    }
    But the array based solution is immediately obvious.

    >Where is a good place for me to start?
    Instead of using a loop like this for filling the array:
    Code:
    for ( int i = 0; i < rows; i++ ) {
      for ( int j = 0; j < cols; j++ )
        a[i][j] = val;
    }
    Use something more like this:
    Code:
    for ( int i = 0; i < cols; i++ ) {
      for ( int j = 0; j < rows; j++ )
        a[j][i] = val;
    }
    Last edited by Prelude; 09-17-2004 at 09:28 PM.
    My best code is written with the delete key.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    sorry misread the example

  5. #5
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    en,there is a little problem with Prelude's way to get the number of rows
    Code:
    const int rows = ( ( count / cols ) + count ) / cols;
    it will got a larger number than necessary.

    i think it needs a change:
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
      int start, end, cols;
    
      cout<<"Starting value, ending value and columns: ";
      cin>> start >> end >> cols;
    
      const int count = end - start + 1;
      const int rows =  ( count / cols ) + ((count % cols)>0);
    
      cout.put ( '\n' );
    
      for ( int i = 0, first = start; i < rows; i++, first++ ) {
        for ( int j = first; j <= end; j += rows )
          printf("%5d",j);
    
        cout.put ( '\n' );
      }
    }

    blow me ... ...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >there is a little problem with Prelude's way to get the number of rows
    Thanks for noticing that. I was in a hurry and didn't use enough test cases.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Prelude made a mistake? Now what's this world coming to?

  8. #8
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Prelude made a mistake? Now what's this world coming to?
    hehe, it seems this world is coming to end,once Prelude made a mistake.

    blow me ... ...

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Prelude made a mistake? Now what's this world coming to?
    >hehe, it seems this world is coming to end,once Prelude made a mistake.

    Laziness breeds mistakes, so sue me.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Quote Originally Posted by Hermitsky
    en,there is a little problem with Prelude's way to get the number of rows
    Code:
    const int rows = ( ( count / cols ) + count ) / cols;
    it will got a larger number than necessary.

    i think it needs a change:
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
      int start, end, cols;
    
      cout<<"Starting value, ending value and columns: ";
      cin>> start >> end >> cols;
    
      const int count = end - start + 1;
      const int rows =  ( count / cols ) + ((count % cols)>0);
    
      cout.put ( '\n' );
    
      for ( int i = 0, first = start; i < rows; i++, first++ ) {
        for ( int j = first; j <= end; j += rows )
          printf("%5d",j);
    
        cout.put ( '\n' );
      }
    }
    ok great input guys, I really appreciate it, this board is so helpful. Can someone help walk me through this code especially the nested loop section and just help me understand what exactly the computer is doing?

    Also, what is the "printf" comand? I dont understand the "%5d"??? Thanks!!!

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by rayrayj52
    Also, what is the "printf" comand? I dont understand the "%5d"??? Thanks!!!
    It's just the C way of doing output. The %5d part is a format specifier telling printf how to format the variable j as it gets output and is equivalent to a cout.width(5) statement. Just replace this with a cout statement as in one of the prior examples.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    //determine the total number of numbers to output
    const int count = end - start + 1;

    //determine the number of rows to use
    //count/cols == number of full rows
    //count % cols means divide count by columns
    //and look at the remainder. If the remainder is
    //greater than zero then you will have a short
    //row, so add one extra

    const int rows = ( count / cols ) + ((count % cols)>0);

    //go to a new line
    cout.put ( '\n' );

    //initialze i to zero and first to start (don't
    //change start in the following code), and
    //increase i by one and first by one each time
    //through the outer loop, and initialize j to first
    //each time through second loop with increasing
    //j by number of rows each time through loop.

    for ( int i = 0, first = start; i < rows; i++, first++ ) {
    for ( int j = first; j <= end; j += rows )

    //print out number in columnar format using
    //either printf or cout format specifiers.
    ______________________________________
    example run:

    display numbers 1 to 5 in 3 columns.
    count = 5 - 1 + 1 = 5
    rows = 5/3 = 1 with remainder 2 so rows will be 2

    How the code works:
    think of 1 to 5 like this:
    1 2 3 4 5

    first time through outer loop:
    i starts at 0
    first starts a 1

    first time through inner loop:
    j starts at 1 and prints out 1
    j increments by 2 to 3 and prints out 3
    j increments by 2 to 5 and prints out 5
    j increments by 2 to 7 which is > 5 so inner loop ends

    back to outer loop, where i increments to 1 which is less than 2 so continue, and first increments to 2.

    back to inner loop where
    j starts at 2 and prints out 2
    j increments by 2 to 4 and prints out 4
    j increments by 2 to 6 which is > 5 so inner loop ends.

    back to outer loop,
    i increments by 1 to 2 which is not < 2 so outer loop ends:

    output:
    1 3 5
    2 4

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    let us not forget the setw() function... part of the <iomanip> library


    Here is a small exerpt on how to use this easy yet effective function for controlling column spacing

    Code:
    cout << "start" << setw(4) << 10 << setw(4) << 20 << setw(6) << 30;
    cout << setw(9) << 40 << setw(4) << 50 << setw(6) << 60;
    will produce an output that will look similar to this..
    Code:
    Start    10    20        30
             40    50        60

    OR


    another technique is to set the width of all your cout statements at once...

    Code:
    cout "Start now\n\n";
    cout.width(4);
    cout << 1 << 2 << 3 << endl;
    cout << 4 << 5 << 6 << endl;
    cout << 7 << 8 << 9 << endl;
    will produce an output similar to:
    Code:
    Start now       
    
    1         2         3
    4         5         6
    7         8         9
    nice.. formatted columns.. evertime. i think width is part of the<iostream> library.

    Two good techniques to allow all of your console output to look maaaavelous.



    Last edited by The Brain; 09-20-2004 at 03:10 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Need HELP with Program
    By Jeff in forum C Programming
    Replies: 25
    Last Post: 09-23-2004, 08:03 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21