1 Attachment(s)
graph polynomial using 2d array
So here is the assignment given...
Use only one function, main for this program. Calculate the values of y based on values of x
from −10≤x≤10 by increments of 0.5. Store the x values in a single dimensional double
array and the y values in another single dimensional double array.
Find the minimum and maximum values for y. Use these values to scale the y axis into 50
divisions. The x axis is already divided into 41 divisions. Use the resulting scaling to find the
row and column of a 50X41 character array in which to place a marker indicating a point on the
polynomial curve.
I'm having trouble getting this program to work. Any advice would be appreciated.
Re: graph polynomial using 2d array
Are you getting compiler errors? If so, it's easier if you post them. ;)
Some things I can point out.
It's bad style to declare all local variables at the top of a function. That was necessary in C a long time ago, but in C++ you should declare variables as late as possible.
Code:
for (double i=-10.0; i<=10.0; i+=0.5) {
}
Note that this will work when incrementing with a power of 2, but not in general. Step through the following program to see how the values in the loop will differ.
Code:
int main() {
double acc = -10.;
for (int i = 0; i < 30; ++i) {
acc += 1. / 3;
double tmp = -10. + (i + 1) / 3.;
}
}
Floating point numbers are not precise. When you do operations on them these imprecisions can add up.
Re: graph polynomial using 2d array
Quote:
Originally Posted by
jpetrilli
So here is the assignment given...
Use only one function, main for this program. Calculate the values of y based on values of x
from −10≤x≤10 by increments of 0.5. Store the x values in a single dimensional double
array and the y values in another single dimensional double array.
Code:
for (i=-10.0; i<=10.0; i+=0.5) // fills x and y value array with data
{
x[j]=i;
y[j++]=(a*i*i*i)+(b*i*i)+c*i+d;
}
Do not use floating point variables as loop counters.
Explain this code. When does j get incremented? Or is this ambiguous and undefined? Suggestion -- rewrite it so that it is valid and no one has to look at it 5 times to figure out what it's supposed to do or whether it's guaranteed to work. It doesn't hurt in breaking up that statement into something a little more coherent.
Rewritten using integers:
Code:
for (int =-100; i <= 100; i+=5) // fills x and y value array with data
{
double temp = i / 10.0;
x[j]=temp;
y[j]=(a* temp* temp * temp)+(b* temp* temp)+c*temp+d;
++j;
}
Regards,
Paul McKenzie
Re: graph polynomial using 2d array
The declarations at the top of the function, that is just a requirement by my teacher.
As for the y[j++], I wanted it to do what you rewrote the code to do.
Thanks for your help so far I will try to rewrite the code and repost it if i'm still having trouble.
Re: graph polynomial using 2d array
Quote:
Originally Posted by
jpetrilli
The declarations at the top of the function, that is just a requirement by my teacher.
What declarations? Are you referring to the for() loop? If you are, then first of all, the teacher just described something to you in English, and you made the mistake of literally making a loop out of what was described. If you are talking about the for() loop, do you know why you shouldn't have floating point variables as loop counters?
The loop that I wrote is the same as your original loop except that the number of times the loop is executed is exactly the same, regardless of the compiler or compiler settings. The reason why you never have floating point values as loop counters is that addition of floating point numbers is not exact.
Here is a link for you:
https://www.securecoding.cert.org/co...+loop+counters
Regards,
Paul McKenzie