program asks a user to type in Xi(initial) Xf(where calculation stops at) Inc(what amount it will be increasing by) then to create another function to calculate Y= 5x^3 + 3x + 4 and return value Y. on the Main to create a one dimensional array and to store the results from Y in the array and print them out. NOTE: the loop for goes like this; for(x=Xi; x<=Xf; x=x + Inc)....so far I have this:
It looks like you're a beginner. I suggest that you do this program one step at a time.
- Change main() so that it just calls read() and prints out the values read. If you do this, you'll find that read() has some errors.
- Next, add code to main() that will call your function to compute Y= 5x^3 + 3x + 4. Call it with 2 or 3 values and print out the results. Check the results by hand. You will immediately discover that (1) you have no such function, even though you description explicitly says that there should be one, and (2) your code to compute the function is incorrect.
-Now add code to main to create the array and populate it.
- Finally, add code to print out the array.
Finally, why are xi, xf and inc ints? Shouldn't they be floats?
Ooh wow I forgot to call the functions on main()....ok so I did that, but how do you pass the results from Y into the array? I still can't be able to figure that out :/, what I mean is how do I store the results from the ecuaciĆ³n to the array, printing them out is easy but storing the I still can't seem to wrap my head around that.
Now that you are correctly calling the function in line 30 and storing the results in the array named array[] (you might want to give that a more meaningful name), you can look at the calculate function and see that you no longer need the xf and inc arguments. These values are used for looping (line 28) and are no longer needed when calling the function.
Your index value j never get set or incremented. As a result, you are writing and reading from the same random memory location each time through the loop. You need to initialize j to 0, increment it each time through the loop, and make sure you don't exceed your array boundaries.
Your initial problem description used xi as an initial value, and that value seems to have disappeared from your code.
To cover all of these issues, I suggest code similar to the following:
#include <iostream>
#include <cstdlib>
#include <cmath>
constint arraySize = 30; // constant make code easier to maintain than magic
// numbers in the code
int calculate(int x)
{
int y= (5 * pow(i,3)) + (3 * i) + 4;
return (y);
}
int main()
{
int array[arraySize];
int xi, xf, inc;
cout<<"where does it start?";
cin>> xi;
cout<<"where does it end?";
cin>> xf;
cout<<"how much will it increase by?";
cin>> inc;
int x = xi;
// loop on the number of array elements you have so you don't exceed the bounds
for(int i=0; i < arraySize; i++)
{
array[i]=calculate(x);
cout<<"el resultado es:"<<array[i]<<endl;
// increment your x value inside the loop and break out if you exceed your
// final value. This allows the outer loop to check array bounds.
x = x + inc;
if (x > xf)
break;
}
return EXIT_SUCCESS;
}
Note @dhayden's comment. You might want to make array, xi, xf and inc be floating point numbers (float or double) rather than integers.