Scientific Format:

Initial Comments
/* Formatting  and    <iomanip.h>
                   file: 4ex2.cpp  (or used as 4ex2.appended)
   FALL 1998
   ___________________________________
   Jacob Y. Kazakia   jyk0
   September 28, 1998
   Programming example  2 of week 4
   Recitation Instructor: J.Y.Kazakia
   Recitation Section  01
   ___________________________________
 
Purpose: This program reads three numbers a, b, and c through the keyboard and  outputs them on the screen in various SCIENTIFIC formats.
 
Algorithm: The functions: setiosflags, setw, setprecision are
           used to format output.
 
              ios:: fixed
              ios:: scientific
              ios:: showpoint
*/
#include <iostream.h>
#include <iomanip.h>
 
void main()
{
//  variables are typed and defined
 
float a,b,c;     // the three numbers
 
cout<<" \n\n   Please enter 3 numbers \n  a = ";
cin>>a;
cout<<"\n  b = ";
cin>>b;
cout<<"\n  c = ";
cin>>c;
Since the function setiosflags is sent to cout,
Data directed to the default output  will be in scientific  form
 // Output everything in scientific form
 
cout<<setiosflags( ios::scientific );
 
//***********************************************************
Precision is set to 5 digits after the decimal point.
 
A string of numbers is sent out so that we can easily examine the location of data in the output.
 
The width is set to 20 for each one of the three numbers a, b, and c.
cout<<"\n\n\n    With precision 5 and width 20  \n";
cout<<setprecision(5);
 
cout<<"\n         1         2         3         4         5 \n";
cout<<"12345678901234567890123456789012345678901234567890 \n" ;
cout<< setw(20)<<a <<setw(20)<< b <<setw(20)<< c ;
 
//***********************************************************
Precision is set to 3 digits after the decimal point.
 
A string of numbers is sent out so that we can easily examine the location of data in the output.
 
The width is set to 10 for each one of the three numbers a, b, and c.
cout<<"\n\n\n    With precision 3  and width 10  \n" ;
cout<<setprecision(3);
 
cout<<"\n         1         2         3         4         5 \n";
cout<<"12345678901234567890123456789012345678901234567890 \n" ;
cout<< setw(10)<<a <<setw(10)<< b <<setw(10)<< c ;
 
//***********************************************************
 
cout<<endl<<endl;
cout<<" enter e (exit) to terminate the program....";
char w;
cin>>w;
}
 
 /*     THESE ARE SOME OF THE OUTPUTS

The first number, when entered as fixed, needs 14 decimal digits.  However the number of significant digits is 1, so comes out correctly in scientific format.
 
Suppose we had precision 15. Do you think that we could have captured the detail at the end of number c?

 
Notice that 5 digits after the decimal point are shown even if they are zero.
 
The numbers are placed into the 20 slot openings with right justification.

 Please enter 3 numbers
  a = 0.0000000000001
 
  b = 123456789.45678
 
  c = -4567.0000000045
 
 
 
    With precision 5 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
         1.00000e-13         1.23457e+08        -4.56700e+03
Note that the third number  needs 10 places to be displayed.
One for sign
One for integer part
One for point
Three for precision
Four for exponent
RULE:
Width >= precision + 7
What do you think would happen if we violated this rule? Try it out.
With precision 3  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
 1.000e-13 1.235e+08-4.567e+03
 
 enter e (exit) to terminate the program....
 
 
************************************************************
Same story here for a different set of numbers.
 
Try to understand why the output looks as it does.
************************************************************
 
 
 
 
   Please enter 3 numbers
  a = 123
 
  b = 12
 
  c = 0.6789
 
 
 
    With precision 5 and width 20
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
         1.23000e+02         1.20000e+01         6.78900e-01
 
 
    With precision 3  and width 10
 
         1         2         3         4         5
12345678901234567890123456789012345678901234567890
 1.230e+02 1.200e+01 6.789e-01
 
 enter e (exit) to terminate the program....e                */

© 2001 J.Y. Kazakia. All rights reserved