program doesnt increment right

so there is suppose to be a constant that shows the total number of values to be shown in this range. The user enters two values, lets say userMin is -2 and userMax is 2, it's suppose to add 0.2 to -2 and so on until it reaches 2. It starts doing that up until it reaches -1.0, then it goes to -0.9, -0.7.... etc, It also goes beyond 2, which i think is because the incrementation is wrong. Any ideas?

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
#include <iostream> 
#include <iomanip>

using namespace std;

int main()
{

   cout << fixed << setprecision(1);
   float userMin;
   float userMax;
  
   const int SIZE = 21;
   float arr[SIZE];
   const int SIZE2 = 21;
   float arr2[SIZE2];
   int count = 0;
   cout << "enter min ";
   cin >> userMin;
   cout << "enter max ";
   cin >> userMax;
   float inc = (userMax-userMin)/SIZE;
   float x = inc*count + userMin;



   while (x <= userMax)
   {
      for (int e = 0; e < SIZE; e++)
      {
         arr[e] = x;
         arr2[e] = 2 * x*x*x + 4 * x + 5;
         cout << setw(15) << arr[e]
            << setw(15) << arr2[e]
            << endl;

         count++;
         x = userMin + inc*count;
      }
   }

   return 0;
}


 -2.0      -19.0
   -1.8      -14.1
   -1.6      -10.0
   -1.4      -6.5
   -1.2      -3.7
   -1.0      -1.5
   -0.9       0.3
   -0.7       1.7
   -0.5       2.9
   -0.3       3.8
   -0.1       4.6
    0.1       5.4
    0.3       6.2
    0.5       7.1
    0.7       8.3
    0.9       9.7
    1.0       11.5
    1.2       13.7
    1.4       16.5
    1.6       20.0
    1.8       24.1
    2.0       29.0
    2.2       34.8
    2.4       41.5
    2.6       49.3
    2.8       58.2
    3.0       68.3
    3.1       79.7
    3.3       92.4
    3.5       106.6
    3.7       122.3
    3.9       139.7
    4.1       158.7
    4.3       179.6
    4.5       202.3
    4.7       226.9
    4.9       253.6
    5.0       282.4
    5.2       313.4
    5.4       346.7
    5.6       382.3
    5.8       420.4
Last edited on
I would explore why when you put in 20 for your size, and change "while" to "if", it performs correctly.
Last edited on
can you explain why that happens with 20 instead of 21. i also put a break in the nested loop to stop it at 2. if i dont itll keep going past it.
Edit: the constant has to match up with the total number of values
Last edited on
Don't count the min size value.
You don't need the while loop at line 27 at all. If you remove that you get this output:
$ ./foo
enter min -2
enter max 2
           -2.0          -19.0
           -1.8          -14.1
           -1.6          -10.0
           -1.4           -6.5
           -1.2           -3.7
           -1.0           -1.5
           -0.9            0.3
           -0.7            1.7
           -0.5            2.9
           -0.3            3.8
           -0.1            4.6
            0.1            5.4
            0.3            6.2
            0.5            7.1
            0.7            8.3
            0.9            9.7
            1.0           11.5
            1.2           13.7
            1.4           16.5
            1.6           20.0
            1.8           24.1

Looks close, but is it really? Let's comment out line 9. Don't remove it - we'll put it back later. Here is the output with line 9 commented out:
$ ./foo
enter min -2
enter max 2
             -2            -19
       -1.80952       -14.0882
       -1.61905       -9.96426
       -1.42857       -6.54519
        -1.2381       -3.74808
       -1.04762       -1.49001
      -0.857143       0.311954
      -0.666667        1.74074
       -0.47619        2.87928
      -0.285714         3.8105
     -0.0952381        4.61732
      0.0952381        5.38268
       0.285714         6.1895
       0.476191        7.12072
       0.666667        8.25926
       0.857143        9.68805
        1.04762          11.49
         1.2381        13.7481
        1.42857        16.5452
        1.61905        19.9643
        1.80952        24.0882

Well that ain't right. The x should increase by 0.2. Small rounding errors won't account for these large differences so something is clearly wrong with the increment value. Looking at the code I see a fence post problem. If you divide the range from -2 to 2 into intervals 0.1 wide, then you get 21 values, but only 20 intervals. So let's change line 22 to
float inc = (userMax-userMin)/(SIZE-1);
$ ./foo
enter min -2
enter max 2
             -2            -19
           -1.8        -13.864
           -1.6         -9.592
           -1.4         -6.088
           -1.2         -3.256
             -1             -1
           -0.8          0.776
           -0.6          2.168
           -0.4          3.272
           -0.2          4.184
    2.98023e-08              5
            0.2          5.816
            0.4          6.728
            0.6          7.832
            0.8          9.224
              1             11
            1.2         13.256
            1.4         16.088
            1.6         19.592
            1.8         23.864
              2             29

Much better! The highlighted value might appear wrong, but it's really just a number very close to zero. If you uncomment line 9 you'll get the right output:
$ ./foo
enter min -2
enter max 2
           -2.0          -19.0
           -1.8          -13.9
           -1.6           -9.6
           -1.4           -6.1
           -1.2           -3.3
           -1.0           -1.0
           -0.8            0.8
           -0.6            2.2
           -0.4            3.3
           -0.2            4.2
            0.0            5.0
            0.2            5.8
            0.4            6.7
            0.6            7.8
            0.8            9.2
            1.0           11.0
            1.2           13.3
            1.4           16.1
            1.6           19.6
            1.8           23.9
            2.0           29.0



wow thanks , we must have commented at the same time so i didnt see your post until now. thanks again
Topic archived. No new replies allowed.