C Beginner: Adding Two Polynomials of degree n

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 2
Reputation: angelicxtc is an unknown quantity at this point 
Solved Threads: 0
angelicxtc angelicxtc is offline Offline
Newbie Poster

C Beginner: Adding Two Polynomials of degree n

 
0
  #1
Aug 26th, 2008
Hi. I am taking my first C class and I seem to be having trouble with this program. The problem is:

Write a function that adds two polynomials of at most degree n.

/* f = g + h; n is the max degree of f, g, and h */

void add(double f[], double g[], double h[], int n)
{
...

This is my c code (it is also attached):

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. /* f = g + h; n is the max degree of f, g, and h */
  6.  
  7. void add(double f[], double g[], double h[], int n);
  8.  
  9. #define N 1000
  10.  
  11. int main ()
  12. {
  13. double poly1[N], poly2[N], solution[N];
  14. int degree = 0;
  15.  
  16. add(poly1, poly2, solution, degree);
  17. }
  18.  
  19. void add(double f[], double g[], double h[], int n)
  20. {
  21. int i, degree;
  22.  
  23. printf("Enter the degree of your polynomials: ");
  24. scanf_s("%d", &n);
  25. i = n;
  26. degree = i;
  27.  
  28. while(0 <= n)
  29. {
  30. printf("\nEnter the coefficient for polynomial 1 of x^%d: ", n);
  31. scanf_s("%lf", &g[n]);
  32. n--;
  33. }
  34. while(0 <= i)
  35. {
  36. printf("\nEnter the coefficient for polynomial 2 of x^%d: ", i);
  37. scanf_s("%lf", &h[n]);
  38. i--;
  39. }
  40. while(0 <= degree)
  41. {
  42. if(degree == 0)
  43. {
  44. f[degree] = g[degree] + h[degree];
  45. printf("%lf\n", f[degree]);
  46. }
  47. else
  48. {
  49. f[degree] = g[degree] + h[degree];
  50. printf("%lf*x^%d + \n", f[degree], degree);
  51. }
  52. degree--;
  53. }
  54. }

My code builds and executes without warnings and errors, but when I execute it, I get a run time error that says: Run-Time Check Failure #2 - Stack around the variable 'solution' was corrupted. I was hoping someone could explain to me what that means and what I could do to fix the problem. Any help would be extremely appreciated. Thanks guys :]
Last edited by angelicxtc; Aug 26th, 2008 at 5:47 pm. Reason: giving a more specific understand of the problem
Attached Files
File Type: cpp exercise1.cpp (988 Bytes, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,665
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: C Beginner: Adding Two Polynomials of degree n

 
0
  #2
Aug 26th, 2008
in the future, dont make duplicate threads for the same problem. please go and close your other thread with the same title.


I'm using the free MSVC compiler, and im not getting any runtime errors... it compiled and ran without any issues.

of course it is incomplete and does not give the right answer, but theres nothing wrong "compile wise"

what compiler and OS are you using?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: angelicxtc is an unknown quantity at this point 
Solved Threads: 0
angelicxtc angelicxtc is offline Offline
Newbie Poster

Re: C Beginner: Adding Two Polynomials of degree n

 
0
  #3
Aug 26th, 2008
Sorry, I'm new to this and I didn't realize that I posted the same thread twice. I am using Visual C++ 2005 Express Edition to compile the program. I'm still getting a run-time error when I execute the program and I also noticed that it's not printing the right number either, regardless of what I input. Any advice on how to fix this?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,665
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: C Beginner: Adding Two Polynomials of degree n

 
0
  #4
Aug 26th, 2008
you're not running the same program i am, apparently.

im running the code you pasted above. it compiles and runs without issue.

im using MSVC also. perhaps you are running something different than what you posted.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,665
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: C Beginner: Adding Two Polynomials of degree n

 
0
  #5
Aug 26th, 2008
OH ... Wait, are you trying to compile this as a C++ program? this is not a C++ program. what you have here is a basic "C" program.

thats your first problem. you need to figure out what language your programming in.

as to your "real" problem... you are getting the input (mostly) correct, but you are not adding the coefficients of similar degrees together.

in other words, your single function "add" is doing everything BUT adding.

another problem appears that you should have THREE (3) polynomials, not two. hence the "add" function has three inputs (f, g, h) and one output (the sum of the three). each of these are arrays of a size that depends on the degree of the polynomials.

it would help if you wrote your "add" function to actually add the coefficients that you should have already gotten from your "main" routine, and passed TO it.

so before going any further, stop now, and rewrite your program to make sense according to this pattern:

  1. SIMPLE PROGRAM FLOW EXAMPLE
  2.  
  3. "main" -- gets the input from user as to how many degrees
  4. -- get each of the coefficients for all three polynomials.
  5. -- pass this info into the function "add"
  6.  
  7. "add" -- add the same-degree coefficients together of each polynomial
  8. -- return the results back to "main"
  9.  
  10. "main" -- format and print the results

dont worry if it doesnt work exactly right, just get it in this form. then, at least, it will make sense to you and to anyone who reads it.



.
Last edited by jephthah; Aug 26th, 2008 at 7:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C Beginner: Adding Two Polynomials of degree n

 
0
  #6
Aug 27th, 2008
For portability issues you wanna get rid of that scanf_s
The rough guide for addition would be

for ( int i = 0; i <= a.deg; i++ )
c.coef[i] += a.coef[i];
for ( int i = 0; i <= b.deg; i++ )
c.coef[i] += b.coef[i];
Last edited by iamthwee; Aug 27th, 2008 at 8:14 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC