A function to output double values in a given width
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_COUNT 100
void show(double array[], size_t array_size, unsigned int field_width);
int main()
{
double array[MAX_COUNT] = {0.0};
int count = 6;
char answer = 'n';
array[0] = 123456.123456;
array[1] = 12345.12345;
array[2] = 1234.1234;
array[3] = 123.123;
array[4] = 12.12;
array[5] = 1.1;
show(array, count, 12);
printf("\n\n\n");
show(array, count, 2);
printf("\n\n\n");
show(array, count, 6);
}
void show(double array[], size_t array_size, unsigned int field_width)
{
char format[10] = "%";
char width_str[4];
int i = 0;
int j = 1;
do
{
width_str[i++] = '0'+field_width%10;
}while((field_width /= 10) != 0);
do
{
format[j++] = width_str[--i];
}while(i>0);
format[j] = '\0';
strcat(format, "lf");
for(j = 0 ; j<array_size ; j++)
{
printf("\n");
printf(format, array[j]);
}
}
Related examples in the same category