to print a single column as multiple columns

hi,

Here is my problem. How would you change this 20 lines long column output into 4 parallel columns? With cout, no arrays:

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

into

1     6     11     16
2     7     12     17
3     8     13     18
4     9     14     19
5    10     15     20
Last edited on
If you can't use arrays, you'll have to mathematically find the values to print.
You have to print the text line by line. Thus it is 1, 6, 11, 16, 2, 7, 12, etc.
Each line is a simple arithmetic progression.
1
2
3
4
5
for(int line = 1; i <= 5; i++){
   for(int number = line; number <= 20; number += 5)
      std::cout << number << '\t';
   std::cout << '\n';
}

You may want to use setw and etc. form iomanip for formatting instead of a tab.
I remember these puzzles that they used to give us in school. Damn it, I actually think I miss them.

First of all I would need to know how I'm getting the data. If it's read from a file then it could be done pretty easily: Read -> Display -> Tab -> Skip_Next_Five ->Repeat... -> Jump_To_Top -> Skip_Num_Iterations -> Read -> Display...

If the user is entering them then it gets obnoxious because you need some way to store them.

Does dynamically allocated memory count as an array in this case? It technically is, but I thought it was worth asking.
Last edited on
@Computergeek01, I'm pretty sure the values are constants.

@OP, If they are not constants, you should probably look into ncurses..
cheers guys,

hamsterman was the closest. A neat solution :)

** data is entered by the user in the form of 'for loops'.
** the values are constants
** I wouldn't want to use dynamic memory allocation

I used your suggestion to break a long column of temperature scale into smaller ones for a clearer display.

Thank you!
Mat
Last edited on
Topic archived. No new replies allowed.