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):

``````#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;

}

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 :]

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 …

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.

## All 5 Replies

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?

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?

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.

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.

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:

``````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"

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

.

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 += a.coef;
for ( int i = 0; i <= b.deg; i++ )
c.coef += b.coef;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.