| | |
C Beginner: Adding Two Polynomials of degree n
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
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):
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 :]
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):
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <math.h> /* f = g + h; n is the max degree of f, g, and h */ void add(double f[], double g[], double h[], int n); #define N 1000 int main () { double poly1[N], poly2[N], solution[N]; int degree = 0; add(poly1, poly2, solution, degree); } void add(double f[], double g[], double h[], int n) { int i, degree; printf("Enter the degree of your polynomials: "); scanf_s("%d", &n); i = n; degree = i; while(0 <= n) { printf("\nEnter the coefficient for polynomial 1 of x^%d: ", n); scanf_s("%lf", &g[n]); n--; } while(0 <= i) { printf("\nEnter the coefficient for polynomial 2 of x^%d: ", i); scanf_s("%lf", &h[n]); i--; } while(0 <= degree) { if(degree == 0) { f[degree] = g[degree] + h[degree]; printf("%lf\n", f[degree]); } else { f[degree] = g[degree] + h[degree]; printf("%lf*x^%d + \n", f[degree], degree); } degree--; } }
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
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?
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?
•
•
Join Date: Aug 2008
Posts: 2
Reputation:
Solved Threads: 0
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?
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:
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.
.
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:
C Syntax (Toggle Plain Text)
SIMPLE PROGRAM FLOW EXAMPLE "main" -- gets the input from user as to how many degrees -- get each of the coefficients for all three polynomials. -- pass this info into the function "add" "add" -- add the same-degree coefficients together of each polynomial -- return the results back to "main" "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.
![]() |
Other Threads in the C Forum
- Previous Thread: Need help to convert a string to an integer
- Next Thread: from the user input, how i can count the occurance of words . Help
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming stack standard string strings structures suggestions system systemcall test testautomation unix user variable voidmain() wab win32api windows.h






