Range of Numbers Issue UPDATED* new issue

1) Define a symbolic constant for the maximum number of points
2) Ask the user for the values of min and max;
3) Using the three values above(SIZE, min and max) compute the values for x;
4) Use Equation 1 to compute the values of f(x);
5) Display a table with the following format:

So those are the requirements. what im having problem with is with the user input for min and max. the program i did does get the output that is shown as an example but thats only if the user enter -2 as min and 2 as max. any other range for example -3 and 3 would go beyond the range for the output. The constant(SIZE) which is 21 (the total number of values) does work only for the example, which i think is the problem here. I dont know if im going about it the right way or not. Any thoughts?

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

using namespace std;

int main(){
 

   int userMin; 
   int userMax;
   double inc = 0.2;			   
   const int SIZE = 21;
   double arr[SIZE];
   const int SIZE2 = 21;
   double arr2[SIZE2];
   int count = 0;		
   cout << "enter min ";
   cin >> userMin;
   cout << "enter max ";
   cin >> userMax;
   double x = inc*count + userMin;
   

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

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

   return 0;
}

ex output
     -2      -19
   -1.8      -14
   -1.6      -9.6
   -1.4      -6.1
   -1.2      -3.3
     -1      -1
   -0.8       0.78
   -0.6       2.2
   -0.4       3.3
   -0.2       4.2
      0       5
    0.2       5.8
    0.4       6.7
    0.6       7.8
    0.8       9.2
      1       11
    1.2       12
    1.4       16
    1.6       20
    1.8       24
      2       29
Last edited on
Alright, well first off, I am not 100% sure what is being asked here, anymore info you could include?

Secondly, let's start with what I can see just from the code you posted.

1.) In your entire program, none of the code below here is used at all? Are you trying to put these values into an array?

1
2
3
double arr[SIZE];
const int SIZE2 = 21;
double arr2[SIZE2];

2.)You just declared that count would equal 0. Now you are multiplying by count or 0,
then you add userMin. So x is effectively userMin right from the start.

double x = inc*count + userMin;

So when you code

x = userMin + inc*count;

You are actually making x = userMin + userMin + inc*count

A little more explanation as to what the intent of this project is would be appreciated.
wow sorry about that, its for an array, the only instructions stated are the ones mentioned at the top and this one below also f(x)= is the formula 2 * x*x*x + 4 * x + 5
instructions: evaluate an equation f(x) over a range of x values (xmin, xmax) and make a table like the one shown below, <--- this is the sample output i posted b) The user will enter the values for xmin and xmax. everything besides what was stated in the instructions i did on my own to match the sample of the output

this is the code with the array in it that i left out
1
2
3
4
5
6
7
8
9
10
11
12
13
while (x <= userMax)			
   {
      for (int e = 0; e < SIZE; e++)
      {
         arr[e] = x;
         arr2[e] = 2 * x*x*x + 4 * x + 5;
         cout << fixed << setw(15) << setprecision(2) << arr[e]
            << setw(15) << setprecision(2) << arr2[e] << endl;

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

Last edited on
No problem, also I just noticed you seem to have an increment variable.

Was this decided or was it arbitrary?

EDIT: Just saw the output, okay inc is decided.
Last edited on

2.)You just declared that count would equal 0. Now you are multiplying by count or 0,
then you add userMin. So x is effectively userMin right from the start.


it increments count so it would be for example
x=-2+0.2*0; this would =-2
x=-2+0.2*1; this would =-1.8
and so on until it reaches 2
Also, the number of increments between any two numbers is going to change.

For .2 inc between -2 and 2, there are 20 increments.

For .2 inc between -4 and 4, there are 40 increments.

Do you see the problem here? If you try to print out all increments between -4 and 4 but are limited to printing out only 20 of them you are going to be missing half of them.
yea i figured that out but didnt know how to go about in solving that issue. the constant for the total number of values is what im guessing is part of it
Last edited on
How about this?

const int SIZE = (userMax - userMin) / inc;

This will make the size equal to the total number of increments between your min and max value.
it says expression must have a constant value. im also confused with ^that
Last edited on
Post full code please as you have it now please.
so i solved the issue with the constant. i just keep it 21 and switch the variable inc to float inc =(userMax-userMin)/SIZE; BUT now i get a new problem with the output. if 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. 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
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
#include <iostream> 
#include <iomanip>

#define size 20

using namespace std;

int main()
{

     float userMin;
     float userMax;
     float arr[size];
     float arr2[size];
     int count = 0;
	
     cout << "enter min ";
     cin >> userMin;
     cout << "enter max ";
     cin >> userMax;
	
     float inc = (userMax - userMin) / size;
     float x = inc*count + userMin;

     cout << fixed << setprecision(1);
     while (count < size)
     {
	arr[count] = x;
	arr2[count] = 2 * x*x*x + 4 * x + 5;
	cout << "\t" << arr[count];
	cout << "\t" << arr2[count] << "\n";

	count++;
	x += inc;
     }

     return 0;
}
Topic archived. No new replies allowed.