// Array example for cs 149, spring 2001 v. 3/29/06 // Illustrates use of setw and setprecision. #include #include int main () { const int N=100; double Sum=0.0; int i; // Print the same number using different setw and setprecision values // The second column of numbers is hard to read for ( i=1; i <= N; i++ ) { Sum += i; cout << setw( 5 ) << i << setw( 4 ) << Sum << endl; } cout << endl; // The second column is easier to read since the width is 6. for ( i=1; i <= N; i++ ) { Sum += 10*i; cout << setw( 5 ) << i << setw( 6 ) << Sum << endl; } cout << endl; // The second column prints with scientific notation; harder to read? for ( i=1; i <= N; i++ ) { Sum += 10000*i; cout << setw( 5 ) << i << setw( 12 ) << Sum << endl; } cout << endl; // The second column does not switch to scientific notation since a // larger precision is used. for ( i=1; i <= N; i++ ) { Sum += 100000*i; cout << setw( 5 ) << i << setw( 12 ) << setprecision( 12 ) << Sum << endl; } cout << endl; return 0; }