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