equation Y= 5x^3 + 3x + 4 with a loop

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:

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
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;



float calculate(int xi, int xf, int inc, float Y)
{
		for(int x=xi; x<=xf; x=x + inc)
	{
		Y= 5 * pow(x,2) + 3 * x + 4;
	}
	return(Y);
	
}

int main()
{
	float array[30];
	int xi, xf, inc, i;
	float result;
	cout<<"enter the initial value:";
	cin>> xi;
	cout<<"enter the final value:";
	cin>> xf;
	cout<<"how much will it increase by?";
	cin>> inc;
	
	result= array[i];
	for(int i=0; i<30; i++)
	{ 
		cout<<"the results of the equation are:"<<array[i]<<endl;
	}
	
return 0;
}

Last edited on
I need to know if this is right or what am I doing wrong, thanks!
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/

The "another function" has to calculate one Y from one X. No loop.

The main() has the loop that calls the another function with different values of X and stored the returned result into the array.


You do ask whether your code is "right".

Does it compile without errors?
Does the binary run without crashing?
Are the results what they should?

I see that it won't compile. Look at the compiler error messages.
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.
what are you exactly trying to help you find?
Store the results from the ecuation that are in Y into the array and print them out.
You haven't declared anything yet. If you run your program you should see that your variables are not declared.
ok I made adjustments but it still doesn't store the values of Y into the array

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
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;



float calculate(int xi, int xf, int inc, float Y)
{
		for(int x=xi; x<=xf; x=x + inc)
	{
		Y= 5 * pow(x,2) + 3 * x + 4;
	}
	return(Y);
	
}

int main()
{
	float array[30];
	int xi, xf, inc, i;
	float result;
	cout<<"enter the initial value:";
	cin>> xi;
	cout<<"enter the final value:";
	cin>> xf;
	cout<<"how much will it increase by?";
	cin>> inc;
	
	result= array[i];
	for(int i=0; i<30; i++)
	{ 
		cout<<"the results of the equation are:"<<array[i]<<endl;
	}
	
return 0;
}
Can you explain line 30?
What is the value of i in it?
What values does array have on line 30?
Where (within lines 31-37) will you use the result?

You do have a separate function. Do you use it? Where?


Do compare 5x^3 and 5 * pow(x,2).
for(i = 0; i < arraysize; i++)
{
array[i] = calculate(stuff..);
}

there!!! resolved!

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
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;



int calculate(int i, int xf, int inc)
{
    int y;
	
	y= (5 * pow(i,3)) + (3 * i) + 4;

	return (y);
	
}

int main()
{
	int array[30];
	int xf, inc, j;
	cout<<"where does it end?";
	cin>> xf;
	cout<<"how much will it increase by?";
	cin>> inc;
	
	
	for(int i=0; i<=xf; i=i + inc)
	{
		array[j]=calculate(i, xf, inc);
		cout<<"el resultado es:"<<array[j]<<endl;
	}
	
return EXIT_SUCCESS;
}
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:

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
#include <iostream>
#include <cstdlib>
#include <cmath>
const int 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.

Edit: Replaced incorrect j with loop counter i
Last edited on
Topic archived. No new replies allowed.