Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Problem In My C++ Source Code


  • Please log in to reply
1 reply to this topic

#1 vhalerielee

vhalerielee

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:05:17 PM

Posted 22 September 2006 - 01:17 AM

Hey guys, I need a bit of help here. I am having problems with this code:

================================================================
#include <iostream>
#define MAX 50

void poly(int n, double a[], const double x0, double p)
{
   int i;
   p = a[n];
   for(i = n-1; i >= 0; i--)
   {
	 p = a[i] + x0*p;
   }
   printf("%lf", p);
}

int main()
{
   int i,n;
   double x0,p;
   double a[MAX+1];

   printf("Degree of polynomial: ");
   scanf("%d",&n);
   if(n > MAX || n == MAX)
   {
	  printf("Degree must be less than or equal to 50\n");
	  do
	  {
		  printf("Degree of polynomial:\n");
		  scanf("%d", &n);
	  } while (n > MAX || n == MAX);
   }
   printf("Coefficients, a[0], a[1], ..., a[%d]\n", n);
   for(i = 0; i <= n; i++){
	  scanf("%lf", &a[i]);
   }
   printf("value of x = ");
   scanf("%lf", &x0);
   poly(n,a,x0,p);
}
============================================================

this program is about evaluating a polynomial. For example, x^2 + 2x + 1, if x=2, the output would be 9. It won't work on polynomials with a degree higher than 2... there's a problem on the algebraic part... so maybe guys you could figure it out for me... I think there's a problem in this part:
void poly(int n, double a[], const double x0, double p)
{
   int i;
   p = a[n];
   for(i = n-1; i >= 0; i--)
   {
	 p = a[i] + x0*p;
   }
   printf("%lf", p);
}

thanks guys and more power!

BC AdBot (Login to Remove)

 


m

#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:17 AM

Posted 24 September 2006 - 10:19 AM

It works just fine once the programing errors are fixed. Well, it worked for 3,4, and 5 degrees, so I didn't check any further. I assume that since it worked in those cases that it should work in all cases.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users