CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    5

    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.
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: graph polynomial using 2d array

    Quote Originally Posted by jpetrilli View Post
    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.
    Code:
    y[j++]=...
    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
    Last edited by Paul McKenzie; April 27th, 2010 at 08:25 AM.

  4. #4
    Join Date
    Feb 2010
    Posts
    5

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: graph polynomial using 2d array

    Quote Originally Posted by jpetrilli View Post
    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
    Last edited by Paul McKenzie; April 27th, 2010 at 09:40 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured