CS 102 Lab #3

Write a program that will read in some numbers from a file, and output the numbers to another file in a specific format.

The file to read from will be called "raw". The file that you will write to will be called "formatted".

Your program will first ask the user to type in the number of columns. It will then read from the file "raw" and output the numbers there into a formatted table or matrix in the file "formatted".

The numbers in "raw" will be integers from -999 to 999. The output in "formatted" should be lined up in neat rows and columns. If there are not enough numbers to finish the last row, fill as many places from the left as you can (like the bottom row of a calendar).

Although there are a number of ways of accomplishing this, you must use nested loops (i.e. a while within a while).

For example, if the file "raw" contained:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Your program should work as follows:

% a.out
Enter number of columns: 7
Done.
% more formatted
    1    2    3    4    5    6    7
    8    9   10   11   12   13   14
   15   16   17   18   19   20   21
   22   23   24   25   26   27
% a.out
Enter number of columns: 5
Done.
% more formatted
    1    2    3    4    5 
    6    7    8    9   10
   11   12   13   14   15
   16   17   18   19   20
   21   22   23   24   25
   26   27