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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<iostream>		//Required for cin, cout
#include<cmath>			//Required for sin()
#include<fstream>		// to build data file to be used by Excel to plot
#include <string>		// 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<b): of the function to be plotted \n";	// k<=20
  cin >> 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<b): of the function to be plotted 
-20 20
Enter the number of points you want over the range of 'a' to 'b'
100x and ROBBINSX 
-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
-14.400000 -0.293104
-14.000000 -0.289657
-13.600000 -0.286485
-13.200000 -0.283593
-12.800000 -0.280987
-12.400000 -0.278679
-12.000000 -0.276676
-11.600000 -0.274994
-11.200000 -0.273648
-10.800000 -0.272656
-10.400000 -0.272040
-10.000000 -0.271828
-9.600000 -0.272052
-9.200000 -0.272749
-8.800000 -0.273966
-8.400000 -0.275758
-8.000000 -0.278193
-7.600000 -0.281352
-7.200000 -0.285338
-6.800000 -0.290276
-6.400000 -0.296325
-6.000000 -0.303686
-5.600000 -0.312620
-5.200000 -0.323467
-4.800000 -0.336682
-4.400000 -0.352888
-4.000000 -0.372956
-3.600000 -0.398147
-3.200000 -0.430352
-2.800000 -0.472546
-2.400000 -0.529687
-2.000000 -0.610701
-1.600000 -0.733444
-1.200000 -0.939581
-0.800000 -1.354109
-0.400000 -2.602027
0.000000 1.000000
0.400000 -2.602027
0.800000 -1.354109
1.200000 -0.939581
1.600000 -0.733444
2.000000 -0.610701
2.400000 -0.529687
2.800000 -0.472546
3.200000 -0.430352
3.600000 -0.398147
4.000000 -0.372956
4.400000 -0.352888
4.800000 -0.336682
5.200000 -0.323467
5.600000 -0.312620
6.000000 -0.303686
6.400000 -0.296325
6.800000 -0.290276
7.200000 -0.285338
7.600000 -0.281352
8.000000 -0.278193
8.400000 -0.275758
8.800000 -0.273966
9.200000 -0.272749
9.600000 -0.272052
10.000000 -0.271828
10.400000 -0.272040
10.800000 -0.272656
11.200000 -0.273648
11.600000 -0.274994
12.000000 -0.276676
12.400000 -0.278679
12.800000 -0.280987
13.200000 -0.283593
13.600000 -0.286485
14.000000 -0.289657
14.400000 -0.293104
14.800000 -0.296821
15.200000 -0.300804
15.600000 -0.305053
16.000000 -0.309565
16.400000 -0.314340
16.800000 -0.319378
17.200000 -0.324682
17.600000 -0.330252
18.000000 -0.336092
18.400000 -0.342203
18.800000 -0.348591
19.200000 -0.355258
19.600000 -0.362211
20.000000 -0.369453
*/
}				// end of function 
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <format>
#include <vector>

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).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
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
Topic archived. No new replies allowed.