I believe I might be over-complicating this and searched many solutions on here.
This is C code to write an input stream in command line of double numbers like "10 1 666 10000 666 77 88 3 5 9" with default 13 decimal positions (prec) and default 3 columns (cols), so that output would actually be
10.0000000000000 1.0000000000000 666.0000000000000
10000.0000000000000 666.0000000000000 77.0000000000000
88.0000000000000 3.0000000000000 5.0000000000000
9.0000000000000
in 3 columns. (All numbers input must be 10,000 or less).
I basically need my output data to look like this (2 spaces max after each column):
5163.1575670644243 6806.8180791650138 8977.2739646595655
2598.0792870876185 7162.5237586596268 6691.2041993469038
1043.6422009949033 6922.8216193121125 3.0480056154058
9926.6081118198181 100.3082369457076 5135.1567125461588
7808.2382885219886 1439.6542863246559 249.6179692983795
214.0065309610279 9280.5883968626968 2687.3871883297220
7612.8426770836513 6644.2027649769589 8364.5604419080173
4740.7550279244360 254.6181218909269 2500.3814813684498
2293.6803491317482 835.3306680501725 5962.7923215430155
9622.5988341929387 57.3069246498001 1557.9630726035341
8398.5614795373385 5958.4870143742182 2568.3835566270945
9935.9135715811644 3410.1040681173131 982.0299691763055
8393.5613269447913 9066.2766808069100 4896.4546037171549
7597.8422193060087 8551.5661488692895 1076.6432081057162
1911.3635059663686 7586.8418836024048 9282.8936429944770
4696.1433149204995 1388.0423596911528 1936.3642689291055
3408.4091921750542 3556.4137089144565 9241.8923917355878
5003.4578691976685 3366.7130954924160 4270.1303140354621
620.6292916653950 4700.7538071840572 1766.0538956877347
441.6238288521989 8153.8591875972779
Here is my code. You can simply compile the .c file via gcc and run the .exe and input a #'s with whitespace in between to have it output (formatted):
#include <stdio.h> // scanf(), fscanf(), fprintf(), fopen()
#include <stdlib.h> // atoi(), getenv()
int main(int argc, char *argv[])
{
int cols = 3; // default value for columns is 3.
int prec = 13; // default value for precision is 13 decimal places.
int count = 0; // keep track of length of input stream, i.e. "10 55 13 666 77" has length 5.
double x;
// Get a command line argument (if it exists) to set precision. If it's not there, do nothing and just use default precision (13)
if (argc > 1)
{ // get an operand from the command line
prec = atoi(argv[1]);
cols = atoi(argv[2]);
}
// User gets prompted to enter input
printf("Enter input stream numbers: \n");
// While loop spits output and formats it
while (scanf("%lf", &x) != EOF)
{
// Asterisk(*) keeps precision for dynamic. Default value for precision is 13.
printf("%19.*f", prec, x);
count++;
// When count == cols, \n is output to format columns
if (count == cols)
{
printf("\n");
count = 0;
}
}
return 0;
}
I currently have the column width fixed at 19, but something tells me it needs to be an asterisk *, so it can be dynamic. As soon as I have different sized #'s as input, they do not stay 2 spaces after each column consistently.
I thought I should re-assign my double x; which is my input to maybe a char x[] array, but I thought if I asked there might be an easier implementation. I'm having a hunch it has something to do with the leading spaces of each column #.
Since I'm using scanf(), is there a way to count each character of my #'s? like if my first number is 10.0000000000000, is there a way to count each position, so I can total up (in this case) an int value = 15, that way I can pad each number dynamically?
Please let me know if I need to be more specific.
