### Moving decimal spaces?

For this project we are supposed to have a table of decimals produced from our code. The code mainly works fine, it's that I do not know how to move the decimal space for the list of numbers. For example, I am trying to get -2.000000+01 and -3.6945283e-01, but instead I get -20.000000 -0.369453 as shown in the output section below. Any advice?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158`` ``````#include //Required for cin, cout #include //Required for sin() #include // to build data file to be used by Excel to plot #include // for using filename and using namespace std; double ROBBINS (double x); //Function Prototype int main () { double a, b, x_incr, new_x; // Declare objects in main() int Number, loop; string filename; ofstream fout; // declare object for sending data to the output file cout << "enter the name of the data file to send data used for plotting, from this program\n"; cin >> filename; fout.open (filename); // open file for inputing data from this program cout << "Enter endpoint range a and b (a> a >> b; cout << "Enter the number of points you want over the range of 'a' to 'b'\n"; cin >> Number; x_incr = (b - a) / Number; // increment between the points based on number wanted loop = int (b - a) / x_incr; // loop gives the for loop repetitions equal to number points wanted cout << loop; // check that shows loop and Number are the same cout.setf (ios::fixed); cout.precision (6); // Set Formats cout << "x and ROBBINSX \n"; // table of sinc(x) values call to sinc() for (int k = 0; k <= loop; k++) { new_x = a + k * x_incr; cout << new_x << " " << ROBBINS (new_x) << endl; //echo vales to screen, call to sinc() here! fout << new_x << " " << ROBBINS (new_x) << endl; // building data file for excel plotting } return 0; // Exit program. fout.close (); } // end main unction used on the next slide double ROBBINS (double x) /* DEMO This function evaluates the sinc function */ { if (x == 0) // x here picks up the values of x in main() { return 1; // but this x has its own memory and does not } // interfere with the values of x in main() else { return -exp(fabs(x)/10) /fabs(x); } /* OUTPUT enter the name of the data file to send data used for plotting, from this program MonaeFile Enter endpoint range a and b (a
Last edited on
Perhaps line 26 should be `cout.setf (ios::scientific);`
Just for gits and shiggles... if your compiler supports C++20 you could get a nice looking table output without too much hassle using std::format:
 ``123456789101112131415161718192021222324252627`` ``````#include #include #include int main() { // fill a vector with by-rote data to display std::vector vec { -20.000000, -0.369453, -19.600000, -0.362211, -19.200000, -0.355258, -18.800000, -0.348591, -18.400000, -0.342203, -18.000000, -0.336092, -17.600000, -0.330252, -17.200000, -0.324682, -16.800000, -0.319378, -16.400000, -0.314340, -16.000000, -0.309565, -15.600000, -0.305053, -15.200000, -0.300804, -14.800000, -0.296821 }; for (size_t i { }; i < vec.size(); i += 2) { std::cout << std::format("{:<15.5E}{:<15.5e}\n", vec[i], vec[i + 1]); } }``````
 ```-2.00000E+01 -3.69453e-01 -1.96000E+01 -3.62211e-01 -1.92000E+01 -3.55258e-01 -1.88000E+01 -3.48591e-01 -1.84000E+01 -3.42203e-01 -1.80000E+01 -3.36092e-01 -1.76000E+01 -3.30252e-01 -1.72000E+01 -3.24682e-01 -1.68000E+01 -3.19378e-01 -1.64000E+01 -3.14340e-01 -1.60000E+01 -3.09565e-01 -1.56000E+01 -3.05053e-01 -1.52000E+01 -3.00804e-01 -1.48000E+01 -2.96821e-01```

The format specifier is left-justify (<), a total block of 15 characters with 5 decimal places in scientific format.
slight picking..
abs works on doubles in c++. fabs is dated (or C code).
 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include #include #include using namespace std; double sinc( double x ) { return abs( x ) < 1e-30 ? 1.0 : sin( x ) / x; } int main() { string filename; cout << "Enter filename for output: "; cin >> filename; ofstream fout( filename ); double a, b; int n; cout << "Enter a, b, n for n points in [a,b]: "; cin >> a >> b >> n; double dx = ( b - a ) / ( n - 1 ); #define FMT << scientific << setprecision( 6 ) << setw( 18 ) << cout FMT "x" FMT "sinc(x)" << '\n'; for ( int k = 0; k < n; k++ ) { double x = a + k * dx, y = sinc( x ); cout FMT x FMT y << '\n'; fout FMT x FMT y << '\n'; } }``````

