# Thread: Program that prints numbers in columns

1. ## 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. 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. 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;
}```

5. 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' );
}
}```

6. >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.

7. Prelude made a mistake? Now what's this world coming to?

8. 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.

9. >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.

10. 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. 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.

12. //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

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. 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.