A function to output double values in a given width : Double « Data Type « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » Data Type » DoubleScreenshots 
A function to output double values in a given width
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[0123456.123456;
  array[112345.12345;
  array[21234.1234;
  array[3123.123;
  array[412.12;
  array[51.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 = ; j<array_size ; j++)
  {
    printf("\n");
    printf(format, array[j]);
  }
}


           
       
Related examples in the same category
1.Table of reciprocals, squares, cubes, and fourth powersTable of reciprocals, squares, cubes, and fourth powers
2.Calculating the area of a room
3.Calculating volume price of alternative productsCalculating volume price of alternative products
4.Calculating average hourly pay rateCalculating average hourly pay rate
5.Convert double to int
6.Double calculation: square Double calculation: square
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.