Plot with, e.g.,
gnuplot -persist -e "p 'results.txt' w l"

 ```Enter filename for output: results.txt Enter a, b, n for n points in [a,b]: -20 20 81 x sinc(x) -2.000000e+01 4.564726e-02 -1.950000e+01 3.105333e-02 -1.900000e+01 7.888274e-03 -1.850000e+01 -1.851247e-02 -1.800000e+01 -4.172151e-02 -1.750000e+01 -5.575006e-02 -1.700000e+01 -5.655279e-02 -1.650000e+01 -4.313851e-02 -1.600000e+01 -1.799396e-02 -1.550000e+01 1.332048e-02 -1.500000e+01 4.335252e-02 -1.450000e+01 6.447552e-02 -1.400000e+01 7.075767e-02 -1.350000e+01 5.953959e-02 -1.300000e+01 3.232054e-02 -1.250000e+01 -5.305752e-03 -1.200000e+01 -4.471441e-02 -1.150000e+01 -7.612628e-02 -1.100000e+01 -9.090820e-02 -1.050000e+01 -8.378055e-02 -1.000000e+01 -5.440211e-02 -9.500000e+00 -7.910644e-03 -9.000000e+00 4.579094e-02 -8.500000e+00 9.393966e-02 -8.000000e+00 1.236698e-01 -7.500000e+00 1.250667e-01 -7.000000e+00 9.385523e-02 -6.500000e+00 3.309538e-02 -6.000000e+00 -4.656925e-02 -5.500000e+00 -1.282801e-01 -5.000000e+00 -1.917849e-01 -4.500000e+00 -2.172289e-01 -4.000000e+00 -1.892006e-01 -3.500000e+00 -1.002238e-01 -3.000000e+00 4.704000e-02 -2.500000e+00 2.393889e-01 -2.000000e+00 4.546487e-01 -1.500000e+00 6.649967e-01 -1.000000e+00 8.414710e-01 -5.000000e-01 9.588511e-01 0.000000e+00 1.000000e+00 5.000000e-01 9.588511e-01 1.000000e+00 8.414710e-01 1.500000e+00 6.649967e-01 2.000000e+00 4.546487e-01 2.500000e+00 2.393889e-01 3.000000e+00 4.704000e-02 3.500000e+00 -1.002238e-01 4.000000e+00 -1.892006e-01 4.500000e+00 -2.172289e-01 5.000000e+00 -1.917849e-01 5.500000e+00 -1.282801e-01 6.000000e+00 -4.656925e-02 6.500000e+00 3.309538e-02 7.000000e+00 9.385523e-02 7.500000e+00 1.250667e-01 8.000000e+00 1.236698e-01 8.500000e+00 9.393966e-02 9.000000e+00 4.579094e-02 9.500000e+00 -7.910644e-03 1.000000e+01 -5.440211e-02 1.050000e+01 -8.378055e-02 1.100000e+01 -9.090820e-02 1.150000e+01 -7.612628e-02 1.200000e+01 -4.471441e-02 1.250000e+01 -5.305752e-03 1.300000e+01 3.232054e-02 1.350000e+01 5.953959e-02 1.400000e+01 7.075767e-02 1.450000e+01 6.447552e-02 1.500000e+01 4.335252e-02 1.550000e+01 1.332048e-02 1.600000e+01 -1.799396e-02 1.650000e+01 -4.313851e-02 1.700000e+01 -5.655279e-02 1.750000e+01 -5.575006e-02 1.800000e+01 -4.172151e-02 1.850000e+01 -1.851247e-02 1.900000e+01 7.888274e-03 1.950000e+01 3.105333e-02 2.000000e+01 4.564726e-02```

Last edited on